cache-action
Version:
Save and restore files as a cache in GitHub Actions
55 lines (54 loc) • 1.87 kB
JavaScript
import { spawn } from "node:child_process";
export class ProcessError extends Error {
constructor(args, code, output) {
let message = "Process failed";
if (code !== null)
message += ` (${code.toString()})`;
message += `: ${args.join(" ")}`;
const trimmedOutput = output.trim();
if (trimmedOutput !== "")
message += `\n${trimmedOutput}`;
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}
export async function waitProcess(proc) {
return new Promise((resolve, reject) => {
const chunks = [];
proc.stderr.on("data", (chunk) => chunks.push(chunk));
proc.on("error", reject);
proc.on("close", (code) => {
if (code === 0) {
resolve(undefined);
}
else {
const output = Buffer.concat(chunks).toString();
reject(new ProcessError(proc.spawnargs, code, output));
}
});
});
}
export async function createArchive(archivePath, filePaths) {
const tar = spawn("tar", ["-cf", "-", "-P", ...filePaths]);
const zstd = spawn("zstd", ["-T0", "-o", archivePath]);
tar.stdout.pipe(zstd.stdin);
await Promise.all([waitProcess(tar), waitProcess(zstd)]);
}
export async function extractArchive(archivePath) {
const zstd = spawn("zstd", ["-d", "-T0", "-c", archivePath]);
const tar = spawn("tar", ["-xf", "-", "-P"]);
zstd.stdout.pipe(tar.stdin);
await Promise.all([waitProcess(zstd), waitProcess(tar)]);
}
export async function azureStorageCopy(source, destination) {
const azcopy = spawn("azcopy", [
"copy",
"--skip-version-check",
"--block-size-mb",
"32",
source,
destination,
]);
await waitProcess(azcopy);
}