import { spawn } from 'node:child_process'; import { resolve } from 'node:path'; import { setTimeout as delay } from 'node:timers/promises'; process.loadEnvFile('.env.local'); if (process.env.NODE_ENV === 'production') throw new Error('dev:all is for local development only'); const root = process.cwd(); const children = []; const inherited = Object.fromEntries(['PATH', 'HOME', 'TMPDIR', 'LANG', 'TERM', 'HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY'].filter(k => process.env[k]).map(k => [k, process.env[k]])); const envFor = patterns => ({ ...inherited, ...Object.fromEntries(Object.entries(process.env).filter(([key]) => patterns.some(pattern => pattern.test(key)))), NODE_ENV: 'development' }); const services = [ { name: 'git', cwd: root, args: ['--import', 'tsx', 'apps/git-service/src/main.ts'], env: envFor([/^WF_GIT_/]), url: 'http://127.0.0.1:4504/health', identity: 'git-storage' }, { name: 'api', cwd: resolve(root, 'apps/api'), args: ['--import', 'tsx', 'src/main.ts'], env: envFor([/^DATABASE_URL$/, /^WF_/, /^S3_/, /^OIDC_/, /^TEMPORAL_/, /^VAULT_/, /^PORT$/, /^HOST$/]), url: 'http://127.0.0.1:4000/health', identity: 'wishforge-control-plane' }, { name: 'web', cwd: resolve(root, 'apps/web'), args: [resolve(root, 'node_modules/next/dist/bin/next'), 'dev', '--hostname', '127.0.0.1', '--port', '3000'], env: inherited, url: 'http://127.0.0.1:3000/api/session' }, ]; let stopping = false; function stop(code = 0) { if (stopping) return; stopping = true; for (const child of children) child.kill('SIGTERM'); process.exitCode = code; } for (const signal of ['SIGINT', 'SIGTERM']) process.once(signal, () => stop()); for (const service of services) { try { const response = await fetch(service.url, { signal: AbortSignal.timeout(1000) }); const body = await response.json(); if (response.ok && (!service.identity || body.service === service.identity)) { console.log(`${service.name}: reusing the existing local service`); continue; } } catch {} const child = spawn(process.execPath, service.args, { cwd: service.cwd, env: service.env, stdio: ['ignore', 'pipe', 'pipe'] }); children.push(child); for (const output of [child.stdout, child.stderr]) output.on('data', bytes => process.stdout.write(`[${service.name}] ${bytes}`)); child.once('exit', code => { if (!stopping) { console.error(`${service.name} exited (${code}); stopping services started by this invocation.`); stop(code || 1); } }); let ready = false; for (let attempt = 0; attempt < 60 && !stopping; attempt++) { try { const response = await fetch(service.url, { signal: AbortSignal.timeout(1000) }); if (response.ok) { ready = true; break; } } catch {} await delay(1000); } if (!ready) { stop(1); throw new Error(`${service.name} did not become ready; inspect its startup output.`); } } console.log('WishForge console: http://localhost:3000 ยท API: http://127.0.0.1:4000'); if (!children.length) console.log('All services were already running; no processes were taken over.');