import { spawn } from 'node:child_process'; import path from 'node:path'; import { startServer, root } from './server.mjs'; import { stopOwnedChild } from './processes.mjs'; const backend = startServer(); const frontend = spawn(process.execPath, [path.join(root, 'apps/admin/node_modules/vite/bin/vite.js'), '--config', path.join(root, 'apps/admin/vite.config.ts')], { cwd: root, env: process.env, stdio: 'inherit', windowsHide: true, }); let stopping = false; async function stop(code = 0) { if (stopping) return; stopping = true; await Promise.all([stopOwnedChild(backend, { graceful: true }), stopOwnedChild(frontend, { timeout: 3000 })]); process.exitCode = code; } for (const signal of ['SIGINT', 'SIGTERM']) process.on(signal, () => stop()); for (const child of [backend, frontend]) { child.on('error', error => { console.error(error.message); stop(1); }); child.on('exit', code => { if (!stopping) stop(code || 0); }); }