oms/tools/src/cli.ts
liufumingren e2d7f04999 feat(tools): 新增 dev 命令,一键启动前后端开发环境
- npm run dev 同时启动 egg-bin dev(6090) 与 vite(5173),日志按 [backend] / [frontend] 前缀分区
- Ctrl+C / SIGTERM 时结束整棵进程树(Windows 用 taskkill /T /F)
- --backend / --frontend 可只启动一端,--no-color 关闭彩色前缀
- 任一端退出时保留另一端继续运行,全部退出后以首个非零退出码返回

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-30 23:10:21 +08:00

91 lines
3.0 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.

#!/usr/bin/env node
import 'dotenv/config';
import { runDeploy } from './commands/deploy';
import { runDev } from './commands/dev';
import { runRollback } from './commands/rollback';
import { initDeployConfig } from './lib/config';
interface Command {
summary: string;
run: (args: string[]) => void;
}
const COMMANDS: Record<string, Command> = {
dev: {
summary: '同时启动前后端开发环境 npm run dev [-- --backend|--frontend|--no-color]',
run: args => runDev(args, { backend: true, frontend: true }),
},
'dev:backend': {
summary: '只启动后端开发环境 npm run dev:backend',
run: args => runDev(args, { backend: true, frontend: false }),
},
'dev:frontend': {
summary: '只启动前端开发环境 npm run dev:frontend',
run: args => runDev(args, { backend: false, frontend: true }),
},
deploy: {
summary: '前后端同时发布 npm run deploy [-- --skip-build|--dry-run|--allow-dirty]',
run: args => runDeploy(args, { backend: true, frontend: true }),
},
'deploy:backend': {
summary: '仅发布后端 npm run deploy:backend [-- 参数同上]',
run: args => runDeploy(args, { backend: true, frontend: false }),
},
'deploy:frontend': {
summary: '仅发布前端 npm run deploy:frontend [-- 参数同上]',
run: args => runDeploy(args, { backend: false, frontend: true }),
},
rollback: {
summary: '前后端一起回滚 npm run rollback [-- --to=v1|v2] [--dry-run]',
run: args => runRollback(args, { backend: true, frontend: true }),
},
'rollback:backend': {
summary: '仅回滚后端 npm run rollback:backend [-- --to=v1|v2]',
run: args => runRollback(args, { backend: true, frontend: false }),
},
'rollback:frontend': {
summary: '仅回滚前端 npm run rollback:frontend',
run: args => runRollback(args, { backend: false, frontend: true }),
},
init: {
summary: '生成发布配置(deploy.config.local.cjs)',
run: () => initDeployConfig(),
},
};
function printUsage(): void {
console.log('OMS 发布工具');
console.log('');
console.log('用法: npm run <script> [-- <参数>] 或 tsx src/cli.ts <命令> [参数]');
console.log('');
console.log('命令:');
for (const [ name, command ] of Object.entries(COMMANDS)) {
console.log(` ${name.padEnd(18)} ${command.summary}`);
}
console.log('');
console.log('发布受限于 git:工作区有未提交改动时默认中止,可用 --allow-dirty 强制发布。');
}
function main(argv: string[]): void {
const [ command, ...rest ] = argv;
if (!command || command === 'help' || command === '--help' || command === '-h') {
printUsage();
return;
}
const entry = COMMANDS[command];
if (!entry) {
throw new Error(`未知命令: ${command}(执行 npm run cli -- help 查看可用命令)`);
}
entry.run(rest);
}
try {
main(process.argv.slice(2));
} catch (err) {
console.error(`❌ ${(err as Error).message}`);
process.exitCode = 1;
}