@napuketto/cli 0.8.8 → 0.8.10
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/dist/index.mjs +101 -217
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import process$1 from "node:process";
|
|
3
|
-
import { CONFIG_FILE_MODE, ConfigBase, ConfigError, EXIT_CONFIG_INVALID, configError, createLogger, errorMessage, hasStringProp, isKernelError, isRecord, kernelError, parseLogConfig, parseToml, resolveConfigPath, resolveDataRoot, resolveSandboxWritePath, stringifyToml } from "@napuketto/kernel";
|
|
3
|
+
import { CONFIG_FILE_MODE, ConfigBase, ConfigError, EXIT_CONFIG_INVALID, configError, createLogger, errorMessage, getErrorCode, hasOptionalStringProp, hasStringProp, isKernelError, isRecord, kernelError, parseLogConfig, parseToml, resolveConfigPath, resolveDataRoot, resolveSandboxWritePath, stringifyToml } from "@napuketto/kernel";
|
|
4
4
|
import { Command } from "commander";
|
|
5
5
|
import { mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
6
6
|
import { readFile, writeFile } from "node:fs/promises";
|
|
@@ -13,22 +13,15 @@ import { StringDecoder } from "node:string_decoder";
|
|
|
13
13
|
import { encode } from "uqr";
|
|
14
14
|
// src/config/schema.ts
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* 2026-08-08 结构拍板:**一个 QQ 账号一个 [[accounts]] 段,协议与通信配置嵌在
|
|
20
|
-
* 账号内**([accounts.onebot11] / [accounts.satori]);账号必填(至少一个)。
|
|
21
|
-
* 协议段为宽松对象,装配时由对应协议包的 zod schema 严格校验;
|
|
22
|
-
* `config validate` / `config apply` 例外(见 protocol-validate.ts:两条路径承诺与
|
|
23
|
-
* 装配同 schema,否则「校验/apply 通过但启动 78」,故仅在这两处懒加载 adapter 补校验)。
|
|
24
|
-
* 全部校验错误为 ConfigError(2026-10-25 嵌入式退出码契约:CLI exit 78 一族,
|
|
25
|
-
* 内联与文件两形态共用本校验器)。
|
|
16
|
+
* 主配置类型与手写校验器(不引入 zod):一个 QQ 账号一个 [[accounts]] 段,协议与通信配置嵌在账号内,
|
|
17
|
+
* 账号必填;协议段为宽松对象,装配时由对应协议包 zod 严格校验(validate/apply 例外,见 protocol-validate.ts)。
|
|
18
|
+
* 全部校验错误为 ConfigError(CLI exit 78 一族),内联与文件两形态共用本校验器。
|
|
26
19
|
*/
|
|
27
20
|
/** 保留占位 QQ 号(模板默认值):必须替换为真实 QQ 号,否则启动校验报错。 */
|
|
28
21
|
const RESERVED_PLACEHOLDER_QINS = ["123456", "654321"];
|
|
29
22
|
/** 默认自动重启。 */
|
|
30
23
|
const DEFAULT_AUTO_RESTART = true;
|
|
31
|
-
/** 默认入站图片自动下载补丁开关(stub
|
|
24
|
+
/** 默认入站图片自动下载补丁开关(stub 私有组件)。 */
|
|
32
25
|
const DEFAULT_NATIVE_MEDIA_PATCH = true;
|
|
33
26
|
/** 默认重启延迟(毫秒;supervisor 显式 -q 场景复用,避免两处常量漂移)。 */
|
|
34
27
|
const DEFAULT_RESTART_DELAY_MS = 2e3;
|
|
@@ -181,23 +174,9 @@ function parseCliConfig(input) {
|
|
|
181
174
|
}
|
|
182
175
|
// src/config/inline.ts
|
|
183
176
|
/**
|
|
184
|
-
* 内联配置(`NAPUKETTO_CONFIG_JSON
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
* (与 napuketto.toml 同构:parseCliConfig 直接吃 JSON.parse 产物),消除托管
|
|
188
|
-
* toml 文件本体。优先级 NAPUKETTO_CONFIG_JSON > NAPUKETTO_CONFIG > 缺省模板生成;
|
|
189
|
-
* 内联生效时不读 / 不写 / 不校验任何 toml 文件。Windows 单环境变量上限 32767
|
|
190
|
-
* 字符(约 32KB),嵌入式配置形状远小于该限(超限宿主退回文件形态)。
|
|
191
|
-
*
|
|
192
|
-
* 读取口径分两档(有意为之):
|
|
193
|
-
* - parseInlineConfig:严格,主配置加载 / config validate 用;JSON 解析失败、
|
|
194
|
-
* 非对象、字段校验失败一律 ConfigError(exit 78 一族)。
|
|
195
|
-
* - readInlineConfigDoc / readInlineConfigSection:宽松,logger 读 [log] 段用;
|
|
196
|
-
* 坏文档返回 undefined(日志配置损坏不阻塞启动,严格校验由主配置路径负责)。
|
|
197
|
-
* loader 引导进程另有自己的宽松读取(编译期不静态依赖 kernel),两套不合并。
|
|
198
|
-
*
|
|
199
|
-
* 错误文案指明来源为「内联配置」且不含任何 toml 文件路径。本模块不 import
|
|
200
|
-
* logger(logger.ts 反向依赖本模块判内联来源)。
|
|
177
|
+
* 内联配置(`NAPUKETTO_CONFIG_JSON`):KuroBridge 等嵌入式宿主生成 JSON 经 env 喂入(与 napuketto.toml
|
|
178
|
+
* 同构),消除托管 toml 本体;优先级 NAPUKETTO_CONFIG_JSON > NAPUKETTO_CONFIG > 缺省模板生成,内联生效时不读写任何 toml。
|
|
179
|
+
* Windows 单个 env 上限 32767 字符(约 32KB),嵌入式配置形状远小于该限(超限宿主退回文件形态)。
|
|
201
180
|
*/
|
|
202
181
|
/** 内联配置 env 键(唯一规范拼写;读取为精确键访问,Windows 上 process.env 本身大小写不敏感)。 */
|
|
203
182
|
const INLINE_CONFIG_ENV = "NAPUKETTO_CONFIG_JSON";
|
|
@@ -222,7 +201,8 @@ function asConfigObject(value) {
|
|
|
222
201
|
}
|
|
223
202
|
/**
|
|
224
203
|
* 读内联配置文档(宽松口径:未设置 / 空串 / JSON 解析失败 / 非对象 → undefined)。
|
|
225
|
-
*
|
|
204
|
+
* 供日志段等「坏配置不阻塞启动」的消费方使用;loader 引导进程另有自己的宽松读取
|
|
205
|
+
* (编译期不静态依赖 kernel),两套有意不合并。
|
|
226
206
|
*/
|
|
227
207
|
function readInlineConfigDoc() {
|
|
228
208
|
const text = readInlineConfigText();
|
|
@@ -238,7 +218,7 @@ function readInlineConfigSection(key) {
|
|
|
238
218
|
const doc = readInlineConfigDoc();
|
|
239
219
|
return doc === void 0 ? void 0 : doc[key];
|
|
240
220
|
}
|
|
241
|
-
/** 严格解析内联 JSON 对象(主配置校验入口;错误一概 ConfigError
|
|
221
|
+
/** 严格解析内联 JSON 对象(主配置校验入口;错误一概 ConfigError,文案标明「内联配置」且不含 toml 路径)。 */
|
|
242
222
|
function parseJsonObject(text) {
|
|
243
223
|
let doc;
|
|
244
224
|
try {
|
|
@@ -260,17 +240,9 @@ function parseInlineConfig(text) {
|
|
|
260
240
|
}
|
|
261
241
|
// src/config/protocol-validate.ts
|
|
262
242
|
/**
|
|
263
|
-
* 协议段严格校验(`config validate` / `config apply`
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
* 拉进 CLI 进程,字段级校验由 loader 装配时用各协议自己的 zod schema 完成。
|
|
267
|
-
* 但 `config validate` 的承诺是「与运行时同一校验器落地」——只做 `isRecord`
|
|
268
|
-
* 浅校验会让 `httpServers = "not-an-array"` 报「配置合法」,真正启动却以 78
|
|
269
|
-
* 退出;`config apply` 若跳过这层,则会把非法协议段写进主配置。因此两条路径
|
|
270
|
-
* 懒加载 adapter 补校验(普通 CLI 启动与 supervisor 不引入协议包)。
|
|
271
|
-
*
|
|
272
|
-
* 空段与 loader 装配语义一致:`{}` = 不启用该协议,跳过校验(kurobot 的
|
|
273
|
-
* url 必填只在段非空时生效)。
|
|
243
|
+
* 协议段严格校验(`config validate` / `config apply` 共用):schema 刻意保持协议段为宽松对象——普通启动
|
|
244
|
+
* 不把协议包拉进 CLI 进程,字段级校验由 loader 装配时用各协议自己的 zod schema 完成;但 validate 承诺
|
|
245
|
+
* 「与运行时同一校验器落地」:只做浅校验会让 httpServers 非法时误报合法、启动却 78;故懒加载 adapter 补校验。
|
|
274
246
|
*/
|
|
275
247
|
const PROTOCOL_KEYS = [
|
|
276
248
|
"onebot11",
|
|
@@ -288,7 +260,10 @@ async function loadProtocolSchemas() {
|
|
|
288
260
|
milky: milkyConfigSchema
|
|
289
261
|
};
|
|
290
262
|
}
|
|
291
|
-
/**
|
|
263
|
+
/**
|
|
264
|
+
* 单账号协议段校验(空段 `{}` = 不启用,与 loader 装配语义一致而跳过——kurobot 的 url
|
|
265
|
+
* 必填只在段非空时生效;zod 错误压成单行,归 ConfigError 78 一族)。
|
|
266
|
+
*/
|
|
292
267
|
function validateAccountSections(account, schemas) {
|
|
293
268
|
for (const key of PROTOCOL_KEYS) {
|
|
294
269
|
const section = account[key];
|
|
@@ -320,12 +295,8 @@ function validateSandboxConfigPaths(config, dataDir) {
|
|
|
320
295
|
}
|
|
321
296
|
// src/config/template.ts
|
|
322
297
|
/**
|
|
323
|
-
* 配置模板(TOML
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
* 2026-08-08 结构拍板:**一个 QQ 账号一个 [[accounts]] 段,协议与通信配置嵌在
|
|
327
|
-
* 账号内**([accounts.onebot11] / [accounts.satori]);账号必填(至少一个)。
|
|
328
|
-
* dataDir 不再写死(缺省 = 项目根/.napuketto,跨平台),模板保持可移植。
|
|
298
|
+
* 配置模板(TOML 注释版):`config init` 与首次启动(loadCliConfig)缺失时生成。
|
|
299
|
+
* 一个 QQ 账号一个 [[accounts]] 段、协议与通信配置嵌在账号内;dataDir 不写死以保持可移植。
|
|
329
300
|
*/
|
|
330
301
|
function configTemplate() {
|
|
331
302
|
return `# ============================================================
|
|
@@ -505,11 +476,8 @@ port = 3001
|
|
|
505
476
|
// src/config/store.ts
|
|
506
477
|
/**
|
|
507
478
|
* 主配置 store:默认值、ConfigBase 子类、模板落盘、内联/文件来源加载。
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
* - `CliConfigStore` 只负责 napuketto.toml 的读写/校验(kernel ConfigBase);
|
|
511
|
-
* - `loadCliConfigWithSource` 负责来源决议(NAPUKETTO_CONFIG_JSON 优先)与来源日志;
|
|
512
|
-
* - 4 个 config 子命令留在 commands/config.ts,不反向依赖本文件的私有实现。
|
|
479
|
+
* `CliConfigStore` 只管 napuketto.toml 的读写/校验;`loadCliConfigWithSource` 负责来源决议(内联优先)
|
|
480
|
+
* 与来源日志,config 子命令只消费其导出接口。
|
|
513
481
|
*/
|
|
514
482
|
/** 主配置默认值(dataRoot 为当前解析的数据根 = 项目根/.napuketto)。 */
|
|
515
483
|
function defaultCliConfig(dataRoot) {
|
|
@@ -573,7 +541,7 @@ function resolveCliConfigSource(opts = {}) {
|
|
|
573
541
|
/** 「配置来源」日志每进程至多一行(autoStart 与 runSupervisor 各加载一次)。 */
|
|
574
542
|
let sourceLogged = false;
|
|
575
543
|
/**
|
|
576
|
-
*
|
|
544
|
+
* 加载主配置(内联优先):
|
|
577
545
|
* NAPUKETTO_CONFIG_JSON 非空 → 内存文档校验(不读 / 不写 / 不校验任何 toml
|
|
578
546
|
* 文件),打一行来源日志;否则现行文件链路(缺失自动落模板)。校验失败两形态
|
|
579
547
|
* 同为 ConfigError(exit 78),文案区别只在来源标注。
|
|
@@ -602,15 +570,9 @@ async function loadCliConfigWithSource(opts = {}) {
|
|
|
602
570
|
}
|
|
603
571
|
// src/logger.ts
|
|
604
572
|
/**
|
|
605
|
-
* cli 进程级 logger
|
|
606
|
-
*
|
|
607
|
-
*
|
|
608
|
-
* 环境变量决定(kernel parseLogConfig 单一解析源)。懒构造:commander preAction
|
|
609
|
-
* 钩子先 resolveCliLogConfig(dataDir)(banner 决策 + 缓存),首个日志调用时按
|
|
610
|
-
* 缓存配置构造;未初始化时兜底用缺省数据根解析(-d 生效前的极早期日志)。
|
|
611
|
-
*
|
|
612
|
-
* 环境变量(优先级高于 toml,见 kernel parseLogConfig):NAPUKETTO_LOG_LEVEL /
|
|
613
|
-
* NAPUKETTO_LOG_FORMAT / NAPUKETTO_NO_BANNER / NAPUKETTO_LOG_COLORIZE。
|
|
573
|
+
* cli 进程级 logger:复用 kernel `createLogger`,输出格式/级别/时间戳/颜色由 napuketto.toml [log] 段 +
|
|
574
|
+
* 环境变量决定(kernel parseLogConfig 单一解析源;env 优先级高于 toml)。
|
|
575
|
+
* 懒构造:preAction 钩子先 resolveCliLogConfig 缓存(banner 决策),首个日志调用时按缓存构造。
|
|
614
576
|
*/
|
|
615
577
|
/** 已解析日志配置缓存(同一数据根复用;显式传入不同数据根时重新解析)。 */
|
|
616
578
|
let cachedConfig = null;
|
|
@@ -618,9 +580,9 @@ let cachedConfig = null;
|
|
|
618
580
|
let cachedDataRoot = resolveDataRoot();
|
|
619
581
|
/** 同步解析日志配置(内联 > toml 文件的 [log] 段 + env 覆盖)。损坏/缺失配置回退缺省
|
|
620
582
|
* (不阻塞启动:配置的严格校验由 loadCliConfig 主路径负责报错)。内联生效时不读
|
|
621
|
-
* 任何 toml
|
|
583
|
+
* 任何 toml 文件。
|
|
622
584
|
*
|
|
623
|
-
*
|
|
585
|
+
* 缓存口径:显式 `dataDir` 解析出的根与缓存不一致时重新解析,
|
|
624
586
|
* 避免早期无参调用(缺省数据根)把后续 `-d` 指定数据根的配置/日志路径锁死;
|
|
625
587
|
* 无参调用始终复用当前缓存(preAction 解析后,懒构造 logger 的 LazyCliLogger
|
|
626
588
|
* 不能再用缺省根把 `-d` 的结果覆盖掉)。 */
|
|
@@ -890,14 +852,9 @@ async function cmdConfigValidate(opts) {
|
|
|
890
852
|
}
|
|
891
853
|
// src/runtime/state.ts
|
|
892
854
|
/**
|
|
893
|
-
*
|
|
894
|
-
*
|
|
895
|
-
*
|
|
896
|
-
* koishi 插件专用),supervisor 子进程不在此模式;引入独立通道(socket/pipe)
|
|
897
|
-
* 复杂度高。采用 **PID 状态文件**:supervisor/单账号 boot 启动时把进程信息写
|
|
898
|
-
* `<数据根>/<uin>/runtime.json`,supervisor 自身写 `<数据根>/supervisor.json`;
|
|
899
|
-
* `napuketto status/stop/restart` 读文件 + liveness 检测 + 树杀(win32
|
|
900
|
-
* taskkill /T /F,杀 boot 进程树连带 self-host 孙进程)+ detached 重启。
|
|
855
|
+
* 账号运行时状态文件 + 进程控制原语(跨进程通信走 PID 状态文件而非 IPC 通道:现有 stdio JSON 行协议仅
|
|
856
|
+
* koishi 插件用,supervisor 子进程不在此模式,独立 socket/pipe 复杂度高)。supervisor/boot 写
|
|
857
|
+
* `<数据根>/<uin>/runtime.json` 与 `supervisor.json`;status/stop/restart 读文件 + liveness + 树杀。
|
|
901
858
|
*/
|
|
902
859
|
/**
|
|
903
860
|
* supervisor 注入给受管子进程的托管标记 env。
|
|
@@ -1000,17 +957,13 @@ function isAccountRuntimeKind(val) {
|
|
|
1000
957
|
function isAccountRuntimeStatus(val) {
|
|
1001
958
|
return val === "running" || val === "exited";
|
|
1002
959
|
}
|
|
1003
|
-
/** 可选字符串字段判定(缺省合法)。 */
|
|
1004
|
-
function isOptionalString(val) {
|
|
1005
|
-
return val === void 0 || typeof val === "string";
|
|
1006
|
-
}
|
|
1007
960
|
/** runtime.json 形状守卫(损坏 / 旧格式 / 手工改错的文件一律判非法)。 */
|
|
1008
961
|
function isAccountRuntime(val) {
|
|
1009
|
-
return isRecord(val) && hasStringProp(val, "uin") && isValidPid(val["pid"]) && hasStringProp(val, "startedAt") && isAccountRuntimeKind(val["kind"]) && isAccountRuntimeStatus(val["status"]) && (val["selfHostPid"] === void 0 || isValidPid(val["selfHostPid"])) &&
|
|
962
|
+
return isRecord(val) && hasStringProp(val, "uin") && isValidPid(val["pid"]) && hasStringProp(val, "startedAt") && isAccountRuntimeKind(val["kind"]) && isAccountRuntimeStatus(val["status"]) && (val["selfHostPid"] === void 0 || isValidPid(val["selfHostPid"])) && hasOptionalStringProp(val, "cmdline") && hasOptionalStringProp(val, "selfHostCmdline");
|
|
1010
963
|
}
|
|
1011
964
|
/** supervisor.json 形状守卫。 */
|
|
1012
965
|
function isSupervisorRuntime(val) {
|
|
1013
|
-
return isRecord(val) && isValidPid(val["pid"]) && hasStringProp(val, "startedAt") && Array.isArray(val["accounts"]) && val["accounts"].every((item) => typeof item === "string") &&
|
|
966
|
+
return isRecord(val) && isValidPid(val["pid"]) && hasStringProp(val, "startedAt") && Array.isArray(val["accounts"]) && val["accounts"].every((item) => typeof item === "string") && hasOptionalStringProp(val, "cmdline");
|
|
1014
967
|
}
|
|
1015
968
|
/** 读账号运行时状态(缺失/损坏返回 null)。 */
|
|
1016
969
|
function readAccountRuntime(dataRoot, uin) {
|
|
@@ -1056,17 +1009,9 @@ function isPidAlive(pid) {
|
|
|
1056
1009
|
process$1.kill(pid, 0);
|
|
1057
1010
|
return true;
|
|
1058
1011
|
} catch (err) {
|
|
1059
|
-
return
|
|
1012
|
+
return getErrorCode(err) === "EPERM";
|
|
1060
1013
|
}
|
|
1061
1014
|
}
|
|
1062
|
-
/** 目标进程不存在(ESRCH):正常路径,不回调 onError。 */
|
|
1063
|
-
function isNoSuchProcess(err) {
|
|
1064
|
-
return isRecord(err) && err["code"] === "ESRCH";
|
|
1065
|
-
}
|
|
1066
|
-
/** 去重并过滤非法 pid(类型/范围统一走 loader isValidPid)。 */
|
|
1067
|
-
function uniquePids(pids) {
|
|
1068
|
-
return [...new Set(pids)].filter((pid) => isValidPid(pid));
|
|
1069
|
-
}
|
|
1070
1015
|
/**
|
|
1071
1016
|
* 树杀(超时兜底强杀):win32 走 `taskkill /T /F`;POSIX 先 SIGTERM,宽限后对
|
|
1072
1017
|
* 仍存活目标补 SIGKILL。
|
|
@@ -1078,7 +1023,7 @@ function uniquePids(pids) {
|
|
|
1078
1023
|
* 是异步事件,不监听会静默失败。
|
|
1079
1024
|
*/
|
|
1080
1025
|
function killProcessTree(pid, options = {}) {
|
|
1081
|
-
const targets =
|
|
1026
|
+
const targets = [.../* @__PURE__ */ new Set([pid, ...options.extraPids ?? []])].filter((target) => isValidPid(target));
|
|
1082
1027
|
if (targets.length === 0) return;
|
|
1083
1028
|
if (process$1.platform === "win32") {
|
|
1084
1029
|
for (const target of targets) spawn("taskkill", [
|
|
@@ -1098,7 +1043,7 @@ function killProcessTree(pid, options = {}) {
|
|
|
1098
1043
|
try {
|
|
1099
1044
|
process$1.kill(target, signal);
|
|
1100
1045
|
} catch (err) {
|
|
1101
|
-
if (
|
|
1046
|
+
if (getErrorCode(err) !== "ESRCH") options.onError?.(`发送 ${signal} 失败(pid ${target}):${errorMessage(err)}`);
|
|
1102
1047
|
}
|
|
1103
1048
|
};
|
|
1104
1049
|
const killTarget = (target, signal) => {
|
|
@@ -1167,15 +1112,9 @@ function aggregateStatus(runtimes, aliveFn = isPidAlive) {
|
|
|
1167
1112
|
}
|
|
1168
1113
|
// src/commands/ops.ts
|
|
1169
1114
|
/**
|
|
1170
|
-
*
|
|
1171
|
-
*
|
|
1172
|
-
*
|
|
1173
|
-
* stop/restart 语义(2026-10-25 优雅化):
|
|
1174
|
-
* - stop -q <uin>:写单账号停止哨兵 → 等该账号进程链优雅退出 → 超时树杀兜底。
|
|
1175
|
-
* ⚠️ supervisor 在跑且 autoRestart 时账号会被自动拉起——提示用户先 `napuketto stop`。
|
|
1176
|
-
* - stop(无 -q):写 all 哨兵(supervisor + 全部账号优雅退出)→ 超时树杀兜底。
|
|
1177
|
-
* - restart:stop 同目标 + detached 重新拉起(-q → 单账号 boot;无 -q →
|
|
1178
|
-
* supervisor autoStart)。
|
|
1115
|
+
* status / stop / restart 运维命令实现:数据源为 runtime/state 状态文件 + liveness 检测。
|
|
1116
|
+
* stop 先写停止哨兵(单账号或 all)等进程链优雅退出,超时再树杀兜底;restart = stop 同目标 + detached 拉起。
|
|
1117
|
+
* ⚠️ supervisor 在跑且 autoRestart 时单账号会被自动拉起,彻底停止需 `napuketto stop`。
|
|
1179
1118
|
*/
|
|
1180
1119
|
/** 优雅停等待上限(毫秒):哨兵后子进程链自行退出,超时树杀兜底。 */
|
|
1181
1120
|
const GRACEFUL_STOP_WAIT_MS = 8e3;
|
|
@@ -1284,13 +1223,7 @@ function killAccountTree(runtime) {
|
|
|
1284
1223
|
pids: targets
|
|
1285
1224
|
}, "已发送停止(树杀)");
|
|
1286
1225
|
}
|
|
1287
|
-
/** stop
|
|
1288
|
-
*
|
|
1289
|
-
* 2026-10-25 优雅化:先写停止哨兵(目标账号或 all)→ 等进程链自行有序退出
|
|
1290
|
-
* (退出码 0)→ 超时残留树杀兜底 → 清理哨兵。单账号哨兵只命中对应 boot/self-host,
|
|
1291
|
-
* 不干扰同数据根的其他账号。
|
|
1292
|
-
*/
|
|
1293
|
-
/** stop -q:写单账号独立哨兵 → 等优雅退出 → 超时树杀兜底 → 清本账号哨兵。 */
|
|
1226
|
+
/** stop -q:写单账号独立哨兵 → 等优雅退出(退出码 0)→ 超时树杀兜底 → 清本账号哨兵;不干扰同数据根其他账号。 */
|
|
1294
1227
|
async function stopSingleAccount(dataRoot, target) {
|
|
1295
1228
|
logger.info({
|
|
1296
1229
|
uin: target.uin,
|
|
@@ -1389,16 +1322,11 @@ async function cmdRestart(dataRoot, opts) {
|
|
|
1389
1322
|
}
|
|
1390
1323
|
// src/runtime/lines.ts
|
|
1391
1324
|
/**
|
|
1392
|
-
* 子进程输出逐行切分(boot / supervisor
|
|
1393
|
-
*
|
|
1394
|
-
*
|
|
1395
|
-
* 缓冲上限,原生层(QQ wrapper.node)异常刷屏或误写二进制时会把整块数据积在
|
|
1396
|
-
* 内存里,直到换行符出现才触发 line 事件。这里用轻量切分器保留逐行语义
|
|
1397
|
-
* (UTF-8 多字节跨 chunk 由 StringDecoder 拼接、\r\n 归一),同时给单行缓冲设
|
|
1398
|
-
* 上限:超过上限的部分直接丢弃,行结束时补 `…[truncated]` 标记。
|
|
1399
|
-
* 上限按字符计(UTF-16 code unit),64 KiB 对正常日志行足够宽松。
|
|
1325
|
+
* 子进程输出逐行切分(boot / supervisor 共用):不用 `readline.createInterface` 是因为它对
|
|
1326
|
+
* 「无换行符的超大输出」没有缓冲上限(原生层 wrapper.node 刷屏会把整块数据积在内存里)。
|
|
1327
|
+
* 这里用 StringDecoder 拼多字节、`\r\n` 归一,并给单行缓冲设上限,超出部分丢弃并标记截断。
|
|
1400
1328
|
*/
|
|
1401
|
-
/**
|
|
1329
|
+
/** 单行缓冲上限(字符,UTF-16 code unit):64 KiB 对正常日志行足够宽松,异常无换行输出在此截断。 */
|
|
1402
1330
|
const MAX_LINE_LENGTH = 65536;
|
|
1403
1331
|
/** 截断行尾标记(保留 grep 可见性,避免静默丢数据)。 */
|
|
1404
1332
|
const TRUNCATED_SUFFIX = "…[truncated]";
|
|
@@ -1460,19 +1388,14 @@ function forwardLines(input, onLine, maxLineLength = MAX_LINE_LENGTH) {
|
|
|
1460
1388
|
}
|
|
1461
1389
|
// src/runtime/qr.ts
|
|
1462
1390
|
/**
|
|
1463
|
-
* 终端二维码渲染(uqr 编码 + half-block ANSI
|
|
1464
|
-
*
|
|
1465
|
-
*
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
*
|
|
1469
|
-
*
|
|
1470
|
-
*
|
|
1471
|
-
* 因此这里只取 uqr 的编码结果,按旧 qrcode `terminal + small` 的观感自行绘制:
|
|
1472
|
-
* 白底(`\x1b[47m`)+ 黑模块(`\x1b[30m`),两行模块压进一个 `▀▄█` 字符行。
|
|
1473
|
-
* 好处是与终端主题无关、宽度恒等于模块数(含静区),扫码行为与替换前一致。
|
|
1391
|
+
* 终端二维码渲染(uqr 编码 + half-block ANSI):只取 uqr 的编码结果,它自带的两个终端渲染器
|
|
1392
|
+
* 都不合用——`renderANSI` 每模块占一个全角空格使宽度翻倍,登录 URL 动辄 40+ 模块,80 列终端
|
|
1393
|
+
* 折行即废码;`renderUnicodeCompact` 用默认前景色,极性随主题反色。故自绘白底黑模块。
|
|
1394
|
+
*/
|
|
1395
|
+
/**
|
|
1396
|
+
* 白底 + 黑前景:静区/亮模块走背景色,暗模块走前景色,两行模块压进一个 `▀▄█` 字符行。
|
|
1397
|
+
* 宽度恒等于模块数(含静区)且与终端主题无关。
|
|
1474
1398
|
*/
|
|
1475
|
-
/** 白底 + 黑前景:静区/亮模块走背景色,暗模块走前景色。 */
|
|
1476
1399
|
const STYLE = "\x1B[47m\x1B[30m";
|
|
1477
1400
|
const RESET = "\x1B[0m";
|
|
1478
1401
|
/** 纠错级别 M(15% 容错)与旧 qrcode 默认值一致;静区 1 模块同旧输出。 */
|
|
@@ -1548,7 +1471,7 @@ function renderTerminalQr(raw) {
|
|
|
1548
1471
|
process$1.stdout.write(`${renderQrAnsi(data["qrcodeUrl"])}\n`);
|
|
1549
1472
|
} catch {}
|
|
1550
1473
|
}
|
|
1551
|
-
/**
|
|
1474
|
+
/** 解析自建宿主装配面的三个包入口。 */
|
|
1552
1475
|
async function resolvePackageEntries() {
|
|
1553
1476
|
return {
|
|
1554
1477
|
kernelEntry: await packageEntry("@napuketto/kernel"),
|
|
@@ -1560,8 +1483,14 @@ async function resolvePackageEntries() {
|
|
|
1560
1483
|
function resolveStubDir(opts) {
|
|
1561
1484
|
return opts.stubDir ?? process$1.env["NAPUKETTO_STUB_DIR"] ?? defaultStubDir();
|
|
1562
1485
|
}
|
|
1563
|
-
/**
|
|
1564
|
-
*
|
|
1486
|
+
/**
|
|
1487
|
+
* QQ 原生文件多级来源 + 自动下载(本机未装 QQ 也能跑):L0 NAPUKETTO_QQ_FILES 显式文件根 →
|
|
1488
|
+
* L1 本机 QQ 安装(--qq-path / NAPUKETTO_QQ_PATH / 注册表 / 常见路径)→ L2 数据根缓存
|
|
1489
|
+
* `<数据根>/qq-files/<版本>` → 全缺失时下载官方安装包(sha256 校验 + 7z 解包 + 提取缓存,幂等)。
|
|
1490
|
+
* cached 来源下 qq.qqPath 为语义占位(缓存目录无 QQ.exe)——launchSelfHost 只消费
|
|
1491
|
+
* qq.wrapperPath / qq.version(NAPUKETTO_QQ_PATH 仅注入、self-host 未读取),占位安全。
|
|
1492
|
+
* 失败记录可操作提示并置 exitCode 1,返回 undefined(调用方提前返回)。
|
|
1493
|
+
*/
|
|
1565
1494
|
async function locateQqFiles(dataRoot, opts) {
|
|
1566
1495
|
try {
|
|
1567
1496
|
return await resolveQqFiles({
|
|
@@ -1575,12 +1504,10 @@ async function locateQqFiles(dataRoot, opts) {
|
|
|
1575
1504
|
}
|
|
1576
1505
|
}
|
|
1577
1506
|
/**
|
|
1578
|
-
*
|
|
1579
|
-
*
|
|
1580
|
-
*
|
|
1581
|
-
*
|
|
1582
|
-
* 获取/释放(它才是数据目录的实际持有者),cli 这里只做 spawn 前预检,
|
|
1583
|
-
* 给用户友好提示(supervisor 等批量拉起同用)。
|
|
1507
|
+
* 单实例锁预检:同一账号数据目录只允许一个实例(QQ 原生层 MMKV/登录单例有锁,第二个实例
|
|
1508
|
+
* 抢不到锁会卡死)。占用 → 快速失败并提示占用 PID;残留(PID 已死 / PID 被复用,靠 cmdline
|
|
1509
|
+
* 校验识别)→ self-host 会自动接管。真正的锁由 self-host 子进程获取/释放(它才是数据目录的
|
|
1510
|
+
* 实际持有者),cli 这里只做 spawn 前预检给友好提示(supervisor 等批量拉起同用)。
|
|
1584
1511
|
* @returns 可用 true;占用时记录错误并置 exitCode 1 返回 false
|
|
1585
1512
|
*/
|
|
1586
1513
|
function ensureInstanceLockFree(cfgDir) {
|
|
@@ -1594,8 +1521,8 @@ function ensureInstanceLockFree(cfgDir) {
|
|
|
1594
1521
|
return false;
|
|
1595
1522
|
}
|
|
1596
1523
|
/** 启动信息走结构化日志(时间戳 + 级别 + pid + 元数据,与 kernel 子进程格式一致)。
|
|
1597
|
-
*
|
|
1598
|
-
*
|
|
1524
|
+
* 引导细节降 debug(banner 之外的定位/路径细节对运行状态无信息量),
|
|
1525
|
+
* 致命错误与登录/退出生命周期保持 info 及以上。 */
|
|
1599
1526
|
function logBootInfo(qq, cfgDir) {
|
|
1600
1527
|
logger.debug({
|
|
1601
1528
|
qqVersion: qq.version,
|
|
@@ -1605,14 +1532,14 @@ function logBootInfo(qq, cfgDir) {
|
|
|
1605
1532
|
logger.debug({ dataDir: cfgDir }, "数据目录");
|
|
1606
1533
|
logger.debug("自建宿主引导(标准 node + stub QQNT.dll)");
|
|
1607
1534
|
}
|
|
1608
|
-
/** launchSelfHost
|
|
1535
|
+
/** launchSelfHost 调用参数构造(唯一启动路径:自建宿主)。
|
|
1609
1536
|
* configPath:全局配置文件(项目根 napuketto.toml),注入 NAPUKETTO_CONFIG 供装配链读取;
|
|
1610
1537
|
* 内联配置生效(NAPUKETTO_CONFIG_JSON)时不解析、不注入——子进程经 env 继承
|
|
1611
1538
|
* 内联文本,装配链按同一优先级读取;避免项目根探测出无关路径注入孙进程。
|
|
1612
1539
|
* extraEnv:传播有效控制台渲染决策(子进程 stdout 恒 pipe,TTY 判定失真)+
|
|
1613
1540
|
* 数据根(NAPUKETTO_DATA_ROOT,self-host 停止哨兵目录)。
|
|
1614
1541
|
* cwd:子进程 cwd 指向数据根——QQ 原生层 fallback 落盘(guild1.db 等)落在专门
|
|
1615
|
-
*
|
|
1542
|
+
* 的数据目录,不污染项目根。 */
|
|
1616
1543
|
function buildLaunchOptions(ctx) {
|
|
1617
1544
|
const { opts, qq, cfgDir, dataRoot, entries, stubDir } = ctx;
|
|
1618
1545
|
return {
|
|
@@ -1638,7 +1565,7 @@ function buildLaunchOptions(ctx) {
|
|
|
1638
1565
|
};
|
|
1639
1566
|
}
|
|
1640
1567
|
/** 启动自建宿主子进程(包入口解析 → stub 目录 → 启动信息 → launchSelfHost)。
|
|
1641
|
-
* ⚠️
|
|
1568
|
+
* ⚠️ launchSelfHost 是 async(win32 非 node 宿主需解析/下载 win-node)。 */
|
|
1642
1569
|
async function startSelfHost(opts, qq, cfgDir, dataRoot) {
|
|
1643
1570
|
const entries = await resolvePackageEntries();
|
|
1644
1571
|
const stubDir = resolveStubDir(opts);
|
|
@@ -1669,8 +1596,7 @@ function isSupervisedChild() {
|
|
|
1669
1596
|
return process$1.env[SUPERVISOR_CHILD_ENV] === "1";
|
|
1670
1597
|
}
|
|
1671
1598
|
/**
|
|
1672
|
-
* 单账号运行时状态落盘(napuketto status/stop
|
|
1673
|
-
* pid 记 boot 进程——树杀连带 self-host 孙进程)。
|
|
1599
|
+
* 单账号运行时状态落盘(napuketto status/stop 消费;pid 记 boot 进程——树杀连带 self-host 孙进程)。
|
|
1674
1600
|
*
|
|
1675
1601
|
* 托管模式(supervisor 拉起)下 runtime.json 的所有权在 supervisor:`kind`、
|
|
1676
1602
|
* `restartCount`、启动时间与 boot pid 由它写入,本函数只合并补充 selfHostPid /
|
|
@@ -1715,7 +1641,7 @@ function recordRuntimeState(dataRoot, account, child, status, exitCode) {
|
|
|
1715
1641
|
...exitCode !== void 0 ? { exitCode } : {}
|
|
1716
1642
|
});
|
|
1717
1643
|
}
|
|
1718
|
-
/**
|
|
1644
|
+
/** 优雅关停编排:触发后等 self-host 经自身哨兵/信号
|
|
1719
1645
|
* 处理自行退出(exit 0),10s 未退则 kill 兜底(Windows 下为硬杀)——
|
|
1720
1646
|
* boot 进程随后随 child 退出自然返回(退出码 0 = 请求式正常退出)。
|
|
1721
1647
|
* 返回句柄由 runSingleAccount 在 waitSelfHostExit 的 finally 中 dispose:
|
|
@@ -1779,7 +1705,7 @@ function installGracefulStop(child, dataRoot, account) {
|
|
|
1779
1705
|
/**
|
|
1780
1706
|
* 常驻:等待自建宿主进程退出(exit / error 两路径均 resolve,去留由 self-host 决定)。
|
|
1781
1707
|
*
|
|
1782
|
-
*
|
|
1708
|
+
* 退出码契约:boot 进程必须把 self-host 的失败退出码透传给父进程
|
|
1783
1709
|
* (supervisor / 容器编排),否则 supervisor 的 `EXIT_CONFIG_INVALID(78)` 免重启
|
|
1784
1710
|
* 判定永远不命中,所有失败都被当成 code=0 正常退出。规则:
|
|
1785
1711
|
* - 收到停止请求(isStopping)→ 0(请求式正常退出,与崩溃区分);
|
|
@@ -1829,21 +1755,16 @@ async function runSingleAccount(opts = {}) {
|
|
|
1829
1755
|
}
|
|
1830
1756
|
// src/runtime/supervisor.ts
|
|
1831
1757
|
/**
|
|
1832
|
-
* supervisor
|
|
1833
|
-
*
|
|
1834
|
-
*
|
|
1835
|
-
* --data-dir <dataRoot> [--stub-dir <dir>],复用单账号 runSingleAccount 零改动),
|
|
1836
|
-
* 本模块作为父进程:
|
|
1837
|
-
* - 拉起:主配置 accounts(supervisor 子命令)或显式 -q 列表
|
|
1838
|
-
* - 守护:账号异常退出且 autoRestart → 延迟重启
|
|
1839
|
-
* - 信号转发:SIGINT/SIGTERM → 停止全部子进程 → 父进程退出
|
|
1758
|
+
* supervisor:多账号走多进程(ADR-015)——每账号一个独立子进程(`node <入口> -q <uin> --data-dir <dataRoot>`),
|
|
1759
|
+
* 复用单账号 runSingleAccount。本模块作为父进程负责拉起(主配置 accounts 或显式 -q 列表)、守护重启
|
|
1760
|
+
* (异常退出且 autoRestart → 延迟重启)与信号转发(SIGINT/SIGTERM → 停止全部子进程 → 父进程退出)。
|
|
1840
1761
|
*/
|
|
1841
1762
|
/** 退出轮询间隔(毫秒)。 */
|
|
1842
1763
|
const EXIT_POLL_MS = 100;
|
|
1843
1764
|
/** 子进程优雅退出时限(毫秒):哨兵/信号后子进程自行停止,超时强杀兜底。 */
|
|
1844
1765
|
const FORCE_EXIT_MS = 8e3;
|
|
1845
1766
|
/**
|
|
1846
|
-
*
|
|
1767
|
+
* 短生命周期崩溃阈值(毫秒):运行不足该时长的退出视为「连续秒退」,
|
|
1847
1768
|
* 重启延迟按 `restartDelayMs * 2^n` 指数退避,避免登录后立刻崩溃时高频循环重登
|
|
1848
1769
|
* 触发 QQ 风控;稳定运行超过该时长后退避计数清零。
|
|
1849
1770
|
*/
|
|
@@ -1851,7 +1772,7 @@ const SHORT_LIVED_CRASH_MS = 3e4;
|
|
|
1851
1772
|
/** 指数退避延迟上限(毫秒):连续秒退时单次重启最多等 60s。 */
|
|
1852
1773
|
const MAX_RESTART_DELAY_MS = 6e4;
|
|
1853
1774
|
/**
|
|
1854
|
-
*
|
|
1775
|
+
* 计算下一次守护重启延迟。
|
|
1855
1776
|
* @param baseDelayMs 配置的首次重启延迟(restartDelayMs)
|
|
1856
1777
|
* @param consecutiveFailures 连续短生命周期失败次数(从 1 起)
|
|
1857
1778
|
*/
|
|
@@ -1931,7 +1852,7 @@ function writeAccountExited(ctx, acct, child, exitCode, restartCount) {
|
|
|
1931
1852
|
}, fallback);
|
|
1932
1853
|
}
|
|
1933
1854
|
/**
|
|
1934
|
-
*
|
|
1855
|
+
* 守护重启编排:短生命周期崩溃按指数退避,稳定运行超过阈值后
|
|
1935
1856
|
* 计数清零(恢复配置的基础延迟)。日志始终带连续失败次数与下次重启延迟。
|
|
1936
1857
|
*/
|
|
1937
1858
|
function scheduleRestart(ctx, acct, code, signal, startedAtMs, restartCount) {
|
|
@@ -2036,7 +1957,7 @@ function startAccount(ctx, acct) {
|
|
|
2036
1957
|
ctx.onSettled();
|
|
2037
1958
|
});
|
|
2038
1959
|
}
|
|
2039
|
-
/**
|
|
1960
|
+
/** 停止全部子进程,全部退出后父进程退出。
|
|
2040
1961
|
*
|
|
2041
1962
|
* 优雅路径:写停止哨兵(all)→ 子进程链(boot / self-host)自行有序退出 →
|
|
2042
1963
|
* 全部退出后 exit 0(退出码契约:0 = 请求式正常退出)。超时(FORCE_EXIT_MS)
|
|
@@ -2178,9 +2099,7 @@ async function runSupervisor(opts = {}) {
|
|
|
2178
2099
|
// src/commands/start.ts
|
|
2179
2100
|
/**
|
|
2180
2101
|
* 默认动作(无子命令)用例:-q 单账号 / 多 -q supervisor / 无 -q 读配置拉起全部账号。
|
|
2181
|
-
*
|
|
2182
|
-
* 从 index.ts 拆出(2026-10-25 模块收拢):这里只做编排,不 import commander;
|
|
2183
|
-
* 参数解析、帮助文案与退出码收敛在 cli/program.ts。
|
|
2102
|
+
* 这里只做编排,不 import commander;参数解析、帮助文案与退出码收敛在 cli/program.ts。
|
|
2184
2103
|
*/
|
|
2185
2104
|
/** 单账号启动分支(-q 单值;构造 bootOptions 后走 runSingleAccount)。 */
|
|
2186
2105
|
async function runSingleAccountBranch(opts, qqs) {
|
|
@@ -2193,8 +2112,7 @@ async function runSingleAccountBranch(opts, qqs) {
|
|
|
2193
2112
|
...opts.stubDir === void 0 ? {} : { stubDir: opts.stubDir }
|
|
2194
2113
|
});
|
|
2195
2114
|
}
|
|
2196
|
-
/** 无 -q 时:读全局配置 accounts
|
|
2197
|
-
* 不再有「配置为空 → 交互式单账号登录」老路径:缺账号直接报错提示编辑模板。
|
|
2115
|
+
/** 无 -q 时:读全局配置 accounts(账号必填),supervisor 拉起全部启用账号;缺账号直接报错提示编辑模板。
|
|
2198
2116
|
* 配置加载内联优先(NAPUKETTO_CONFIG_JSON > 文件);校验失败 ConfigError → exit 78。 */
|
|
2199
2117
|
async function autoStart(opts) {
|
|
2200
2118
|
const { config } = await loadCliConfigWithSource({
|
|
@@ -2209,7 +2127,7 @@ async function autoStart(opts) {
|
|
|
2209
2127
|
});
|
|
2210
2128
|
}
|
|
2211
2129
|
/**
|
|
2212
|
-
* -q
|
|
2130
|
+
* -q 列表入口校验:格式/占位值复用主配置规则,重复值直接报错。
|
|
2213
2131
|
* ConfigError → exit 78;放在分发最前,非法值不会进入数据目录拼接或 supervisor。
|
|
2214
2132
|
*/
|
|
2215
2133
|
function parseQqList(qqs) {
|
|
@@ -2248,26 +2166,15 @@ async function runSupervisorCommand(opts) {
|
|
|
2248
2166
|
}
|
|
2249
2167
|
// src/cli/error.ts
|
|
2250
2168
|
/**
|
|
2251
|
-
* CLI
|
|
2252
|
-
*
|
|
2253
|
-
*
|
|
2254
|
-
* 其余 = 1(运行时崩溃,现状不变)。
|
|
2255
|
-
*
|
|
2256
|
-
* 退出码只认错误类型(instanceof ConfigError),与日志文案完全解耦——
|
|
2257
|
-
* 文案是人读的,退出码是宿主消费的机器契约。
|
|
2258
|
-
*
|
|
2259
|
-
* 日志分两档:
|
|
2260
|
-
* - 预期内用户错误(ConfigError / KernelError INVALID_PARAM):消息链拼成单行,
|
|
2261
|
-
* 只带 errorType/code 轻量字段,不在默认级别打印堆栈;
|
|
2262
|
-
* - 预期外错误:保持 { err } 结构化输出(pino serializer 展开堆栈)。
|
|
2263
|
-
* 两档都保留 logger.debug({ err }) 的完整堆栈,排障不降级。
|
|
2169
|
+
* CLI 动作错误出口:0 = 请求式优雅关停(不经此路径);78 = 配置校验失败(ConfigError 一族,
|
|
2170
|
+
* 宿主据此区分「配置未就绪」与运行时崩溃,免重启);其余 = 1。
|
|
2171
|
+
* 退出码只认错误类型,与日志文案解耦;预期错误打单行结论,两档都留 debug 完整堆栈。
|
|
2264
2172
|
*/
|
|
2265
2173
|
/** 消息链最大深度(防御异常 cause 环与超长链)。 */
|
|
2266
2174
|
const MAX_CAUSE_DEPTH = 8;
|
|
2267
2175
|
/**
|
|
2268
2176
|
* 预期内用户错误:配置未就绪(ConfigError)或参数非法(INVALID_PARAM)。
|
|
2269
|
-
* ConfigError
|
|
2270
|
-
* 让「配置失败」与「参数非法」两类预期语义在分类处可见。
|
|
2177
|
+
* ConfigError 本身是 code=INVALID_PARAM 的 KernelError 子型,显式分开写让两类预期语义可见。
|
|
2271
2178
|
*/
|
|
2272
2179
|
function isExpectedUserError(err) {
|
|
2273
2180
|
return err instanceof ConfigError || isKernelError(err) && err.code === "INVALID_PARAM";
|
|
@@ -2290,7 +2197,7 @@ function errorChainText(err) {
|
|
|
2290
2197
|
}
|
|
2291
2198
|
return parts.join(" — ");
|
|
2292
2199
|
}
|
|
2293
|
-
/**
|
|
2200
|
+
/** 统一错误输出(预期错误打单行消息链 + debug 堆栈;预期外错误保持 { err } 结构化输出,pino serializer 展开堆栈)。 */
|
|
2294
2201
|
function reportError(err) {
|
|
2295
2202
|
if (isExpectedUserError(err)) {
|
|
2296
2203
|
logger.error({
|
|
@@ -2314,26 +2221,17 @@ function handleActionError(err) {
|
|
|
2314
2221
|
}
|
|
2315
2222
|
// src/cli/version.ts
|
|
2316
2223
|
/**
|
|
2317
|
-
* CLI 自身版本号(`--version`
|
|
2318
|
-
*
|
|
2319
|
-
* 2026-11-10 修复:此前 `program.version("0.0.1")` 是硬编码字面量,导致 npm 上
|
|
2320
|
-
* 无论发到哪个版本,`napuketto --version` 都自报 `0.0.1`(补发 0.5.14 后冒烟发现)。
|
|
2321
|
-
* 改为运行时读自身 `package.json`,发版由 changesets 改那一处即可自动跟随。
|
|
2322
|
-
*
|
|
2323
|
-
* 候选路径(先命中先用,且要求 `name` 对得上,避免读到别家的 package.json):
|
|
2324
|
-
* - `../package.json`:生产形态。tsdown 打成单文件 `dist/index.mjs`,
|
|
2325
|
-
* 相对 dist 上一级即包根。
|
|
2326
|
-
* - `../../package.json`:源码直跑形态(vitest / tsx),
|
|
2327
|
-
* 本文件位于 `src/cli/`,往上两级即包根。
|
|
2328
|
-
*
|
|
2329
|
-
* 都读不到(被裁剪的异常部署)兜底 `0.0.0-unknown`,不抛错——版本号拿不到
|
|
2330
|
-
* 不该让整个 CLI 起不来。
|
|
2224
|
+
* CLI 自身版本号(`--version` 输出):运行时读自身 `package.json`,发版只改那一处。
|
|
2225
|
+
* 候选路径先命中先用且校验 `name`,避免读到别家的 package.json(被裁剪的异常部署才会命中兜底)。
|
|
2331
2226
|
*/
|
|
2332
2227
|
/** 读不到 package.json 时的兜底版本号。 */
|
|
2333
2228
|
const VERSION_FALLBACK = "0.0.0-unknown";
|
|
2334
2229
|
/** 自身包名,用于校验候选 package.json 确实是本包。 */
|
|
2335
2230
|
const SELF_NAME = "@napuketto/cli";
|
|
2336
|
-
/**
|
|
2231
|
+
/**
|
|
2232
|
+
* 候选 package.json 相对路径(按命中概率排序):`../` 是 tsdown 单文件 `dist/index.mjs` 的生产形态,
|
|
2233
|
+
* `../../` 是源码直跑(vitest / tsx)形态——本文件位于 `src/cli/`,往上两级才是包根。
|
|
2234
|
+
*/
|
|
2337
2235
|
const CANDIDATES = ["../package.json", "../../package.json"];
|
|
2338
2236
|
/**
|
|
2339
2237
|
* 取 CLI 自身版本号。
|
|
@@ -2348,10 +2246,8 @@ function cliVersion() {
|
|
|
2348
2246
|
}
|
|
2349
2247
|
// src/cli/program.ts
|
|
2350
2248
|
/**
|
|
2351
|
-
* commander 装配层:全局选项、banner
|
|
2352
|
-
*
|
|
2353
|
-
* 从 index.ts 拆出(2026-10-25 模块收拢):只做 CLI 契约适配(参数/帮助/输出/
|
|
2354
|
-
* 退出码),用例实现见 commands/。禁止在此写业务逻辑。
|
|
2249
|
+
* commander 装配层:全局选项、banner、子命令注册、默认动作分发。只做 CLI 契约适配
|
|
2250
|
+
* (参数/帮助/输出/退出码),禁止在此写业务逻辑;用例实现见 commands/。
|
|
2355
2251
|
*/
|
|
2356
2252
|
/** commander collect:累积 -q 多值。 */
|
|
2357
2253
|
function collectQq(value, prev) {
|
|
@@ -2372,7 +2268,7 @@ function rootDataDir(program) {
|
|
|
2372
2268
|
const { dataDir } = program.opts();
|
|
2373
2269
|
return dataDir === void 0 ? {} : { dataDir };
|
|
2374
2270
|
}
|
|
2375
|
-
/** 注册 config 子命令(init/list/apply
|
|
2271
|
+
/** 注册 config 子命令(init/list/apply;-d 复用主命令 option)。 */
|
|
2376
2272
|
function registerConfigCommands(program) {
|
|
2377
2273
|
const configCmd = program.command("config").description("全局配置管理(napuketto.toml)");
|
|
2378
2274
|
configCmd.command("init").description("生成默认全局配置").action(async () => {
|
|
@@ -2415,7 +2311,7 @@ function registerConfigCommands(program) {
|
|
|
2415
2311
|
}
|
|
2416
2312
|
});
|
|
2417
2313
|
}
|
|
2418
|
-
/** 注册 supervisor 子命令(读主配置 accounts
|
|
2314
|
+
/** 注册 supervisor 子命令(读主配置 accounts 批量拉起;-d / --qq-path 复用主命令 option)。 */
|
|
2419
2315
|
function registerSupervisorCommand(program) {
|
|
2420
2316
|
program.command("supervisor").description("多账号编排(读全局配置 accounts 拉起子进程)").action(async () => {
|
|
2421
2317
|
try {
|
|
@@ -2439,7 +2335,7 @@ function opsAction(program, run) {
|
|
|
2439
2335
|
}
|
|
2440
2336
|
};
|
|
2441
2337
|
}
|
|
2442
|
-
/** 注册运维子命令(status/stop/restart
|
|
2338
|
+
/** 注册运维子命令(status/stop/restart;-q 复用主命令 option)。 */
|
|
2443
2339
|
function registerOpsCommands(program) {
|
|
2444
2340
|
program.command("status").description("查看各账号运行状态(读 runtime.json + 进程存活检测)").option("-q, --qq <uin>", "只看指定账号").action(opsAction(program, (dataRoot, arg) => cmdStatus(dataRoot, arg)));
|
|
2445
2341
|
program.command("stop").description("停止账号进程树(无 -q 停 supervisor 全部;-q 停单账号,supervisor 守护时会自动拉起)").option("-q, --qq <uin>", "目标账号(缺省停 supervisor + 全部账号)").action(opsAction(program, (dataRoot, arg) => cmdStop(dataRoot, arg)));
|
|
@@ -2471,21 +2367,9 @@ function main() {
|
|
|
2471
2367
|
}
|
|
2472
2368
|
// src/index.ts
|
|
2473
2369
|
/**
|
|
2474
|
-
* @napuketto/cli
|
|
2475
|
-
*
|
|
2476
|
-
*
|
|
2477
|
-
* commander 参数解析:
|
|
2478
|
-
* - 单账号:-q <uin> → runSingleAccount(自建宿主:标准 node + stub QQNT.dll,不拉起 QQ)
|
|
2479
|
-
* - 多账号:-q A -q B 或 supervisor 子命令 → runSupervisor(子进程编排)
|
|
2480
|
-
* - 无 -q:读全局配置 accounts(至少一个,账号必填)→ supervisor 拉起全部启用账号
|
|
2481
|
-
* - config 子命令:init / list / apply / validate(主配置管理)
|
|
2482
|
-
* - 退出码契约(2026-10-25):0 = 请求式优雅关停;78 = 配置校验失败(ConfigError
|
|
2483
|
-
* 一族,宿主消费,免重启);其余错误 = 1(handleActionError 统一出口)
|
|
2484
|
-
*
|
|
2485
|
-
* 路线 B(拉起 QQ + 注入)已淘汰(2026-08-07 用户拍板),相关入口已移除。
|
|
2486
|
-
*
|
|
2487
|
-
* 2026-10-25 模块收拢:本文件只保留进程入口;commander 装配在 cli/program.ts,
|
|
2488
|
-
* 用例在 commands/,配置领域在 config/,运行期在 runtime/。
|
|
2370
|
+
* @napuketto/cli 进程入口:唯一启动路径是自建宿主(标准 node + stub QQNT.dll),不拉起 QQ。
|
|
2371
|
+
* 无 -q 时读全局配置 accounts(账号必填,协议嵌在账号内)拉起全部启用账号。
|
|
2372
|
+
* 退出码契约(0 / 78 / 1)见 cli/error.ts。
|
|
2489
2373
|
*/
|
|
2490
2374
|
main();
|
|
2491
2375
|
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@napuketto/cli",
|
|
4
|
-
"version": "0.8.
|
|
4
|
+
"version": "0.8.10",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Command line interface for Napuketto",
|
|
7
7
|
"keywords": [
|
|
@@ -52,9 +52,9 @@
|
|
|
52
52
|
"start": "node ./dist/index.mjs"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@napuketto/adapter": "^0.24.
|
|
56
|
-
"@napuketto/kernel": "^0.19.
|
|
57
|
-
"@napuketto/loader": "^0.16.
|
|
55
|
+
"@napuketto/adapter": "^0.24.5",
|
|
56
|
+
"@napuketto/kernel": "^0.19.5",
|
|
57
|
+
"@napuketto/loader": "^0.16.7",
|
|
58
58
|
"@napuketto/network": "^0.5.1",
|
|
59
59
|
"commander": "^15.1.0",
|
|
60
60
|
"uqr": "^0.1.3"
|