/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ // One command to develop the desktop app from source. It: // 1. builds the CLI, so a linked `dapi` (see `npm run link` in apps/cli) runs the // latest code and the app's headless server matches it; // 2. starts the web dev server (Vite on :5173), first reclaiming the port // from a Vite left behind by an earlier run that did not come down; // 3. waits for that server, then launches Electron, which loads it. // Ctrl-C tears the whole tree down. import { spawn, execFileSync } from "node:child_process"; import { get } from "node:http"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { npm } from "./lib/npm.mjs"; const ROOT = fileURLToPath(new URL("..", import.meta.url)); const BIN = join(ROOT, "node_modules", ".bin"); const DEV_PORT = 5173; const DEV_URL = `http://localhost:${DEV_PORT}`; const WINDOWS = process.platform === "win32"; const children = []; let shuttingDown = false; function run(name, bin, args, cwd) { // Spawn the tool binary directly rather than via `npm run`, so the teardown // SIGTERM isn't dressed up as a "Lifecycle script failed" error by an npm // wrapper. Own process group (detached) so we can signal the tool *and* its // children (esbuild, electron) in one shot on teardown. Windows has neither // groups nor spawnable `.cmd` shims: the shim runs through a shell, stays in // this console, and `taskkill /T` takes the tree down instead. const child = WINDOWS ? spawn(`"${join(BIN, `${bin}.cmd`)}"`, args, { cwd, stdio: "inherit", shell: true }) : spawn(join(BIN, bin), args, { cwd, stdio: "inherit", detached: true }); child.on("exit", (code) => { if (shuttingDown) return; // A child dying on its own (e.g. Vite crashed) should bring the rest down. console.error(`\n[dev:desktop] ${name} exited (${code}); shutting down.`); shutdown(code ?? 1); }); children.push(child); return child; } /** Stops a process and everything under it. */ function killTree(pid, signal = "SIGTERM") { try { if (WINDOWS) execFileSync("taskkill", ["/pid", String(pid), "/T", "/F"], { stdio: "ignore" }); else process.kill(pid, signal); } catch { // Already gone. } } function shutdown(code) { if (shuttingDown) return; shuttingDown = true; // A negative pid signals the child's whole process group. for (const child of children) killTree(WINDOWS ? child.pid : -child.pid); process.exit(code); } // Resolves once the dev server answers. Probes over HTTP against the same URL // Electron loads, so we follow its host resolution (Vite binds localhost as // IPv6 ::1) rather than guessing an address family. function waitForServer(url, timeoutMs = 30000) { const deadline = Date.now() + timeoutMs; return new Promise((resolve, reject) => { const tryOnce = () => { const req = get(url, (res) => { res.destroy(); resolve(); // Any response means the server is up. }); req.once("error", () => { req.destroy(); if (Date.now() > deadline) { reject(new Error(`Vite did not come up at ${url} in time`)); } else { setTimeout(tryOnce, 200); } }); }; tryOnce(); }); } for (const signal of ["SIGINT", "SIGTERM"]) { process.on(signal, () => shutdown(0)); } /** PIDs listening on a TCP port (lsof, or netstat on Windows); [] when none or unknown. */ function listeners(port) { if (WINDOWS) { try { // ` TCP [::1]:5173 [::]:0 LISTENING 1234` const out = execFileSync("netstat", ["-ano", "-p", "TCP"], { stdio: ["ignore", "pipe", "ignore"] }).toString(); const v6 = execFileSync("netstat", ["-ano", "-p", "TCPv6"], { stdio: ["ignore", "pipe", "ignore"] }).toString(); const pids = `${out}\n${v6}` .split("\n") .map((line) => line.trim().split(/\s+/)) .filter((cols) => cols[0] === "TCP" && cols[3] === "LISTENING" && cols[1]?.endsWith(`:${port}`)) .map((cols) => Number(cols[4])) .filter(Boolean); return [...new Set(pids)]; } catch { return []; } } try { const out = execFileSync("lsof", ["-nP", "-t", `-iTCP:${port}`, "-sTCP:LISTEN"], { stdio: ["ignore", "pipe", "ignore"] }); return out.toString().split("\n").map((line) => Number(line.trim())).filter(Boolean); } catch { return []; // lsof exits 1 when nothing listens. } } /** The command line of a process, or "" when it is gone. */ function commandOf(pid) { if (WINDOWS) { try { const query = `(Get-CimInstance Win32_Process -Filter "ProcessId=${Number(pid)}").CommandLine`; return execFileSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", query], { stdio: ["ignore", "pipe", "ignore"] }).toString().trim(); } catch { return ""; } } try { return execFileSync("ps", ["-o", "command=", "-p", String(pid)], { stdio: ["ignore", "pipe", "ignore"] }).toString().trim(); } catch { return ""; } } /** Whether a command line is a Vite started from this checkout. Windows paths compare without case. */ function isOurVite(command) { const root = ROOT.replace(/[\\/]$/, ""); const [haystack, needle] = WINDOWS ? [command.toLowerCase(), root.toLowerCase()] : [command, root]; return haystack.includes("vite") && haystack.includes(needle); } /** * Frees the dev port. A Vite left over from an earlier run of this repo * (Ctrl-C'd terminal, crashed Electron, a detached child) is killed and the * port awaited; anything else on the port is not ours to touch, so we say * what it is and stop. */ async function reclaimPort(port) { const pids = listeners(port); if (!pids.length) return; for (const pid of pids) { const command = commandOf(pid); if (!isOurVite(command)) { console.error(`[dev:desktop] port ${port} is in use by another process (pid ${pid}): ${command || "unknown"}`); process.exit(1); } console.log(`[dev:desktop] port ${port} held by a stale vite (pid ${pid}); stopping it…`); killTree(pid); } const deadline = Date.now() + 5000; while (listeners(port).length) { if (Date.now() > deadline) { for (const pid of listeners(port)) killTree(pid, "SIGKILL"); await new Promise((r) => setTimeout(r, 200)); break; } await new Promise((r) => setTimeout(r, 100)); } } // 1. Build the CLI (blocking) so `dapi` and the app agree on the latest code. console.log("[dev:desktop] building CLI…"); npm(["run", "build", "--workspace=@diffusionstudio/cli"]); // 2. Start the web dev server, on a port that is free. await reclaimPort(DEV_PORT); console.log("[dev:desktop] starting web dev server…"); run("web", "vite", [], join(ROOT, "apps", "web")); // 3. Once it is up, build the desktop app (blocking, mirrors its `dev` // script) and launch Electron, which loads :5173. try { await waitForServer(DEV_URL); } catch (err) { console.error(`[dev:desktop] ${err.message}`); shutdown(1); } console.log("[dev:desktop] building desktop app…"); npm(["run", "build", "--workspace=@diffusionstudio/desktop"]); console.log("[dev:desktop] starting desktop app…"); run("desktop", "electron-forge", ["start"], join(ROOT, "apps", "desktop"));