Files
clawdbot/scripts/watch-node.mjs
2026-01-31 18:31:49 +09:00

77 lines
1.5 KiB
JavaScript

#!/usr/bin/env node
import { spawn, spawnSync } from "node:child_process";
import process from "node:process";
const args = process.argv.slice(2);
const env = { ...process.env };
const cwd = process.cwd();
const initialBuild = spawnSync("pnpm", ["build"], {
cwd,
env,
stdio: "inherit",
});
if (initialBuild.status !== 0) {
process.exit(initialBuild.status ?? 1);
}
const compilerProcess = spawn("pnpm", ["tsc", '-p', 'tsconfig.json', '--noEmit', 'false', '--watch'], {
cwd,
env,
stdio: "inherit",
});
let nodeProcess = null;
let restartTimer = null;
function spawnNode() {
nodeProcess = spawn(process.execPath, ["--watch", "openclaw.mjs", ...args], {
cwd,
env,
stdio: "inherit",
});
nodeProcess.on("exit", (code, signal) => {
if (signal || exiting) {
return;
}
// If the build is mid-refresh, node can exit on missing modules. Retry.
if (restartTimer) {
clearTimeout(restartTimer);
}
restartTimer = setTimeout(() => {
restartTimer = null;
spawnNode();
}, 250);
});
}
spawnNode();
let exiting = false;
function cleanup(code = 0) {
if (exiting) {
return;
}
exiting = true;
if (restartTimer) {
clearTimeout(restartTimer);
restartTimer = null;
}
nodeProcess?.kill("SIGTERM");
compilerProcess.kill("SIGTERM");
process.exit(code);
}
process.on("SIGINT", () => cleanup(130));
process.on("SIGTERM", () => cleanup(143));
compilerProcess.on("exit", (code) => {
if (exiting) {
return;
}
cleanup(code ?? 1);
});