diff --git a/scripts/watch-node.mjs b/scripts/watch-node.mjs index 70f54c1a67..78947e2036 100644 --- a/scripts/watch-node.mjs +++ b/scripts/watch-node.mjs @@ -22,11 +22,32 @@ const compilerProcess = spawn("pnpm", ["tsdown", '--watch', 'src/'], { stdio: "inherit", }); -const nodeProcess = spawn(process.execPath, ["--watch", "openclaw.mjs", ...args], { - 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; @@ -35,7 +56,11 @@ function cleanup(code = 0) { return; } exiting = true; - nodeProcess.kill("SIGTERM"); + if (restartTimer) { + clearTimeout(restartTimer); + restartTimer = null; + } + nodeProcess?.kill("SIGTERM"); compilerProcess.kill("SIGTERM"); process.exit(code); } @@ -49,10 +74,3 @@ compilerProcess.on("exit", (code) => { } cleanup(code ?? 1); }); - -nodeProcess.on("exit", (code, signal) => { - if (signal || exiting) { - return; - } - cleanup(code ?? 1); -});