// `bun run dev` runs this: Electron with --dev, restarted whenever a file in electron/ changes. // (Vite hot-reloads the page by itself, but the main process only loads its code once.) import { spawn } from 'node:child_process'; import { watch } from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); const electron = path.join(root, 'node_modules', '.bin', 'electron'); let child = null; let restarting = false; let timer = null; function start() { child = spawn(electron, ['.', '--dev', ...process.argv.slice(2)], { cwd: root, stdio: 'inherit' }); child.on('exit', (code) => { if (restarting) { restarting = false; start(); return; } process.exit(code ?? 0); // the window was closed: stop dev mode, as before }); } watch(path.join(root, 'electron'), { recursive: true }, (_event, file) => { if (!file || !/\.(c?js|json)$/.test(file)) return; clearTimeout(timer); timer = setTimeout(() => { console.log(`[app] ${file} changed, restarting Electron`); restarting = true; child.kill('SIGTERM'); }, 250); }); for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => { restarting = false; child?.kill(sig); }); start();