comp-hub 0.31.1 → 0.31.2
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.js +1 -0
- package/bin/lib/startup.js +33 -13
- package/dist/index.js +2 -2
- package/dist/master.js +1 -1
- package/package.json +1 -2
package/bin/cli.js
CHANGED
|
@@ -15,6 +15,7 @@ program
|
|
|
15
15
|
.name("comphub")
|
|
16
16
|
.option("-p, --port <number>", "Startup port", portArg)
|
|
17
17
|
.option("-d, --dir <path>", "Resource directory relative path")
|
|
18
|
+
.option("-l, --log-level <level>", "Log level: debug, info, warn, error (default: info)", "error")
|
|
18
19
|
.version(pkg.version, "-v, --version", "Show version")
|
|
19
20
|
.addOption(new Option("--allow-debug", "Enable debug mode").hideHelp())
|
|
20
21
|
.addOption(new Option("--api <url>", "API URL").hideHelp())
|
package/bin/lib/startup.js
CHANGED
|
@@ -18,7 +18,8 @@ function mergeConfig(opts, cwd) {
|
|
|
18
18
|
api: opts.api || fileConfig.api,
|
|
19
19
|
allowDebug: opts.allowDebug != null ? opts.allowDebug : fileConfig.allowDebug != null ? fileConfig.allowDebug : false,
|
|
20
20
|
proxy: opts.proxy || fileConfig.proxy,
|
|
21
|
-
port: opts.port
|
|
21
|
+
port: opts.port,
|
|
22
|
+
logLevel: opts.logLevel || fileConfig.logLevel || "info"
|
|
22
23
|
};
|
|
23
24
|
}
|
|
24
25
|
|
|
@@ -33,7 +34,7 @@ async function waitForMasterRuntime() {
|
|
|
33
34
|
return null;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
async function spawnMaster(publicPort, pkg, cwd) {
|
|
37
|
+
async function spawnMaster(publicPort, pkg, cwd, _opts) {
|
|
37
38
|
const masterEntry = path.join(projectRoot, "dist/master.js");
|
|
38
39
|
|
|
39
40
|
if (!fs.existsSync(masterEntry)) {
|
|
@@ -41,10 +42,8 @@ async function spawnMaster(publicPort, pkg, cwd) {
|
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
const masterArgs = ["--port", String(publicPort), "--public-port", String(publicPort)];
|
|
44
|
-
const child = spawn(process.execPath, [masterEntry, ...masterArgs], {
|
|
45
|
-
stdio: "
|
|
46
|
-
detached: true,
|
|
47
|
-
windowsHide: true,
|
|
45
|
+
const child = spawn(process.execPath, ["--no-deprecation", masterEntry, ...masterArgs], {
|
|
46
|
+
stdio: "inherit",
|
|
48
47
|
cwd,
|
|
49
48
|
env: {
|
|
50
49
|
...process.env,
|
|
@@ -52,7 +51,17 @@ async function spawnMaster(publicPort, pkg, cwd) {
|
|
|
52
51
|
}
|
|
53
52
|
});
|
|
54
53
|
|
|
55
|
-
|
|
54
|
+
// 转发父进程信号,确保 Ctrl+C 时 master 也能优雅退出
|
|
55
|
+
["SIGINT", "SIGTERM"].forEach(sig => {
|
|
56
|
+
process.on(sig, () => {
|
|
57
|
+
child.kill(sig);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Master 退出时 CLI 也跟着退出
|
|
62
|
+
child.on("exit", (code) => {
|
|
63
|
+
process.exit(code || 0);
|
|
64
|
+
});
|
|
56
65
|
|
|
57
66
|
const runtime = await waitForMasterRuntime();
|
|
58
67
|
if (!runtime) {
|
|
@@ -60,7 +69,7 @@ async function spawnMaster(publicPort, pkg, cwd) {
|
|
|
60
69
|
throw new Error("Master startup timeout");
|
|
61
70
|
}
|
|
62
71
|
|
|
63
|
-
return runtime;
|
|
72
|
+
return { runtime, child };
|
|
64
73
|
}
|
|
65
74
|
|
|
66
75
|
/**
|
|
@@ -70,6 +79,7 @@ async function spawnMaster(publicPort, pkg, cwd) {
|
|
|
70
79
|
async function ensureMasterRunning(opts, pkg) {
|
|
71
80
|
let portMismatch = false;
|
|
72
81
|
let runningPublicPort = 0;
|
|
82
|
+
let masterChild = null;
|
|
73
83
|
|
|
74
84
|
const existingRuntime = await readMasterRuntime();
|
|
75
85
|
|
|
@@ -80,15 +90,17 @@ async function ensureMasterRunning(opts, pkg) {
|
|
|
80
90
|
portMismatch = true;
|
|
81
91
|
runningPublicPort = existingRuntime.publicPort;
|
|
82
92
|
}
|
|
83
|
-
return { runtime: existingRuntime, portMismatch, runningPublicPort };
|
|
93
|
+
return { runtime: existingRuntime, portMismatch, runningPublicPort, masterChild };
|
|
84
94
|
}
|
|
85
95
|
|
|
86
|
-
// 启动新 master
|
|
96
|
+
// 启动新 master(前台运行,日志输出到终端)
|
|
87
97
|
const portHint = opts.port != null ? opts.port : DEFAULT_PORT;
|
|
88
98
|
const publicPort = await getPort({ port: getPort.makeRange(portHint, portHint + 100) });
|
|
89
99
|
|
|
90
|
-
const runtime = await spawnMaster(publicPort, pkg, process.cwd());
|
|
91
|
-
|
|
100
|
+
const { runtime, child } = await spawnMaster(publicPort, pkg, process.cwd(), opts);
|
|
101
|
+
masterChild = child;
|
|
102
|
+
|
|
103
|
+
return { runtime, portMismatch, runningPublicPort, masterChild };
|
|
92
104
|
}
|
|
93
105
|
|
|
94
106
|
// ---- Registration ----
|
|
@@ -102,6 +114,7 @@ async function registerProject(masterPort, hash, cwd, opts) {
|
|
|
102
114
|
if (opts.api) body.api = opts.api;
|
|
103
115
|
if (opts.allowDebug) body.allowDebug = true;
|
|
104
116
|
if (opts.proxy) body.proxy = opts.proxy;
|
|
117
|
+
if (opts.logLevel) body.logLevel = opts.logLevel;
|
|
105
118
|
|
|
106
119
|
const res = await httpPost(apiUrl(masterPort, ADMIN_API.REGISTER), body);
|
|
107
120
|
if (!res.ok) {
|
|
@@ -170,7 +183,7 @@ async function main(pkg, opts) {
|
|
|
170
183
|
|
|
171
184
|
console.log(`${green("✓")} ${pkg.name} v${pkg.version}`);
|
|
172
185
|
|
|
173
|
-
const { runtime, portMismatch, runningPublicPort } = await ensureMasterRunning(opts, pkg);
|
|
186
|
+
const { runtime, portMismatch, runningPublicPort, masterChild } = await ensureMasterRunning(opts, pkg);
|
|
174
187
|
|
|
175
188
|
const masterPort = runtime.managementPort;
|
|
176
189
|
const displayPort = runtime.publicPort;
|
|
@@ -178,6 +191,13 @@ async function main(pkg, opts) {
|
|
|
178
191
|
await registerProject(masterPort, hash, cwd, opts);
|
|
179
192
|
|
|
180
193
|
printBanner(pkg, cwd, hash, displayPort, opts, { portMismatch, runningPublicPort });
|
|
194
|
+
|
|
195
|
+
// 如果是新启动的 master(非复用已有),CLI 进程保持前台运行以显示服务日志
|
|
196
|
+
// masterChild 已通过 stdio:inherit + 移除 unref() 绑定到当前终端,事件循环不会退出
|
|
197
|
+
// Ctrl+C 时会通过 spawnMaster 中的信号处理优雅关闭
|
|
198
|
+
if (masterChild) {
|
|
199
|
+
console.log(`\n Press ${cyan("Ctrl+C")} to stop\n`);
|
|
200
|
+
}
|
|
181
201
|
}
|
|
182
202
|
|
|
183
203
|
module.exports = { mergeConfig, main };
|