|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import { watch } from "node:fs"; |
| 3 | +import { stat } from "node:fs/promises"; |
| 4 | +import path from "node:path"; |
| 5 | +import { setTimeout as sleep } from "node:timers/promises"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | + |
| 8 | +const desktopDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 9 | +const webDir = path.resolve(desktopDir, "../web"); |
| 10 | +const serverDir = path.resolve(desktopDir, "../server"); |
| 11 | +const outDir = path.join(desktopDir, "out"); |
| 12 | +const serverOutDir = path.join(serverDir, "dist"); |
| 13 | +const WEB_URL = "http://127.0.0.1:5173"; |
| 14 | +const startedAt = Date.now(); |
| 15 | + |
| 16 | +const children = new Set(); |
| 17 | + |
| 18 | +// Each child leads its own process group so shutdown also reaches the |
| 19 | +// grandchildren (pnpm -> vp, start-electron -> Electron). |
| 20 | +function run(command, args, options) { |
| 21 | + const child = spawn(command, args, { stdio: "inherit", detached: true, ...options }); |
| 22 | + children.add(child); |
| 23 | + child.on("exit", () => children.delete(child)); |
| 24 | + return child; |
| 25 | +} |
| 26 | + |
| 27 | +function stop(child) { |
| 28 | + try { |
| 29 | + process.kill(-child.pid, "SIGTERM"); |
| 30 | + } catch {} |
| 31 | +} |
| 32 | + |
| 33 | +function shutdown() { |
| 34 | + for (const child of children) stop(child); |
| 35 | + process.exit(0); |
| 36 | +} |
| 37 | +process.on("SIGINT", shutdown); |
| 38 | +process.on("SIGTERM", shutdown); |
| 39 | + |
| 40 | +// Electron is useless without any of them, so losing one ends the session (this |
| 41 | +// also catches Vite refusing an occupied port). |
| 42 | +for (const [args, cwd] of [ |
| 43 | + [["run", "dev"], webDir], |
| 44 | + [["exec", "vp", "pack", "--watch"], desktopDir], |
| 45 | + [["exec", "vp", "pack", "--watch"], serverDir], |
| 46 | +]) { |
| 47 | + run("pnpm", args, { cwd }).on("exit", shutdown); |
| 48 | +} |
| 49 | + |
| 50 | +// Wait for fresh bundles and a reachable web server, so Electron |
| 51 | +// does not boot on a stale bundle and immediately restart. |
| 52 | +const bundles = [path.join(outDir, "main.cjs"), path.join(outDir, "preload.cjs"), path.join(serverOutDir, "index.mjs")]; |
| 53 | + |
| 54 | +async function ready() { |
| 55 | + for (const file of bundles) { |
| 56 | + const built = await stat(file).catch(() => null); |
| 57 | + if (built === null || built.mtimeMs < startedAt) return false; |
| 58 | + } |
| 59 | + return fetch(WEB_URL).then( |
| 60 | + () => true, |
| 61 | + () => false, |
| 62 | + ); |
| 63 | +} |
| 64 | +while (!(await ready())) await sleep(100); |
| 65 | + |
| 66 | +let electron; |
| 67 | +function startElectron() { |
| 68 | + electron = run("node", ["scripts/start-electron.mjs"], { |
| 69 | + cwd: desktopDir, |
| 70 | + env: { ...process.env, CODEWORK_WEB_URL: WEB_URL }, |
| 71 | + }); |
| 72 | + // Quitting the app window ends the dev session; restarts are not quits. |
| 73 | + const current = electron; |
| 74 | + current.on("exit", () => { |
| 75 | + if (electron === current) shutdown(); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +// Main supervises the app server, so restarting Electron also restarts it. |
| 80 | +let restartTimer; |
| 81 | +const onRebuild = (dir) => (_event, file) => { |
| 82 | + if (!bundles.includes(path.join(dir, String(file)))) return; |
| 83 | + clearTimeout(restartTimer); |
| 84 | + restartTimer = setTimeout(() => { |
| 85 | + const previous = electron; |
| 86 | + if (previous === undefined) return; |
| 87 | + electron = undefined; |
| 88 | + stop(previous); |
| 89 | + previous.once("exit", startElectron); |
| 90 | + }, 100); |
| 91 | +}; |
| 92 | +watch(outDir, onRebuild(outDir)); |
| 93 | +watch(serverOutDir, onRebuild(serverOutDir)); |
| 94 | + |
| 95 | +startElectron(); |
0 commit comments