import { watch } from 'fs'; const SRC_DIR = 'src'; async function spawnServer() { return Bun.spawn(['bun', 'run', 'src/index.tsx'], { stdio: ['inherit', 'inherit', 'inherit'], }); } async function restart(current: ReturnType) { current.kill(); await current.exited; return spawnServer(); } let server = await spawnServer(); // Debounce rapid file-save bursts (e.g. formatter touching many files at once) let debounce: ReturnType | null = null; watch(SRC_DIR, { recursive: true }, (_, filename) => { if (filename?.endsWith('.css')) return; // CSS is handled by build-css.ts --watch if (debounce) clearTimeout(debounce); debounce = setTimeout(async () => { console.clear(); console.log(`[dev] ${filename} changed — restarting…`); server = await restart(server); }, 50); }); const css = Bun.spawn(['bun', 'scripts/build-css.ts', '--watch'], { stdio: ['inherit', 'inherit', 'inherit'], }); process.on('SIGINT', async () => { server.kill(); css.kill(); await Promise.all([server.exited, css.exited]); process.exit(0); }); await Promise.all([server.exited, css.exited]);