comp-hub 0.1.20 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.cjs +77 -0
- package/dist/index.js +1 -1
- package/package.json +6 -4
- package/bin/cli copy.js +0 -15
- package/bin/cli.js +0 -70
package/bin/cli.cjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { program, Option } = require('commander');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { spawn } = require('child_process');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
|
|
7
|
+
const projectRoot = path.resolve(__dirname, '..');
|
|
8
|
+
|
|
9
|
+
const readJson = (p) => JSON.parse(fs.readFileSync(p, 'utf8'));
|
|
10
|
+
|
|
11
|
+
//读取包信息
|
|
12
|
+
function getLocalPkgInfo() {
|
|
13
|
+
const pkgPath = path.join(projectRoot, 'package.json');
|
|
14
|
+
const pkg = readJson(pkgPath);
|
|
15
|
+
const entry = path.join(projectRoot, pkg.main || 'dist/index.js');
|
|
16
|
+
return { pkg, entry };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const { pkg, entry } = getLocalPkgInfo();
|
|
20
|
+
|
|
21
|
+
//命令行
|
|
22
|
+
program
|
|
23
|
+
.option('-p, --port <number>', '启动端口', (v) => parseInt(v, 10), 3000)
|
|
24
|
+
.option('-d, --dir <path>', '资源目录相对路径', './')
|
|
25
|
+
.option('-l, --log <level>', '日志等级', (v) => {
|
|
26
|
+
const ok = ['debug', 'info', 'warn', 'error'];
|
|
27
|
+
if (!ok.includes(v)) throw new Error(`日志等级必须是 ${ok.join(', ')}`);
|
|
28
|
+
return v;
|
|
29
|
+
}, 'info')
|
|
30
|
+
.version(pkg.version, '-v, --version', '显示版本号')
|
|
31
|
+
.addOption(new Option('--allow-debug', '是否允许调试模式').hideHelp())
|
|
32
|
+
.addOption(new Option('--dir-absolute <path>', '资源目录绝对路径').hideHelp())
|
|
33
|
+
.addOption(new Option('--api <url>', 'API地址').hideHelp())
|
|
34
|
+
.addOption(new Option('--cwd-relative <path>', '当前工作目录相对路径').hideHelp())
|
|
35
|
+
.helpOption('-h, --help', '显示帮助信息');
|
|
36
|
+
|
|
37
|
+
//主流程
|
|
38
|
+
function main() {
|
|
39
|
+
program.parse();
|
|
40
|
+
const opts = program.opts();
|
|
41
|
+
|
|
42
|
+
console.log(`✅ ${pkg.name} v${pkg.version}`);
|
|
43
|
+
|
|
44
|
+
//忽略传递的参数配置
|
|
45
|
+
const ignoreArgs = ['-v', '--version', '-h', '--help'];
|
|
46
|
+
const userArgs = process.argv.slice(2).filter((a) => !ignoreArgs.includes(a));
|
|
47
|
+
|
|
48
|
+
const env = {
|
|
49
|
+
...process.env,
|
|
50
|
+
COMP_HUB_VERSION: pkg.version,
|
|
51
|
+
COMP_HUB_CONFIG: JSON.stringify(opts),
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
if (!fs.existsSync(entry)) {
|
|
55
|
+
console.log('\nℹ️ 入口文件不存在,跳过启动');
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const child = spawn(process.execPath, [entry, ...userArgs], {
|
|
60
|
+
stdio: 'inherit',
|
|
61
|
+
cwd: process.cwd(),
|
|
62
|
+
env,
|
|
63
|
+
shell: process.platform === 'win32',
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
child.on('exit', (code) => process.exit(code ?? 0));
|
|
67
|
+
child.on('error', (e) => (console.error(`❌ 启动失败:${e.message}`), process.exit(1)));
|
|
68
|
+
|
|
69
|
+
['SIGINT', 'SIGTERM'].forEach((sig) =>
|
|
70
|
+
process.on(sig, () => {
|
|
71
|
+
console.log('\n⚠️ 终止进程...');
|
|
72
|
+
child.kill(sig);
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
main();
|