57 lines
1.9 KiB
JavaScript
57 lines
1.9 KiB
JavaScript
|
|
#!/usr/bin/env node
|
|||
|
|
/**
|
|||
|
|
* 一键启动开发环境(任务 A1.5):平台服务 + 客户端 dev server。
|
|||
|
|
*
|
|||
|
|
* 平台服务:node server/dist/bin/serve.js(先跑一次 tsc -b)
|
|||
|
|
* 客户端:vite dev server,/v1 由 vite 代理到平台服务,避免跨端口与 CORS
|
|||
|
|
*
|
|||
|
|
* Ctrl+C 一起退出。生产形态走 `pnpm start`(构建后由平台服务单端口托管)。
|
|||
|
|
*/
|
|||
|
|
import { spawn } from 'node:child_process';
|
|||
|
|
import { dirname } from 'node:path';
|
|||
|
|
import { fileURLToPath } from 'node:url';
|
|||
|
|
|
|||
|
|
const root = dirname(dirname(fileURLToPath(import.meta.url)));
|
|||
|
|
const isWindows = process.platform === 'win32';
|
|||
|
|
const children = [];
|
|||
|
|
let shuttingDown = false;
|
|||
|
|
|
|||
|
|
function run(name, command, args) {
|
|||
|
|
const child = spawn(command, args, { cwd: root, stdio: ['ignore', 'pipe', 'pipe'], shell: isWindows });
|
|||
|
|
children.push(child);
|
|||
|
|
const pipe = (stream) =>
|
|||
|
|
stream.on('data', (chunk) => {
|
|||
|
|
for (const line of String(chunk).split(/\r?\n/)) if (line.trim()) process.stdout.write(`[${name}] ${line}\n`);
|
|||
|
|
});
|
|||
|
|
pipe(child.stdout);
|
|||
|
|
pipe(child.stderr);
|
|||
|
|
child.on('exit', (code) => {
|
|||
|
|
if (shuttingDown) return;
|
|||
|
|
console.error(`[${name}] 退出,code=${code}`);
|
|||
|
|
shutdown(1);
|
|||
|
|
});
|
|||
|
|
return child;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function shutdown(code = 0) {
|
|||
|
|
if (shuttingDown) return;
|
|||
|
|
shuttingDown = true;
|
|||
|
|
for (const child of children) child.kill();
|
|||
|
|
process.exit(code);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
process.on('SIGINT', () => shutdown(0));
|
|||
|
|
process.on('SIGTERM', () => shutdown(0));
|
|||
|
|
|
|||
|
|
console.log('构建服务端…');
|
|||
|
|
const build = spawn('pnpm', ['exec', 'tsc', '-b'], { cwd: root, stdio: 'inherit', shell: isWindows });
|
|||
|
|
build.on('exit', (code) => {
|
|||
|
|
if (code !== 0) {
|
|||
|
|
console.error('服务端构建失败,已中止');
|
|||
|
|
process.exit(code ?? 1);
|
|||
|
|
}
|
|||
|
|
console.log('平台服务:http://127.0.0.1:5199 客户端:http://127.0.0.1:5173');
|
|||
|
|
console.log('Ctrl+C 退出');
|
|||
|
|
run('server', 'node', ['server/dist/bin/serve.js']);
|
|||
|
|
run('client', 'pnpm', ['--filter', '@aps/client-app', 'run', 'dev']);
|
|||
|
|
});
|