50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { statSync } from "node:fs";
|
|
import { resolve } from "node:path";
|
|
|
|
const root = process.cwd();
|
|
const watchedFiles = [
|
|
resolve(root, "scripts/caserun-native-server.ts"),
|
|
resolve(root, "scripts/caserun-native-fixtures.ts")
|
|
];
|
|
|
|
let child: ReturnType<typeof Bun.spawn> | null = null;
|
|
let signature = sourceSignature();
|
|
let stopping = false;
|
|
|
|
function sourceSignature(): string {
|
|
return watchedFiles.map((path) => `${path}:${statSync(path).mtimeMs}:${statSync(path).size}`).join("|");
|
|
}
|
|
|
|
function startChild(reason: string): void {
|
|
child = Bun.spawn([process.execPath, "scripts/caserun-native-server.ts"], { cwd: root, stdin: "ignore", stdout: "inherit", stderr: "inherit" });
|
|
console.log(JSON.stringify({ event: "caserun-native-reload", mode: "native-test", reason, pid: child.pid }));
|
|
void child.exited.then((exitCode) => {
|
|
if (!stopping && child?.exitCode === exitCode) console.log(JSON.stringify({ event: "caserun-native-exit", exitCode }));
|
|
});
|
|
}
|
|
|
|
function stopChild(): void {
|
|
if (child && child.exitCode === null) child.kill("SIGTERM");
|
|
child = null;
|
|
}
|
|
|
|
startChild("initial");
|
|
|
|
const timer = setInterval(() => {
|
|
const nextSignature = sourceSignature();
|
|
if (nextSignature === signature) return;
|
|
signature = nextSignature;
|
|
stopChild();
|
|
startChild("source-change");
|
|
}, 500);
|
|
|
|
function shutdown(): void {
|
|
stopping = true;
|
|
clearInterval(timer);
|
|
stopChild();
|
|
process.exit(0);
|
|
}
|
|
|
|
process.on("SIGINT", shutdown);
|
|
process.on("SIGTERM", shutdown);
|