3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2026-06-10 02:37:12 +00:00
rust-cache/src/utils.ts
Arpad Borsos be9aa033ee
package maintenance
- update to TS6
- replace ncc by rollup, which seems to now be the recommended workflow according to <https://github.com/actions/typescript-action>
- remove buildjet provider, as they are shutting down, see <https://buildjet.com/for-github-actions/blog/we-are-shutting-down>
2026-06-06 14:02:52 +02:00

82 lines
1.8 KiB
TypeScript

import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as warpbuildCache from "@actions/warpbuild-cache";
import * as ghCache from "@actions/cache";
import fs from "fs";
export function reportError(e: any) {
const { commandFailed } = e;
if (commandFailed) {
core.error(`Command failed: ${commandFailed.command}`);
core.error(commandFailed.stderr);
} else {
core.error(`${e.stack}`);
}
}
export async function getCmdOutput(cmdFormat: string, cmd: string, options: exec.ExecOptions = {}): Promise<string> {
cmd = cmdFormat.replace("{0}", cmd);
let stdout = "";
let stderr = "";
try {
await exec.exec(cmd, [], {
silent: true,
listeners: {
stdout(data) {
stdout += data.toString();
},
stderr(data) {
stderr += data.toString();
},
},
...options,
});
} catch (e) {
(e as any).commandFailed = {
command: cmd,
stderr,
};
throw e;
}
return stdout;
}
export interface GhCache {
isFeatureAvailable: typeof ghCache.isFeatureAvailable;
restoreCache: typeof ghCache.restoreCache;
saveCache: (paths: string[], key: string) => Promise<string | number>;
}
export interface CacheProvider {
name: string;
cache: GhCache;
}
export function getCacheProvider(): CacheProvider {
const cacheProvider = core.getInput("cache-provider");
let cache: GhCache;
switch (cacheProvider) {
case "github":
cache = ghCache;
break;
case "warpbuild":
cache = warpbuildCache;
break;
default:
throw new Error(`The \`cache-provider\` \`${cacheProvider}\` is not valid.`);
}
return {
name: cacheProvider,
cache: cache,
};
}
export async function exists(path: string) {
try {
await fs.promises.access(path);
return true;
} catch {
return false;
}
}