// 同时启动后端(node --watch)与前端(vite),Ctrl+C 一起退出 import { spawn } from 'node:child_process'; const procs = [ ['server', 'npm run dev:server', '\x1b[36m'], ['web', 'npm run dev:web', '\x1b[35m'], ].map(([name, cmd, color]) => { const child = spawn(cmd, { shell: true, stdio: ['ignore', 'pipe', 'pipe'] }); const prefix = `${color}[${name}]\x1b[0m `; const relay = (stream, out) => stream.on('data', (buf) => { for (const line of buf.toString().split(/\r?\n/)) if (line) out.write(prefix + line + '\n'); }); relay(child.stdout, process.stdout); relay(child.stderr, process.stderr); return child; }); const stop = () => { for (const p of procs) p.kill(); process.exit(0); }; process.on('SIGINT', stop); process.on('SIGTERM', stop);