|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import chalk from 'chalk'; |
| 4 | +import { spawn } from 'node:child_process'; |
| 5 | +import http from 'http'; |
| 6 | + |
| 7 | +function colorFor(name) { |
| 8 | + const colors = [chalk.cyan, chalk.magenta, chalk.green, chalk.blue, chalk.yellow, chalk.redBright]; |
| 9 | + let sum = 0; for (let i=0;i<name.length;i++) sum += name.charCodeAt(i); |
| 10 | + return colors[sum % colors.length]; |
| 11 | +} |
| 12 | + |
| 13 | +async function waitForHealth(url, timeoutMs=15000, interval=500) { |
| 14 | + const start = Date.now(); |
| 15 | + return new Promise(resolve => { |
| 16 | + const check = () => { |
| 17 | + const req = http.get(url, res => { |
| 18 | + if (res.statusCode && res.statusCode < 500) { |
| 19 | + resolve(true); req.destroy(); return; |
| 20 | + } |
| 21 | + res.resume(); |
| 22 | + if (Date.now() - start > timeoutMs) return resolve(false); |
| 23 | + setTimeout(check, interval); |
| 24 | + }); |
| 25 | + req.on('error', () => { |
| 26 | + if (Date.now() - start > timeoutMs) return resolve(false); |
| 27 | + setTimeout(check, interval); |
| 28 | + }); |
| 29 | + }; |
| 30 | + check(); |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +export async function runDev({ docker=false } = {}) { |
| 35 | + const cwd = process.cwd(); |
| 36 | + const configPath = path.join(cwd, 'polyglot.json'); |
| 37 | + if (!fs.existsSync(configPath)) { |
| 38 | + console.error(chalk.red('polyglot.json not found. Run inside a generated workspace.')); |
| 39 | + process.exit(1); |
| 40 | + } |
| 41 | + const cfg = JSON.parse(fs.readFileSync(configPath, 'utf-8')); |
| 42 | + const servicesDir = path.join(cwd, 'services'); |
| 43 | + if (!fs.existsSync(servicesDir)) { |
| 44 | + console.error(chalk.red('services/ directory not found.')); |
| 45 | + process.exit(1); |
| 46 | + } |
| 47 | + if (docker) { |
| 48 | + console.log(chalk.cyan('🛳 Starting via docker compose...')); |
| 49 | + const compose = spawn('docker', ['compose', 'up', '--build'], { stdio: 'inherit' }); |
| 50 | + compose.on('exit', code => process.exit(code || 0)); |
| 51 | + return; |
| 52 | + } |
| 53 | + console.log(chalk.cyan('🚀 Starting services locally (best effort)...')); |
| 54 | + const procs = []; |
| 55 | + const healthPromises = []; |
| 56 | + for (const svc of cfg.services) { |
| 57 | + const svcPath = path.join(cwd, svc.path); |
| 58 | + if (!fs.existsSync(svcPath)) continue; |
| 59 | + // Only auto-run node & frontend services (others require language runtime dev tasks) |
| 60 | + if (!['node','frontend'].includes(svc.type)) continue; |
| 61 | + const pkgPath = path.join(svcPath, 'package.json'); |
| 62 | + if (!fs.existsSync(pkgPath)) continue; |
| 63 | + let pkg; |
| 64 | + try { pkg = JSON.parse(fs.readFileSync(pkgPath,'utf-8')); } catch { continue; } |
| 65 | + if (!pkg.scripts || !pkg.scripts.dev) continue; |
| 66 | + const color = colorFor(svc.name); |
| 67 | + const pm = detectPM(cwd); |
| 68 | + const cmd = pm === 'yarn' ? 'yarn' : pm === 'pnpm' ? 'pnpm' : pm === 'bun' ? 'bun' : 'npm'; |
| 69 | + const args = pm === 'yarn' ? ['run','dev'] : ['run','dev']; |
| 70 | + const child = spawn(cmd, args, { cwd: svcPath, env: { ...process.env, PORT: String(svc.port) }, shell: true }); |
| 71 | + procs.push(child); |
| 72 | + child.stdout.on('data', d => process.stdout.write(color(`[${svc.name}] `) + d.toString())); |
| 73 | + child.stderr.on('data', d => process.stderr.write(color(`[${svc.name}] `) + d.toString())); |
| 74 | + child.on('exit', code => { |
| 75 | + process.stdout.write(color(`[${svc.name}] exited with code ${code}\n`)); |
| 76 | + }); |
| 77 | + // health check |
| 78 | + const healthUrl = `http://localhost:${svc.port}/health`; |
| 79 | + const hp = waitForHealth(healthUrl).then(ok => { |
| 80 | + const msg = ok ? chalk.green(`✔ health OK ${svc.name} ${healthUrl}`) : chalk.yellow(`⚠ health timeout ${svc.name} ${healthUrl}`); |
| 81 | + console.log(msg); |
| 82 | + }); |
| 83 | + healthPromises.push(hp); |
| 84 | + } |
| 85 | + if (!procs.length) { |
| 86 | + console.log(chalk.yellow('No auto-runnable Node/Frontend services found. Use --docker to start all via compose.')); |
| 87 | + } |
| 88 | + await Promise.all(healthPromises); |
| 89 | + console.log(chalk.blue('Watching services. Press Ctrl+C to exit.')); |
| 90 | + process.on('SIGINT', () => { procs.forEach(p => p.kill('SIGINT')); process.exit(0); }); |
| 91 | +} |
| 92 | + |
| 93 | +function detectPM(root) { |
| 94 | + if (fs.existsSync(path.join(root,'pnpm-lock.yaml'))) return 'pnpm'; |
| 95 | + if (fs.existsSync(path.join(root,'yarn.lock'))) return 'yarn'; |
| 96 | + if (fs.existsSync(path.join(root,'bun.lockb'))) return 'bun'; |
| 97 | + return 'npm'; |
| 98 | +} |
0 commit comments