2023-02-17 00:37:00 +00:00
|
|
|
import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
|
|
import { join } from "node:path";
|
2023-02-08 14:46:33 +00:00
|
|
|
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;
|
2023-02-17 17:31:05 +00:00
|
|
|
cache?: string[];
|
2023-02-08 14:46:33 +00:00
|
|
|
}): 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;
|
|
|
|
}
|
2023-02-17 00:37:00 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|