Files
SubTracker/scripts/dev.mjs
2026-04-11 13:36:43 +08:00

79 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { spawn } from 'node:child_process'
const npmExecPath = process.env.npm_execpath
if (!npmExecPath) {
console.error('未找到 npm_execpath无法启动 workspace dev 脚本。')
process.exit(1)
}
const children = []
let shuttingDown = false
function startWorkspace(name, color, args) {
const child = spawn(process.execPath, [npmExecPath, ...args], {
cwd: process.cwd(),
stdio: ['inherit', 'pipe', 'pipe'],
env: {
...process.env,
FORCE_COLOR: '1'
}
})
child.stdout?.on('data', (chunk) => {
process.stdout.write(`${color}[${name}]\x1b[0m ${chunk}`)
})
child.stderr?.on('data', (chunk) => {
process.stderr.write(`${color}[${name}]\x1b[0m ${chunk}`)
})
child.on('exit', (code, signal) => {
if (!shuttingDown) {
if (signal) {
console.log(`[${name}] 已退出signal=${signal}`)
} else {
console.log(`[${name}] 已退出code=${code}`)
}
shutdown(code ?? 0)
}
})
children.push(child)
}
function shutdown(exitCode = 0) {
if (shuttingDown) return
shuttingDown = true
for (const child of children) {
if (!child.killed) {
child.kill('SIGINT')
}
}
setTimeout(() => {
for (const child of children) {
if (!child.killed) {
child.kill('SIGTERM')
}
}
}, 1200)
setTimeout(() => {
process.exit(exitCode)
}, 1800)
}
process.on('SIGINT', () => {
console.log('\n正在关闭开发服务...')
shutdown(0)
})
process.on('SIGTERM', () => {
shutdown(0)
})
startWorkspace('api', '\x1b[36m', ['run', 'dev', '-w', 'apps/api'])
startWorkspace('web', '\x1b[35m', ['run', 'dev', '-w', 'apps/web'])