-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.js
More file actions
38 lines (31 loc) · 1.13 KB
/
cli.js
File metadata and controls
38 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env node // 让系统识别为 Node.js 脚本
import { spawn } from 'child_process';// 使用 spawn 代替 exec
let expressProcess; // 存储 Express 服务器进程的引用
// 解析 CLI 参数
const args = process.argv.slice(2); // 获取传递的命令行参数
console.log(`Received CLI arguments: ${args}`);
// 启动 Express 服务器
function startExpress() {
console.log('Starting Express server...');
expressProcess = spawn('npm', ['run', 'express'], { stdio: 'inherit', shell: true });
expressProcess.on('close', (code) => {
console.log(`Express server exited with code ${code}`);
});
}
// 停止 Express 服务器
function stopExpress() {
if (expressProcess) {
console.log('Stopping Express server...');
expressProcess.kill('SIGTERM'); // 发送 SIGTERM 信号停止进程
} else {
console.log('No Express server is running.');
}
}
// 主逻辑:根据 CLI 参数执行不同操作
if (args.includes('--start')) {
startExpress();
} else if (args.includes('--stop')) {
stopExpress();
} else {
console.log('No valid command provided. Use --start to start the server and --stop to stop it.');
}