Super basic example:
import fs from "node:fs";
import { spawn, type ChildProcess } from "node:child_process";
let child: ChildProcess | undefined;
let timer: NodeJS.Timeout;
function restart() {
child?.kill();
child = spawn("npx", ["tsx", "./src/index.ts"], {
stdio: "inherit",
shell: true,
});
}
fs.watch("./src", { recursive: true }, (_eventType, filename) => {
if (!filename) return;
clearTimeout(timer);
timer = setTimeout(restart, 100);
});
restart();