@wkgeo/cli 0.0.0-stage → 1.1.0
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/CHANGELOG.md +223 -0
- package/LICENSE +21 -0
- package/README.md +1042 -3
- package/SKILL.md +927 -0
- package/lib/ai/client.mjs +161 -0
- package/lib/ai/config.mjs +221 -0
- package/lib/ai/context.mjs +468 -0
- package/lib/ai/log.mjs +53 -0
- package/lib/api.mjs +339 -0
- package/lib/args.mjs +235 -0
- package/lib/cli.mjs +168 -0
- package/lib/cms.mjs +330 -0
- package/lib/commands/ai.mjs +392 -0
- package/lib/commands/apps.mjs +116 -0
- package/lib/commands/auth.mjs +1288 -0
- package/lib/commands/billing.mjs +666 -0
- package/lib/commands/cms.mjs +2533 -0
- package/lib/commands/config-auth.mjs +621 -0
- package/lib/commands/config.mjs +434 -0
- package/lib/commands/dnscred.mjs +428 -0
- package/lib/commands/domainops.mjs +546 -0
- package/lib/commands/domainorder.mjs +439 -0
- package/lib/commands/import.mjs +164 -0
- package/lib/commands/index.mjs +541 -0
- package/lib/commands/logs.mjs +141 -0
- package/lib/commands/orders.mjs +444 -0
- package/lib/commands/overview.mjs +394 -0
- package/lib/commands/plugins.mjs +678 -0
- package/lib/commands/site.mjs +435 -0
- package/lib/commands/siteops.mjs +1012 -0
- package/lib/commands/sites.mjs +905 -0
- package/lib/commands/skill.mjs +119 -0
- package/lib/commands/template.mjs +258 -0
- package/lib/config.mjs +351 -0
- package/lib/credentials.mjs +160 -0
- package/lib/device-store.mjs +132 -0
- package/lib/device.mjs +464 -0
- package/lib/errors.mjs +299 -0
- package/lib/format.mjs +339 -0
- package/lib/http.mjs +71 -0
- package/lib/index.mjs +44 -0
- package/lib/local-import.mjs +185 -0
- package/lib/login.mjs +1638 -0
- package/lib/oauth-urls.mjs +163 -0
- package/lib/page.mjs +393 -0
- package/lib/pkce.mjs +48 -0
- package/lib/proxy.mjs +454 -0
- package/lib/qr.mjs +31 -0
- package/lib/runtime.mjs +650 -0
- package/lib/site-state.mjs +137 -0
- package/lib/tar.mjs +207 -0
- package/lib/template-package.mjs +522 -0
- package/package.json +53 -4
- package/wkgeo.mjs +17 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `skill` 命令:把**随包分发**的 `SKILL.md` 原文打印出来。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么要有它(老板原话 + 我们踩的坑)
|
|
5
|
+
* 手册确实随 npm 包分发了(`package.json` 的 `files` 白名单里就有 `SKILL.md`),
|
|
6
|
+
* 但**没有任何命令能把它取出来** —— AI 端只能自己去 `node_modules` 里翻:
|
|
7
|
+
* `npx` 场景下包落在 `~/.npm/_npx/<hash>/node_modules/@wkgeo/cli/`,
|
|
8
|
+
* 路径没法预期,还可能被 npx 缓存清理掉。换句话说 **"分发了但拿不到",约等于没分发**。
|
|
9
|
+
* 于是补上 `wkgeo skill`:一条命令拿到手册原文,AI 端不用猜路径、不用翻目录。
|
|
10
|
+
*
|
|
11
|
+
* ## 路径解析(⚠️ **绝不能**假设 cwd)
|
|
12
|
+
* 踩过的坑:最初想用 `process.cwd() + 'SKILL.md'`。但 AI 端几乎总是在**别的目录**里
|
|
13
|
+
* 跑这个 CLI(`npx` 更是如此),cwd 下根本没有这个文件 —— 只有安装目录里才有。
|
|
14
|
+
* 所以这里用**相对本模块**的 URL:`lib/commands/skill.mjs` → `../../SKILL.md` = 包根目录。
|
|
15
|
+
* 无论 npx 缓存 / 全局安装 / `npm link`,都指向同一份真实文件。
|
|
16
|
+
*
|
|
17
|
+
* ## 输出形态(**刻意的例外**,README §4 有记法说明)
|
|
18
|
+
* * 默认(**包括非终端/管道**)→ 纯文本原文,**不套 JSON 信封**。
|
|
19
|
+
* 理由:这份东西本身就是"给人/AI 读的 markdown"。套一层 JSON 之后 markdown 里的换行
|
|
20
|
+
* 会变成 `\n` 转义、AI 还得再解一层;而且 `wkgeo skill > SKILL.md` 应该直接得到
|
|
21
|
+
* 一份能读的文档,而不是 `{"ok":true,...}`。
|
|
22
|
+
* * `--json` → 包成标准信封 `{"ok":true,"command":"skill","data":{path,markdown,…}}`,
|
|
23
|
+
* 给"必须统一按 JSON 解析"的脚本/宿主用。
|
|
24
|
+
* * 出错照旧走通用规则(非终端默认 JSON 错误信封 + 退出码 1),且**不打原始堆栈**:
|
|
25
|
+
* 文件缺失是可预期的部署/裁剪问题,把 `ENOENT` 堆栈糊到用户和 AI 脸上没有意义。
|
|
26
|
+
*
|
|
27
|
+
* ## 鉴权
|
|
28
|
+
* 不需要登录、不发任何网络请求(纯本地读文件)—— 跟 `config` 一样,
|
|
29
|
+
* 所以未登录的机器上也能用它把手册取出来。
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import fs from 'node:fs';
|
|
33
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
34
|
+
|
|
35
|
+
import { CliError, EXIT } from '../errors.mjs';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 包根目录的 `SKILL.md`。
|
|
39
|
+
* `import.meta.url` = `<包根>/lib/commands/skill.mjs`,往上两级就是包根。
|
|
40
|
+
*/
|
|
41
|
+
export const SKILL_URL = new URL('../../SKILL.md', import.meta.url);
|
|
42
|
+
|
|
43
|
+
/** 允许传 URL(默认)或路径字符串(单测/排障时指到别处) */
|
|
44
|
+
export function resolveSkillUrl(src = SKILL_URL) {
|
|
45
|
+
if (src instanceof URL) return src;
|
|
46
|
+
return pathToFileURL(String(src));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 读手册原文。
|
|
51
|
+
*
|
|
52
|
+
* @param {URL|string} [src] 手册位置(默认包内 `SKILL.md`)
|
|
53
|
+
* @returns {{path:string, markdown:string, bytes:number, lines:number}}
|
|
54
|
+
* @throws {CliError} 文件不存在/读不了 → `kind="missing_skill"`,退出码 1,带中文 hint(无堆栈)
|
|
55
|
+
*/
|
|
56
|
+
export function readSkillMarkdown(src = SKILL_URL) {
|
|
57
|
+
const url = resolveSkillUrl(src);
|
|
58
|
+
let filePath = String(url);
|
|
59
|
+
try {
|
|
60
|
+
filePath = fileURLToPath(url);
|
|
61
|
+
} catch {
|
|
62
|
+
/* 给了非 file: 的 URL 时退回原样显示,别为了一个展示用的路径把命令搞崩 */
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
const markdown = fs.readFileSync(url, 'utf8');
|
|
66
|
+
return {
|
|
67
|
+
path: filePath,
|
|
68
|
+
markdown,
|
|
69
|
+
bytes: Buffer.byteLength(markdown, 'utf8'),
|
|
70
|
+
lines: markdown.split('\n').length,
|
|
71
|
+
};
|
|
72
|
+
} catch (e) {
|
|
73
|
+
const why = (e && e.code) || '读取失败';
|
|
74
|
+
throw new CliError(`当前安装包里没有手册文件(SKILL.md):${filePath}(${why})`, {
|
|
75
|
+
kind: 'missing_skill',
|
|
76
|
+
code: (e && e.code) || null,
|
|
77
|
+
exitCode: EXIT.ERROR,
|
|
78
|
+
hint:
|
|
79
|
+
'这不是命令用错了:多半是开发目录里只拷了 lib/、或分发包被裁剪过(正常 npm 安装包里一定有 SKILL.md)。' +
|
|
80
|
+
'可改用官方包 `npx -y @wkgeo/cli@latest skill` 取手册,或直接看仓库里的 cli/SKILL.md',
|
|
81
|
+
cause: e,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const skillCommand = {
|
|
87
|
+
name: 'skill',
|
|
88
|
+
summary: '打印给 AI 端的操作手册(随包分发)',
|
|
89
|
+
usage: 'wkgeo skill [--json]',
|
|
90
|
+
/**
|
|
91
|
+
* `--help` 里显式说明"本命令不吃通用输出约定",避免 AI 端按别的命令的习惯去猜:
|
|
92
|
+
* 它默认**永远**打 pure markdown(哪怕在管道里),只有显式 `--json` 才包信封。
|
|
93
|
+
*/
|
|
94
|
+
notes: [
|
|
95
|
+
'默认**永远输出手册原文(markdown)**,即使输出到管道/非终端也不套 JSON 信封 ——',
|
|
96
|
+
' 所以 `wkgeo skill > SKILL.md` 得到的就是一份可直接阅读的文档。',
|
|
97
|
+
'只有显式 `wkgeo skill --json` 才输出信封:{"ok":true,"command":"skill","data":{"path":…,"markdown":…,"bytes":…,"lines":…}}。',
|
|
98
|
+
'本命令不需要登录、不发任何网络请求(纯本地读包内的 SKILL.md)。',
|
|
99
|
+
],
|
|
100
|
+
async run(ctx) {
|
|
101
|
+
const { out } = ctx;
|
|
102
|
+
const info = readSkillMarkdown(ctx.skillUrl || SKILL_URL);
|
|
103
|
+
const data = { path: info.path, bytes: info.bytes, lines: info.lines, markdown: info.markdown };
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* ⚠️ 判断依据必须是**用户显式写的 `--json`**(`ctx.flags.json`),**不能**用 `out.json` ——
|
|
107
|
+
* 后者在"非终端"时会自动变成 true(全 CLI 的通用约定),那样管道里
|
|
108
|
+
* `wkgeo skill > SKILL.md` 就会灌进一坨 JSON 信封,正是这里要避免的。
|
|
109
|
+
* 也就是说:这条命令是"非终端自动 JSON"约定的**唯一例外**(登录除外,那是 JSONL 事件流)。
|
|
110
|
+
*/
|
|
111
|
+
if (ctx.flags.json === true) {
|
|
112
|
+
ctx.done(data);
|
|
113
|
+
return data;
|
|
114
|
+
}
|
|
115
|
+
// 原文直出:不套信封、不加标题、不打诊断(stdout 里只有手册本身,重定向即可存成文件)
|
|
116
|
+
out.plain(info.markdown.endsWith('\n') ? info.markdown : info.markdown + '\n');
|
|
117
|
+
return data;
|
|
118
|
+
},
|
|
119
|
+
};
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `wkgeo template init` —— 一条命令把基础库(`@wkgeo/base-template`)从 npm 下到本地。
|
|
3
|
+
*
|
|
4
|
+
* ## 老板的原话与定调(2026-10-30)
|
|
5
|
+
* 「基础模版可以开源到 github,或者国内的哪里吗,这样别人 init 初始化的时候就可以下载整套内容」
|
|
6
|
+
* → 「还是放 npm」。所以这里走 **npm 公开包 + npmmirror 镜像**,不做 GitHub 拉源码。
|
|
7
|
+
*
|
|
8
|
+
* ## 一条命令干的事
|
|
9
|
+
* 读 registry 元数据 → 选版本(`dist-tags.latest` 或 `--version`)→ 下 `dist.tarball`
|
|
10
|
+
* → **校验 `dist.integrity`(sha512)** → 解包(防路径穿越)→ 只取白名单
|
|
11
|
+
* → 落 `--to/<模板目录>`(默认 `./_base`)→ 跑包内 `tools/check-base.mjs` 自检。
|
|
12
|
+
*
|
|
13
|
+
* ## 缓存
|
|
14
|
+
* `~/.wkgeo/cache/base/<version>/`(`WKGEO_TEMPLATE_CACHE` 可改)。第二次 init **不发网络请求**;
|
|
15
|
+
* `--force` 强制重下;`--offline` 只读缓存,没缓存就报"哪两个 URL 该能访问"。
|
|
16
|
+
*
|
|
17
|
+
* ## 为什么不复用 `npm install`
|
|
18
|
+
* 1. 不装新依赖、不写 node_modules,也不让用户机器上的 `.npmrc`(可能有公司源/镜像 token)参与;
|
|
19
|
+
* 2. integrity 校验与解包安全策略要**由我们说了算**(拒绝符号链接、只落白名单);
|
|
20
|
+
* 3. 语义是"拷一套模板出来",不是"装一个库进来"——落地目录要能直接 `wkgeo template push`。
|
|
21
|
+
*
|
|
22
|
+
* 相关文档:`docs/功能-基础库分发.md`、`docs/规范-基础库开源内容边界.md`。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import fs from 'node:fs';
|
|
26
|
+
import path from 'node:path';
|
|
27
|
+
|
|
28
|
+
import { CliError, UsageError } from '../errors.mjs';
|
|
29
|
+
import {
|
|
30
|
+
PACKAGE_NAME,
|
|
31
|
+
cacheRootOf,
|
|
32
|
+
copyTree,
|
|
33
|
+
downloadToCache,
|
|
34
|
+
listCachedVersions,
|
|
35
|
+
packageDirName,
|
|
36
|
+
readCache,
|
|
37
|
+
readLatestPointer,
|
|
38
|
+
registryPlan,
|
|
39
|
+
metadataUrl,
|
|
40
|
+
selfCheck,
|
|
41
|
+
} from '../template-package.mjs';
|
|
42
|
+
|
|
43
|
+
function fmtBytes(n) {
|
|
44
|
+
if (!Number.isFinite(n)) return '?';
|
|
45
|
+
if (n < 1024) return `${n}B`;
|
|
46
|
+
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)}KB`;
|
|
47
|
+
return `${(n / 1024 / 1024).toFixed(2)}MB`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function readJsonSafe(file) {
|
|
51
|
+
try {
|
|
52
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 目标目录:`--to` 是**父目录**,落 `<to>/<模板目录名>`;`--to` 本身已是该名字时直接用 */
|
|
59
|
+
export function resolveTarget(toFlag, dirName, cwd = process.cwd()) {
|
|
60
|
+
const parent = path.resolve(cwd, toFlag || '.');
|
|
61
|
+
if (path.basename(parent) === dirName) return { parent, target: parent, nested: false };
|
|
62
|
+
return { parent, target: path.join(parent, dirName), nested: true };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const templateInitCommand = {
|
|
66
|
+
name: 'template init',
|
|
67
|
+
summary: '一条命令下载整套基础库(npm 公开包 @wkgeo/base-template,带 integrity 校验与本地缓存)',
|
|
68
|
+
usage:
|
|
69
|
+
'wkgeo template init [--to <父目录>] [--version <x.y.z>] [--registry npm|npmmirror] [--offline] [--force] [--json]\n' +
|
|
70
|
+
'wkgeo template init --to . # 落到 ./_base\n' +
|
|
71
|
+
'wkgeo template init --to templates # 落到 templates/_base',
|
|
72
|
+
args: [],
|
|
73
|
+
flags: {
|
|
74
|
+
to: { type: 'string', desc: '落到哪个**父目录**下(默认当前目录;最终目录是 <父目录>/_base)' },
|
|
75
|
+
version: { type: 'string', desc: '包版本 x.y.z(默认 dist-tags.latest;不带值的 --version 是全局「打印 CLI 版本」)' },
|
|
76
|
+
registry: { type: 'string', desc: '优先源:npm(=官方 registry.npmjs.org,默认)/ npmmirror(国内镜像);失败都自动回落另一个' },
|
|
77
|
+
offline: { type: 'boolean', desc: '只读本地缓存,不联网(没缓存就报错并贴出该访问的 URL)' },
|
|
78
|
+
force: { type: 'boolean', desc: '强制重下并覆盖已存在的目标目录' },
|
|
79
|
+
},
|
|
80
|
+
examples: [
|
|
81
|
+
'wkgeo template init',
|
|
82
|
+
'wkgeo template init --to templates',
|
|
83
|
+
'wkgeo template init --registry npmmirror',
|
|
84
|
+
'wkgeo template init --version 1.0.0 --force',
|
|
85
|
+
'wkgeo template init --offline --json',
|
|
86
|
+
],
|
|
87
|
+
notes: [
|
|
88
|
+
`包名固定 \`${PACKAGE_NAME}\`(scoped 公开包,MIT);镜像为 registry.npmmirror.com。`,
|
|
89
|
+
'下载后会校验 `dist.integrity`(sha512),不符**立即失败并删临时文件**,不会"换个源再试试"。',
|
|
90
|
+
'解包拒绝 `..` / 绝对路径 / 盘符 / 符号链接,且只落白名单条目(assets / starter / tools + 元数据)。',
|
|
91
|
+
'缓存 `~/.wkgeo/cache/base/<version>/`;第二次 init 命中缓存不发请求,`--force` 强制重下。',
|
|
92
|
+
'`--offline` 无缓存时退出码 1,错误里会贴出该能访问的两个 registry URL 与本地缓存路径。',
|
|
93
|
+
'装完自动跑包内 `tools/check-base.mjs starter`(自检不过 = 退出码 1,目录会留着便于排查)。',
|
|
94
|
+
'小坑:`--version` **必须带值**(`--version 1.0.0`);只写 `--version` 是全局行为「打印 CLI 版本」。',
|
|
95
|
+
],
|
|
96
|
+
async run(ctx) {
|
|
97
|
+
const { out, flags } = ctx;
|
|
98
|
+
const t0 = Date.now();
|
|
99
|
+
const log = (s) => out.line(String(s));
|
|
100
|
+
|
|
101
|
+
const cacheRoot = cacheRootOf(ctx.env || process.env);
|
|
102
|
+
const plan = registryPlan(flags.registry === undefined ? null : String(flags.registry), ctx.env || process.env);
|
|
103
|
+
const wanted = flags.version ? String(flags.version).trim() : null;
|
|
104
|
+
const offline = flags.offline === true;
|
|
105
|
+
const force = flags.force === true;
|
|
106
|
+
|
|
107
|
+
if (!out.json) {
|
|
108
|
+
out.head(`基础库模板下载(${PACKAGE_NAME})`);
|
|
109
|
+
log(` 缓存目录 ${cacheRoot}${offline ? ' · 离线模式(只读缓存)' : ''}`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let record = null;
|
|
113
|
+
let cacheHit = false;
|
|
114
|
+
|
|
115
|
+
// ---- 1) 离线:只读缓存 ----
|
|
116
|
+
if (offline) {
|
|
117
|
+
const versions = listCachedVersions(cacheRoot);
|
|
118
|
+
const pick = wanted || readLatestPointer(cacheRoot) || versions[versions.length - 1] || null;
|
|
119
|
+
const hit = pick ? readCache(cacheRoot, pick) : null;
|
|
120
|
+
if (!hit) {
|
|
121
|
+
const urls = plan.map((r) => metadataUrl(r.base)).join('\n - ');
|
|
122
|
+
throw new CliError(
|
|
123
|
+
`离线模式没有可用缓存${wanted ? `(要 ${wanted})` : ''}:${cacheRoot} 下没有完整版本。\n` +
|
|
124
|
+
` 可用版本:${versions.length ? versions.join(', ') : '(空)'}\n` +
|
|
125
|
+
` 联网执行一次即可填充缓存:wkgeo template init${wanted ? ` --version ${wanted}` : ''}\n` +
|
|
126
|
+
` 该能访问的 registry:\n - ${urls}`,
|
|
127
|
+
{ kind: 'cache', code: 'ECACHEMISS', hint: '去掉 --offline 重跑,或换 --registry npmmirror' },
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
cacheHit = true;
|
|
131
|
+
record = { ...hit.meta, cache: { hit: true, dir: hit.dir, pkgDir: hit.pkgDir }, timings: { totalMs: 0 }, registryLabel: hit.meta.registry_base };
|
|
132
|
+
if (!out.json) log(` ✓ 命中缓存 ${pick}(${hit.counted.files} 文件 / ${fmtBytes(hit.counted.bytes)})`);
|
|
133
|
+
} else {
|
|
134
|
+
// ---- 2) 在线:先看缓存(`--version` 指定版本,或缓存里的 latest 指针)----
|
|
135
|
+
if (!force) {
|
|
136
|
+
const candidate = wanted || readLatestPointer(cacheRoot);
|
|
137
|
+
const hit = candidate ? readCache(cacheRoot, candidate) : null;
|
|
138
|
+
if (hit) {
|
|
139
|
+
cacheHit = true;
|
|
140
|
+
record = { ...hit.meta, cache: { hit: true, dir: hit.dir, pkgDir: hit.pkgDir }, timings: { totalMs: 0 }, registryLabel: hit.meta.registry_base };
|
|
141
|
+
if (!out.json) log(` ✓ 命中缓存 ${candidate}(${hit.counted.files} 文件 / ${fmtBytes(hit.counted.bytes)})· 未联网(--force 可强制重下)`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (!record) {
|
|
145
|
+
if (!out.json) log(' 正在读 registry 元数据…');
|
|
146
|
+
record = await downloadToCache({
|
|
147
|
+
fetchImpl: ctx.fetchImpl,
|
|
148
|
+
plan,
|
|
149
|
+
wanted,
|
|
150
|
+
cacheRoot,
|
|
151
|
+
timeoutMs: Number(ctx.flags['http-timeout'] || 30000),
|
|
152
|
+
log: out.json ? null : log,
|
|
153
|
+
});
|
|
154
|
+
if (!out.json) {
|
|
155
|
+
log(` 来源 ${record.registryLabel} · ${record.version} · ${fmtBytes(record.size_bytes)} · ${record.file_count} 文件`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ---- 3) 落地 ----
|
|
161
|
+
const dirName = packageDirName(record.cache.pkgDir);
|
|
162
|
+
const { target } = resolveTarget(flags.to === undefined ? null : String(flags.to), dirName, process.cwd());
|
|
163
|
+
|
|
164
|
+
const existed = fs.existsSync(target) && fs.readdirSync(target).length > 0;
|
|
165
|
+
if (existed && !force) {
|
|
166
|
+
throw new CliError(`目标目录已存在且不为空:${target}`, {
|
|
167
|
+
kind: 'usage',
|
|
168
|
+
code: 'EEXIST',
|
|
169
|
+
exitCode: 2,
|
|
170
|
+
hint: '加 `--force` 覆盖,或换一个 `--to <父目录>`',
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
if (existed && force) {
|
|
174
|
+
if (!out.json) log(` 覆盖已有目录 ${target}`);
|
|
175
|
+
fs.rmSync(target, { recursive: true, force: true });
|
|
176
|
+
}
|
|
177
|
+
copyTree(record.cache.pkgDir, target);
|
|
178
|
+
|
|
179
|
+
const installed = (() => {
|
|
180
|
+
let files = 0;
|
|
181
|
+
let bytes = 0;
|
|
182
|
+
const walk = (d) => {
|
|
183
|
+
for (const it of fs.readdirSync(d, { withFileTypes: true })) {
|
|
184
|
+
const p = path.join(d, it.name);
|
|
185
|
+
if (it.isDirectory()) walk(p);
|
|
186
|
+
else if (it.isFile()) {
|
|
187
|
+
files += 1;
|
|
188
|
+
bytes += fs.statSync(p).size;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
walk(target);
|
|
193
|
+
return { files, bytes };
|
|
194
|
+
})();
|
|
195
|
+
|
|
196
|
+
// ---- 4) 自检 ----
|
|
197
|
+
const finalCheck = selfCheck(target);
|
|
198
|
+
|
|
199
|
+
const versionsFile = readJsonSafe(path.join(target, 'versions.json')) || {};
|
|
200
|
+
const versionFile = (() => {
|
|
201
|
+
try {
|
|
202
|
+
return fs.readFileSync(path.join(target, 'VERSION'), 'utf8').trim();
|
|
203
|
+
} catch {
|
|
204
|
+
return '';
|
|
205
|
+
}
|
|
206
|
+
})();
|
|
207
|
+
|
|
208
|
+
if (!out.json) {
|
|
209
|
+
if (!finalCheck.ok) {
|
|
210
|
+
out.warn(`自检未通过(${finalCheck.mode}):${finalCheck.detail}`);
|
|
211
|
+
}
|
|
212
|
+
out.ok(
|
|
213
|
+
`已安装到 ${target}(${installed.files} 文件 / ${fmtBytes(installed.bytes)})` +
|
|
214
|
+
`${cacheHit ? ' · 缓存命中' : ''}`,
|
|
215
|
+
);
|
|
216
|
+
log(` VERSION ${versionFile || '?'} · 内容版本 ${versionsFile.contentVersion || '?'} · 来自 ${record.registry_base || record.registry}`);
|
|
217
|
+
log(` 自检 ${finalCheck.mode}:${finalCheck.ok ? 'PASS' : 'FAIL'} — ${finalCheck.detail}`);
|
|
218
|
+
log(` 耗时 ${Date.now() - t0}ms`);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const payload = {
|
|
222
|
+
package: PACKAGE_NAME,
|
|
223
|
+
version: record.version,
|
|
224
|
+
content_version: versionsFile.contentVersion || null,
|
|
225
|
+
version_file: versionFile || null,
|
|
226
|
+
registry: record.registry || null,
|
|
227
|
+
registry_base: record.registry_base || record.registryLabel || null,
|
|
228
|
+
tarball: record.tarball || null,
|
|
229
|
+
integrity: record.integrity || null,
|
|
230
|
+
size_bytes: record.size_bytes || null,
|
|
231
|
+
file_count: record.file_count || null,
|
|
232
|
+
target,
|
|
233
|
+
files_written: installed.files,
|
|
234
|
+
bytes_written: installed.bytes,
|
|
235
|
+
cache_hit: cacheHit,
|
|
236
|
+
cache_dir: record.cache.dir,
|
|
237
|
+
self_check: {
|
|
238
|
+
ok: finalCheck.ok,
|
|
239
|
+
mode: finalCheck.mode,
|
|
240
|
+
detail: finalCheck.detail,
|
|
241
|
+
exit_code: finalCheck.exitCode,
|
|
242
|
+
},
|
|
243
|
+
elapsed_ms: Date.now() - t0,
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// 自检不过:目录留着便于排查,但**退出码必须非 0**(否则"装坏了"在 CI 里看不出来)
|
|
247
|
+
if (!finalCheck.ok) {
|
|
248
|
+
throw new CliError(`安装完成但自检未通过(${finalCheck.mode}):${finalCheck.detail}`, {
|
|
249
|
+
kind: 'verify',
|
|
250
|
+
code: 'ECHECKFAIL',
|
|
251
|
+
hint: `目录已保留在 ${target},可手工跑 \`node ${path.join(target, 'tools', 'check-base.mjs')} starter\` 看明细`,
|
|
252
|
+
raw: payload,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
ctx.done(payload);
|
|
257
|
+
},
|
|
258
|
+
};
|