nomadro/apps/taro/scripts/dev.mjs

31 lines
1.3 KiB
JavaScript

import { spawn, spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..');
const cli = process.env.npm_execpath;
if (!cli) throw new Error('Start with pnpm dev:taro');
const python = process.env.TARO_PYTHON || 'python';
const children = [];
let stopping = false;
function stop(code = 0) {
if (stopping) return;
stopping = true;
for (const child of children) {
if (!child.pid) continue;
if (process.platform === 'win32') spawnSync('taskkill', ['/PID', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' });
else child.kill('SIGTERM');
}
process.exit(code);
}
function run(command, args) {
const child = spawn(command, args, { cwd: root, stdio: 'inherit', windowsHide: true });
children.push(child);
child.on('error', error => { console.error(error.message); stop(1); });
child.on('exit', code => { if (!stopping) stop(code ?? 1); });
}
run(python, ['-m', 'uvicorn', 'main:app', '--app-dir', 'apps/taro/api', '--host', '127.0.0.1', '--port', '10089']);
run(process.execPath, [cli, '--filter', '@nomadro/taro-admin', 'dev']);
run(process.execPath, [cli, '--filter', '@nomadro/taro-web', 'dev']);
process.on('SIGINT', () => stop());
process.on('SIGTERM', () => stop());