import { spawn } from 'node:child_process'; import { createRequire } from 'node:module'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; // Tests use prebuilt API code, without starting persistent compiler/Turbo watchers. const root = fileURLToPath(new URL('../', import.meta.url)); const web = resolve(root, 'apps/web'); const require = createRequire(resolve(web, 'package.json')); const vite = resolve(dirname(require.resolve('vite/package.json')), 'bin/vite.js'); const children = [ spawn(process.execPath, [resolve(root, 'apps/api/dist/main.js')], { stdio: 'inherit' }), spawn(process.execPath, [vite, '--host', '127.0.0.1'], { cwd: web, stdio: 'inherit' }), ]; let stopping = false; function stop(code = 0) { if (stopping) return; stopping = true; for (const child of children) child.kill('SIGTERM'); process.exitCode = code; } process.on('SIGINT', () => stop()); process.on('SIGTERM', () => stop()); for (const child of children) { child.on('error', (error) => { console.error(error.message); stop(1); }); child.on('exit', (code) => { if (!stopping) stop(code ?? 1); }); }