58 lines
2.1 KiB
JavaScript
58 lines
2.1 KiB
JavaScript
// 同一开发容器中管理 API、共享包与前后台监听进程;退出时一并清理。
|
|
import { spawn } from 'node:child_process';
|
|
|
|
const children = new Set();
|
|
let stopping = false;
|
|
function stop(code = 0) {
|
|
if (stopping) return;
|
|
stopping = true;
|
|
for (const child of children) {
|
|
try { process.kill(-child.pid, 'SIGTERM'); } catch (error) {
|
|
if (error.code !== 'ESRCH') console.error('[dev] 停止子进程失败:', error.message);
|
|
}
|
|
}
|
|
const timer = setTimeout(() => {
|
|
for (const child of children) {
|
|
try { process.kill(-child.pid, 'SIGKILL'); } catch (error) {
|
|
if (error.code !== 'ESRCH') console.error('[dev] 强制停止子进程失败:', error.message);
|
|
}
|
|
}
|
|
process.exit(code);
|
|
}, 5000);
|
|
timer.unref();
|
|
process.exitCode = code;
|
|
}
|
|
process.on('SIGINT', () => stop());
|
|
process.on('SIGTERM', () => stop());
|
|
|
|
function start(command, args, cwd = '/app') {
|
|
const child = spawn(command, args, { cwd, stdio: 'inherit', detached: true, env: process.env });
|
|
children.add(child);
|
|
child.on('exit', () => children.delete(child));
|
|
return child;
|
|
}
|
|
async function run(command, args, cwd) {
|
|
await new Promise((resolve, reject) => {
|
|
const child = start(command, args, cwd);
|
|
child.on('error', reject);
|
|
child.on('exit', code => code === 0 ? resolve() : reject(new Error(`${command} 退出码 ${code}`)));
|
|
});
|
|
}
|
|
try {
|
|
await run('pnpm', ['--filter', '@artveris/shared', 'build']);
|
|
await run('pnpm', ['--filter', '@artveris/api', 'exec', 'prisma', 'generate']);
|
|
// 迁移脚本复用 API 的配置读取实现;启动前全量编译一次。
|
|
await run('pnpm', ['--filter', '@artveris/api', 'build']);
|
|
await run('node', ['/app/migrate.cjs']);
|
|
for (const name of ['dev:shared', 'dev:api', 'dev:admin', 'dev:client']) {
|
|
const child = start('pnpm', [name]);
|
|
child.on('error', error => { console.error(`[dev] ${name}: ${error.message}`); stop(1); });
|
|
child.on('exit', code => {
|
|
if (!stopping) { console.error(`[dev] ${name} 已退出 (${code})`); stop(code || 1); }
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('[dev] 启动失败:', error.message);
|
|
stop(1);
|
|
}
|