diff --git a/package.json b/package.json index 1fbffd0a7..2cae1fa1a 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "ui:build": "node scripts/ui.js build", "start": "tsx src/entry.ts", "clawdbot": "tsx src/entry.ts", - "gateway:watch": "tsx watch src/entry.ts gateway --force", + "gateway:watch": "node scripts/gateway-watch.mjs gateway --force", "gateway:dev": "CLAWDBOT_SKIP_CHANNELS=1 tsx src/entry.ts --dev gateway", "gateway:dev:reset": "CLAWDBOT_SKIP_CHANNELS=1 tsx src/entry.ts --dev gateway --reset", "tui": "tsx src/entry.ts tui", diff --git a/scripts/gateway-watch.mjs b/scripts/gateway-watch.mjs new file mode 100644 index 000000000..117daf926 --- /dev/null +++ b/scripts/gateway-watch.mjs @@ -0,0 +1,53 @@ +#!/usr/bin/env node +import { spawn } from "node:child_process"; +import { existsSync } from "node:fs"; +import { setTimeout as delay } from "node:timers/promises"; +import process from "node:process"; + +const args = process.argv.slice(2); +const env = { ...process.env }; +const cwd = process.cwd(); + +const tsc = spawn("pnpm", ["exec", "tsc", "--watch", "--preserveWatchOutput"], { + cwd, + env, + stdio: "inherit", +}); + +let nodeProcess = null; +let exiting = false; + +async function waitForEntry() { + while (!existsSync("dist/entry.js")) { + if (exiting) return; + await delay(200); + } +} + +function startNode() { + nodeProcess = spawn(process.execPath, ["--watch", "dist/entry.js", ...args], { + cwd, + env, + stdio: "inherit", + }); +} + +function cleanup(code = 0) { + if (exiting) return; + exiting = true; + nodeProcess?.kill("SIGTERM"); + tsc.kill("SIGTERM"); + process.exit(code); +} + +process.on("SIGINT", () => cleanup(130)); +process.on("SIGTERM", () => cleanup(143)); +process.on("exit", () => cleanup()); + +tsc.on("exit", (code) => { + if (exiting) return; + cleanup(code ?? 1); +}); + +await waitForEntry(); +if (!exiting) startNode();