The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off globalConsole:off globalDate:off globalTimers:off - Standalone Node watch launcher. |
| 2 | import * as NodeChildProcess from "node:child_process"; |
| 3 | import * as NodeFS from "node:fs"; |
| 4 | import * as NodeOS from "node:os"; |
| 5 | import * as NodePath from "node:path"; |
| 6 | |
| 7 | const serverDir = NodePath.resolve(import.meta.dirname, ".."); |
| 8 | const entry = "src/bin.ts"; |
| 9 | const packageDirs = [ |
| 10 | serverDir, |
| 11 | ...NodeFS.globSync("../../packages/*/package.json", { cwd: serverDir }).map((file) => |
| 12 | NodePath.resolve(serverDir, NodePath.dirname(file)), |
| 13 | ), |
| 14 | ]; |
| 15 | |
| 16 | let child: NodeChildProcess.ChildProcess | undefined; |
| 17 | let startedAt = 0; |
| 18 | let restartPending = false; |
| 19 | let stopping = false; |
| 20 | let debounce: NodeJS.Timeout | undefined; |
| 21 | const changedPaths = new Set<string>(); |
| 22 | |
| 23 | // `node --watch` cannot be used: on Windows, libuv reports NTFS last-access updates as changes, |
| 24 | // so merely reading a source file (or an installed dependency directory) restarted the server. |
| 25 | // A path counts as changed only when it is gone or written since the running server started. |
| 26 | const isWrittenSinceStart = (path: string) => { |
| 27 | try { |
| 28 | const stats = NodeFS.statSync(path); |
| 29 | return !stats.isDirectory() && Math.max(stats.mtimeMs, stats.ctimeMs) >= startedAt; |
| 30 | } catch { |
| 31 | return true; |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | const start = () => { |
| 36 | startedAt = Date.now(); |
| 37 | restartPending = false; |
| 38 | child = NodeChildProcess.spawn(process.execPath, [entry, ...process.argv.slice(2)], { |
| 39 | cwd: serverDir, |
| 40 | stdio: "inherit", |
| 41 | }); |
| 42 | child.on("error", (error) => console.error(error)); |
| 43 | child.on("exit", (code, signal) => { |
| 44 | child = undefined; |
| 45 | if (stopping) { |
| 46 | process.exitCode = code ?? (signal === null ? 1 : 128 + NodeOS.constants.signals[signal]); |
| 47 | return; |
| 48 | } |
| 49 | if (restartPending) return start(); |
| 50 | console.log( |
| 51 | `[dev] '${entry}' exited (${signal ?? code}). Waiting for file changes before restarting...`, |
| 52 | ); |
| 53 | }); |
| 54 | }; |
| 55 | |
| 56 | const flush = () => { |
| 57 | debounce = undefined; |
| 58 | const changed = [...changedPaths].find(isWrittenSinceStart); |
| 59 | changedPaths.clear(); |
| 60 | if (changed === undefined || stopping || restartPending) return; |
| 61 | console.log(`[dev] Restarting '${entry}': ${NodePath.relative(serverDir, changed)} changed`); |
| 62 | if (!child) return start(); |
| 63 | restartPending = true; |
| 64 | child.kill("SIGTERM"); |
| 65 | }; |
| 66 | |
| 67 | const onChange = (path: string) => { |
| 68 | changedPaths.add(path); |
| 69 | debounce ??= setTimeout(flush, 200); |
| 70 | }; |
| 71 | |
| 72 | // Watch only each workspace package's src and package.json, never its node_modules. |
| 73 | const watchers = packageDirs.flatMap((dir) => { |
| 74 | const src = NodePath.join(dir, "src"); |
| 75 | return [ |
| 76 | NodeFS.watch(dir, (_event, name) => { |
| 77 | if (name === "package.json") onChange(NodePath.join(dir, name)); |
| 78 | }), |
| 79 | ...(NodeFS.existsSync(src) |
| 80 | ? [ |
| 81 | NodeFS.watch(src, { recursive: true }, (_event, name) => |
| 82 | onChange(name ? NodePath.join(src, name) : src), |
| 83 | ), |
| 84 | ] |
| 85 | : []), |
| 86 | ]; |
| 87 | }); |
| 88 | |
| 89 | const stop = (signal: NodeJS.Signals) => { |
| 90 | stopping = true; |
| 91 | clearTimeout(debounce); |
| 92 | for (const watcher of watchers) watcher.close(); |
| 93 | if (child) child.kill(signal); |
| 94 | }; |
| 95 | process.on("SIGINT", () => stop("SIGINT")); |
| 96 | process.on("SIGTERM", () => stop("SIGTERM")); |
| 97 | process.on("SIGHUP", () => stop("SIGTERM")); |
| 98 | |
| 99 | start(); |