65 lines
3.0 KiB
TypeScript
65 lines
3.0 KiB
TypeScript
import { promises as fs, renameSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { configureWorker } from "./lib/worker-runtime";
|
|
import { readJobJson, writeJobJson } from "./lib/project-jobs";
|
|
import type { AnalysisInput, Decision, JobPhase, ProjectJobState } from "./lib/project-jobs";
|
|
import { applyProject, prepareProject } from "./lib/project-work";
|
|
import type { Preview } from "./lib/project-work";
|
|
|
|
async function main() {
|
|
const [, , directory, phaseArg] = process.argv;
|
|
if (!directory || (phaseArg !== "analysis" && phaseArg !== "apply")) throw new Error("Ungültiger Projektauftrag.");
|
|
const phase: JobPhase = phaseArg;
|
|
try {
|
|
let configuration = "";
|
|
process.stdin.setEncoding("utf8");
|
|
for await (const chunk of process.stdin) configuration += chunk;
|
|
const config = JSON.parse(configuration) as Parameters<typeof configureWorker>[0];
|
|
configureWorker(config);
|
|
configuration = "";
|
|
|
|
// Progress is small and independent of the persisted review. Limit disk
|
|
// writes when an unchanged project has many files.
|
|
let lastUpdate = 0;
|
|
const progress = (message: string) => {
|
|
if (Date.now() - lastUpdate < 300) return;
|
|
lastUpdate = Date.now();
|
|
const partial = join(directory, "status.tmp");
|
|
const status: ProjectJobState = { phase, pid: process.pid, message };
|
|
writeFileSync(partial, JSON.stringify(status), { mode: 0o600 });
|
|
renameSync(partial, join(directory, "state.json"));
|
|
};
|
|
const input = await readJobJson<AnalysisInput>(directory, "input.json");
|
|
if (phase === "analysis") {
|
|
const preview = await prepareProject(config.assets, input.root, input.files, input.rules,
|
|
input.whitelist, input.entityTypes, input.skipped, input.reanalyzeUnchanged, progress, input.imagePolicy);
|
|
if (!preview.drafts.length && !preview.hits.length && !input.fileSelectionChanged) {
|
|
await finish(directory, { phase: "complete", reused: preview.retained.length });
|
|
} else {
|
|
await writeJobJson(directory, "preview.json", preview);
|
|
await writeJobJson(directory, "state.json", { phase: "review" });
|
|
}
|
|
return;
|
|
}
|
|
|
|
const [preview, decision] = await Promise.all([
|
|
readJobJson<Preview>(directory, "preview.json"), readJobJson<Decision>(directory, "decision.json"),
|
|
]);
|
|
const output = await applyProject(config.assets, input.root, preview, input.rules, input.whitelist, decision, progress);
|
|
await finish(directory, { phase: "complete", output });
|
|
} catch (error) {
|
|
await writeJobJson(directory, "state.json", {
|
|
phase: "error", message: error instanceof Error ? error.message : "Lokaler Projektauftrag fehlgeschlagen.",
|
|
});
|
|
process.exitCode = 1;
|
|
}
|
|
}
|
|
|
|
async function finish(directory: string, state: ProjectJobState) {
|
|
await Promise.all(["input.json", "preview.json", "decision.json"].map((file) =>
|
|
fs.rm(join(directory, file), { force: true }).catch(() => undefined)));
|
|
await writeJobJson(directory, "state.json", state);
|
|
}
|
|
|
|
void main().catch(() => { process.exitCode = 1; });
|