define-alpine-the-sequel/helpers/repro-run.ts

51 lines
1.3 KiB
TypeScript

import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { execFile } from "./better-api.js";
export async function reproRun(opts: {
// cwd stores the code available inside the container as /src, and also
// cache/ and rootfs/
cwd: string;
command: string;
cache?: string[];
}): Promise<void> {
const run = execFile("repro-run", { cwd: opts.cwd });
if (!run.child.stdin) throw false;
run.child.stdin.write(
JSON.stringify({
Command: opts.command,
Cache: opts.cache,
})
);
run.child.stdin.end();
run.child.stdout?.pipe(process.stdout);
run.child.stderr?.pipe(process.stderr);
await run;
}
export async function buildRepro<T>(
cacheName: string,
cacheKey: string,
buildScript: string,
run: (cwd: string) => Promise<void>,
getArtifacts: (cwd: string) => T
): Promise<T> {
const dir = join("cache", cacheName);
await mkdir(dir, { recursive: true });
const cacheKeyFile = join(dir, "version");
const output = getArtifacts(dir);
try {
if ((await readFile(cacheKeyFile, "utf-8")) === cacheKey) return output;
} catch {}
{
const buildScriptPath = join(dir, "build");
await writeFile(buildScriptPath, buildScript);
await chmod(buildScriptPath, 0o700);
}
await run(dir);
await writeFile(cacheKeyFile, cacheKey);
return output;
}