@wkgeo/cli 1.0.0 → 1.1.1
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 +249 -203
- package/README.md +151 -19
- package/SKILL.md +108 -27
- package/lib/args.mjs +2 -2
- package/lib/asset-role.mjs +68 -0
- package/lib/cms.mjs +2 -2
- package/lib/commands/billing.mjs +56 -10
- package/lib/commands/cms.mjs +49 -13
- package/lib/commands/content.mjs +108 -0
- package/lib/commands/crawl.mjs +219 -0
- package/lib/commands/geo.mjs +1673 -0
- package/lib/commands/index.mjs +640 -528
- package/lib/commands/member.mjs +930 -0
- package/lib/commands/seo.mjs +1386 -0
- package/lib/commands/siteops.mjs +3 -3
- package/lib/commands/sites.mjs +21 -125
- package/lib/commands/template.mjs +1474 -0
- package/lib/config.mjs +16 -13
- package/lib/content-apply.mjs +664 -0
- package/lib/content-check.mjs +887 -0
- package/lib/content-contract.mjs +957 -0
- package/lib/errors.mjs +6 -0
- package/lib/format.mjs +5 -1
- package/lib/manifest-apply.mjs +1499 -0
- package/lib/manifest-schema.json +476 -0
- package/lib/manifest.mjs +1082 -0
- package/lib/oauth-urls.mjs +1 -1
- package/lib/runtime.mjs +5 -1
- package/lib/site-create.mjs +536 -0
- package/lib/tar.mjs +207 -0
- package/lib/template-anim.mjs +1338 -0
- package/lib/template-animation.mjs +232 -0
- package/lib/template-capture.mjs +700 -0
- package/lib/template-gen.mjs +1204 -0
- package/lib/template-pack.mjs +206 -0
- package/lib/template-package.mjs +522 -0
- package/lib/template-publish.mjs +733 -0
- package/lib/template-score.mjs +865 -0
- package/lib/template-validate.mjs +368 -0
- package/lib/template-verify.mjs +434 -0
- package/lib/templates-sync.mjs +616 -0
- package/package.json +2 -2
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 基础库 npm 分发 —— 下载 / 校验 / 缓存 / 落地(`wkgeo template init` 的全部脏活)。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么单独一层
|
|
5
|
+
* 命令层(`commands/template.mjs`)只负责参数与打印;这一层是**可被测试脚本直接调用**的纯逻辑:
|
|
6
|
+
* `deploy-web/test-base-init.mjs` 起一个本地 http registry,就能把「成功 / 篡改 / 回退 / 缓存 /
|
|
7
|
+
* 离线」五种路径全跑一遍,不需要真的发 npm 包。
|
|
8
|
+
*
|
|
9
|
+
* ## 安全口径(逐条都有对应实现,不是口号)
|
|
10
|
+
* 1. **元数据只从 https 的 registry 读**(`--registry` 三个别名 + 测试用的 env 覆盖);
|
|
11
|
+
* 2. **下载完先校验 `dist.integrity`(sha512)**,不符 → 删临时文件 + 立即失败,
|
|
12
|
+
* **不换下一个 registry 重试**(换源重试会把安全事件变成"偶发网络问题");
|
|
13
|
+
* 3. **解包防穿越**:`..` / 绝对路径 / 盘符 / 符号链接全部拒绝(实现见 `lib/tar.mjs`);
|
|
14
|
+
* 4. **只落白名单顶层目录**(assets / starter / tools + 几个元数据文件);
|
|
15
|
+
* 5. 缓存里的东西**也当作不可信**:命中时复核 `meta.json` 里的文件数。
|
|
16
|
+
*
|
|
17
|
+
* ## 缓存布局
|
|
18
|
+
* `<cacheRoot>/<version>/meta.json` —— 版本、integrity、文件数、下载时间、来源 registry
|
|
19
|
+
* `<cacheRoot>/<version>/pkg/` —— 已解包的内容(**只含白名单**)
|
|
20
|
+
* `<cacheRoot>` 默认 `~/.wkgeo/cache/base`,可用 `WKGEO_TEMPLATE_CACHE` 覆盖(测试用)。
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import crypto from 'node:crypto';
|
|
24
|
+
import fs from 'node:fs';
|
|
25
|
+
import os from 'node:os';
|
|
26
|
+
import path from 'node:path';
|
|
27
|
+
import { spawnSync } from 'node:child_process';
|
|
28
|
+
|
|
29
|
+
import { extractTarGz } from './tar.mjs';
|
|
30
|
+
import { CliError, UsageError } from './errors.mjs';
|
|
31
|
+
|
|
32
|
+
/** npm 包名(scoped:可管控、与 @wkgeo/cli 同 scope;registry 上 404 已验证可用) */
|
|
33
|
+
export const PACKAGE_NAME = '@wkgeo/base-template';
|
|
34
|
+
|
|
35
|
+
/** 三个 registry 别名(`--registry` 只认这三个) */
|
|
36
|
+
export const REGISTRY_ALIASES = ['npm', 'official', 'npmmirror'];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 别名 → 默认地址。
|
|
40
|
+
* `official` 与 `npm` 同址(老板/同事口头习惯两种都有人用,保留两个名字免得记)。
|
|
41
|
+
*/
|
|
42
|
+
export const REGISTRY_DEFAULT = {
|
|
43
|
+
npm: 'https://registry.npmjs.org',
|
|
44
|
+
official: 'https://registry.npmjs.org',
|
|
45
|
+
npmmirror: 'https://registry.npmmirror.com',
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** 包里允许落盘的顶层条目(= package.json 的 files 白名单;多余的**静默丢弃并记账**) */
|
|
49
|
+
export const ALLOWED_TOP = [
|
|
50
|
+
'assets',
|
|
51
|
+
'starter',
|
|
52
|
+
'tools',
|
|
53
|
+
'template.json',
|
|
54
|
+
'components.json',
|
|
55
|
+
'VERSION',
|
|
56
|
+
'versions.json',
|
|
57
|
+
'README.md',
|
|
58
|
+
'LICENSE',
|
|
59
|
+
'NOTICE',
|
|
60
|
+
'package.json',
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
const UA = `wkgeo-cli (+https://www.wkgeo.com/)`;
|
|
64
|
+
|
|
65
|
+
function sleep(ms) {
|
|
66
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 缓存根目录(测试用 WKGEO_TEMPLATE_CACHE 覆盖,避免污染真实 home) */
|
|
70
|
+
export function cacheRootOf(env = process.env) {
|
|
71
|
+
const custom = String(env.WKGEO_TEMPLATE_CACHE || '').trim();
|
|
72
|
+
if (custom) return path.resolve(custom);
|
|
73
|
+
return path.join(os.homedir(), '.wkgeo', 'cache', 'base');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 组装"先读谁、失败再读谁"的 registry 计划。
|
|
78
|
+
*
|
|
79
|
+
* 默认(`--registry` 不写)= `npm`:**先 registry.npmjs.org,失败自动回落 npmmirror**(国内镜像)。
|
|
80
|
+
* `--registry npmmirror` = 反过来(镜像优先,适合国内网络);两级顺序之外没有第三级。
|
|
81
|
+
*
|
|
82
|
+
* @param {string|null} flag `--registry` 的值(null = 没写)
|
|
83
|
+
* @param {object} env
|
|
84
|
+
* @returns {Array<{id:string,label:string,base:string}>}
|
|
85
|
+
*/
|
|
86
|
+
export function registryPlan(flag = null, env = process.env) {
|
|
87
|
+
const raw = String(flag || '').trim().toLowerCase();
|
|
88
|
+
if (raw && !REGISTRY_ALIASES.includes(raw)) {
|
|
89
|
+
throw new UsageError(`--registry 只认 ${REGISTRY_ALIASES.join(' / ')},收到:${flag}`);
|
|
90
|
+
}
|
|
91
|
+
const primary = raw === 'npmmirror' ? 'npmmirror' : 'npm';
|
|
92
|
+
const order = primary === 'npm' ? ['npm', 'npmmirror'] : ['npmmirror', 'npm'];
|
|
93
|
+
|
|
94
|
+
const overrides = {
|
|
95
|
+
npm: String(env.WKGEO_TEMPLATE_REGISTRY_NPM || '').trim(),
|
|
96
|
+
npmmirror: String(env.WKGEO_TEMPLATE_REGISTRY_NPMMIRROR || '').trim(),
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const plan = [];
|
|
100
|
+
for (const id of order) {
|
|
101
|
+
// `official` 只是 `npm` 的别名,覆盖只按 npm 那份算
|
|
102
|
+
const base = (overrides[id] || REGISTRY_DEFAULT[id]).replace(/\/+$/, '');
|
|
103
|
+
if (plan.some((one) => one.base === base)) continue; // 两个别名指同一地址时不要假回退
|
|
104
|
+
plan.push({ id, label: `${id}(${base})`, base });
|
|
105
|
+
}
|
|
106
|
+
return plan;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** registry metadata 里这个包的名字(scoped 包按 npm 文档写法:`@wkgeo%2Fbase-template`) */
|
|
110
|
+
export function metadataUrl(base) {
|
|
111
|
+
return `${base}/${PACKAGE_NAME.replace('/', '%2F')}`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 带重试的 GET(网络错 / 5xx / 408 / 429 重试 **2 次**,指数退避 300ms → 900ms)。
|
|
116
|
+
* 4xx(除 408/429)不重试 —— 包不存在就是不存在,重试只会让用户多等 6 秒。
|
|
117
|
+
*/
|
|
118
|
+
async function httpGet(fetchImpl, url, { timeoutMs = 20000, retries = 2, accept = '*/*', log = null } = {}) {
|
|
119
|
+
let lastErr = null;
|
|
120
|
+
for (let attempt = 0; attempt <= retries; attempt += 1) {
|
|
121
|
+
const ctrl = new AbortController();
|
|
122
|
+
const timer = setTimeout(() => ctrl.abort(), timeoutMs);
|
|
123
|
+
const t0 = Date.now();
|
|
124
|
+
try {
|
|
125
|
+
const res = await fetchImpl(url, {
|
|
126
|
+
signal: ctrl.signal,
|
|
127
|
+
redirect: 'follow',
|
|
128
|
+
headers: { 'user-agent': UA, accept, 'accept-encoding': 'identity' },
|
|
129
|
+
});
|
|
130
|
+
const ms = Date.now() - t0;
|
|
131
|
+
if (!res.ok) {
|
|
132
|
+
const err = new Error(`HTTP ${res.status}${res.statusText ? ` ${res.statusText}` : ''}`);
|
|
133
|
+
err.httpStatus = res.status;
|
|
134
|
+
err.ms = ms;
|
|
135
|
+
const retriable = res.status >= 500 || res.status === 408 || res.status === 429;
|
|
136
|
+
if (!retriable) {
|
|
137
|
+
err.fatal = true;
|
|
138
|
+
throw err;
|
|
139
|
+
}
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
142
|
+
return { res, ms, attempts: attempt + 1 };
|
|
143
|
+
} catch (e) {
|
|
144
|
+
if (e.ms === undefined) e.ms = Date.now() - t0;
|
|
145
|
+
if (e.fatal || attempt === retries) {
|
|
146
|
+
lastErr = e;
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
const wait = 300 * 3 ** attempt;
|
|
150
|
+
if (log) log(` 重试 ${attempt + 1}/2:${e.message || e}(${e.ms}ms)→ ${wait}ms 后再试`);
|
|
151
|
+
await sleep(wait);
|
|
152
|
+
lastErr = e;
|
|
153
|
+
} finally {
|
|
154
|
+
clearTimeout(timer);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
throw lastErr || new Error('请求失败');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 读 metadata(含 `dist-tags` / `versions`) */
|
|
161
|
+
export async function fetchMetadata(fetchImpl, base, opts = {}) {
|
|
162
|
+
const url = metadataUrl(base);
|
|
163
|
+
const { res, ms, attempts } = await httpGet(fetchImpl, url, { ...opts, accept: 'application/json' });
|
|
164
|
+
let json;
|
|
165
|
+
try {
|
|
166
|
+
json = await res.json();
|
|
167
|
+
} catch (e) {
|
|
168
|
+
throw new CliError(`registry 返回的不是 JSON(${url}):${e.message}`, { kind: 'network' });
|
|
169
|
+
}
|
|
170
|
+
return { json, url, ms, attempts };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 选版本:`--version` 精确命中,否则 `dist-tags.latest` */
|
|
174
|
+
export function pickVersion(meta, wanted = null) {
|
|
175
|
+
const versions = Object.keys((meta && meta.versions) || {});
|
|
176
|
+
if (!versions.length) throw new CliError('registry 元数据里没有任何版本(versions 为空)');
|
|
177
|
+
if (wanted) {
|
|
178
|
+
const one = meta.versions[wanted];
|
|
179
|
+
if (!one) {
|
|
180
|
+
throw new UsageError(
|
|
181
|
+
`registry 上没有 ${PACKAGE_NAME}@${wanted}(可用:${versions.slice(-12).join(', ')})` +
|
|
182
|
+
(meta['dist-tags']?.latest ? `;不写 --version 则用最新 ${meta['dist-tags'].latest}` : ''),
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
return { version: wanted, dist: one.dist || {}, source: 'flag' };
|
|
186
|
+
}
|
|
187
|
+
const latest = (meta['dist-tags'] && meta['dist-tags'].latest) || versions[versions.length - 1];
|
|
188
|
+
const one = meta.versions[latest];
|
|
189
|
+
if (!one) throw new CliError(`dist-tags.latest=${latest} 在 versions 里找不到,registry 元数据不一致`);
|
|
190
|
+
return { version: latest, dist: one.dist || {}, source: 'latest' };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 校验 sha512 integrity / sha1 shasum。
|
|
195
|
+
* @returns {{algo:string, integrity:string|null, shasum:string|null}}
|
|
196
|
+
* @throws {CliError} 不符(调用方负责删临时文件)
|
|
197
|
+
*/
|
|
198
|
+
export function verifyIntegrity(buf, dist = {}) {
|
|
199
|
+
const integrity = String(dist.integrity || '').trim();
|
|
200
|
+
const shasum = String(dist.shasum || '').trim();
|
|
201
|
+
|
|
202
|
+
if (integrity) {
|
|
203
|
+
const tokens = integrity.split(/\s+/).filter(Boolean);
|
|
204
|
+
let lastMismatch = null;
|
|
205
|
+
for (const token of tokens) {
|
|
206
|
+
const m = /^(sha\d+)-([A-Za-z0-9+/=]+)$/.exec(token);
|
|
207
|
+
if (!m) continue;
|
|
208
|
+
const algo = m[1];
|
|
209
|
+
const want = m[2];
|
|
210
|
+
const got = crypto.createHash(algo).update(buf).digest('base64');
|
|
211
|
+
if (got === want) return { algo, integrity, shasum: shasum || null };
|
|
212
|
+
lastMismatch = { algo, want, got };
|
|
213
|
+
}
|
|
214
|
+
const detail = lastMismatch
|
|
215
|
+
? `${lastMismatch.algo} 期望 ${lastMismatch.want.slice(0, 16)}… 实际 ${lastMismatch.got.slice(0, 16)}…`
|
|
216
|
+
: `integrity 字段无法解析(${integrity.slice(0, 40)})`;
|
|
217
|
+
throw new CliError(`integrity 校验失败:${detail}`, {
|
|
218
|
+
kind: 'integrity',
|
|
219
|
+
code: 'EINTEGRITY',
|
|
220
|
+
hint: '包内容与 registry 声明不一致,已删除临时文件。不要用 --force 绕过;换网络/镜像重下,仍不符请报告包被篡改',
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (shasum) {
|
|
225
|
+
const got = crypto.createHash('sha1').update(buf).digest('hex');
|
|
226
|
+
if (got === shasum) return { algo: 'sha1', integrity: null, shasum };
|
|
227
|
+
throw new CliError(`shasum 校验失败:期望 ${shasum.slice(0, 12)}… 实际 ${got.slice(0, 12)}…`, {
|
|
228
|
+
kind: 'integrity',
|
|
229
|
+
code: 'EINTEGRITY',
|
|
230
|
+
hint: '包内容与 registry 声明不一致,已删除临时文件(registry 没给 sha512,只能退 sha1)',
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
throw new CliError('registry 元数据既没有 dist.integrity 也没有 dist.shasum,无法校验 —— 拒绝安装', {
|
|
235
|
+
kind: 'integrity',
|
|
236
|
+
code: 'EINTEGRITY',
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** 递归数文件(缓存命中时复核用) */
|
|
241
|
+
export function countFiles(dir) {
|
|
242
|
+
let n = 0;
|
|
243
|
+
let bytes = 0;
|
|
244
|
+
const walk = (d) => {
|
|
245
|
+
let items = [];
|
|
246
|
+
try {
|
|
247
|
+
items = fs.readdirSync(d, { withFileTypes: true });
|
|
248
|
+
} catch {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
for (const it of items) {
|
|
252
|
+
const p = path.join(d, it.name);
|
|
253
|
+
if (it.isDirectory()) walk(p);
|
|
254
|
+
else if (it.isFile()) {
|
|
255
|
+
n += 1;
|
|
256
|
+
try {
|
|
257
|
+
bytes += fs.statSync(p).size;
|
|
258
|
+
} catch {
|
|
259
|
+
/* 忽略 */
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
walk(dir);
|
|
265
|
+
return { files: n, bytes };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function readJsonSafe(file) {
|
|
269
|
+
try {
|
|
270
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
271
|
+
} catch {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** 缓存命中判定:`meta.json` + `pkg/` 都在,且文件数对得上 */
|
|
277
|
+
export function readCache(cacheRoot, version) {
|
|
278
|
+
const dir = path.join(cacheRoot, version);
|
|
279
|
+
const metaFile = path.join(dir, 'meta.json');
|
|
280
|
+
const pkgDir = path.join(dir, 'pkg');
|
|
281
|
+
const meta = readJsonSafe(metaFile);
|
|
282
|
+
if (!meta || !fs.existsSync(pkgDir)) return null;
|
|
283
|
+
if (meta.version !== version) return null;
|
|
284
|
+
const counted = countFiles(pkgDir);
|
|
285
|
+
if (Number(meta.file_count) !== counted.files) return null; // 缓存被手工改过 → 当没命中
|
|
286
|
+
return { dir, metaFile, pkgDir, meta, counted };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* `latest` 指针:不带 `--version` 时也要能命中缓存,否则每次 init 都得先联网读 metadata
|
|
291
|
+
* (那就谈不上"第二次不发请求"了)。文件 = `<cacheRoot>/latest.json`。
|
|
292
|
+
*/
|
|
293
|
+
export function readLatestPointer(cacheRoot) {
|
|
294
|
+
const one = readJsonSafe(path.join(cacheRoot, 'latest.json'));
|
|
295
|
+
return one && typeof one.version === 'string' ? one.version : null;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function writeLatestPointer(cacheRoot, version) {
|
|
299
|
+
try {
|
|
300
|
+
fs.mkdirSync(cacheRoot, { recursive: true });
|
|
301
|
+
fs.writeFileSync(
|
|
302
|
+
path.join(cacheRoot, 'latest.json'),
|
|
303
|
+
`${JSON.stringify({ version, savedAt: new Date().toISOString() }, null, 2)}\n`,
|
|
304
|
+
);
|
|
305
|
+
} catch {
|
|
306
|
+
/* 指针写不了不影响主流程(最多下次多联网一次) */
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** 缓存里已有的版本(按语义版本排序,给 `--offline` 与错误提示用) */
|
|
311
|
+
export function listCachedVersions(cacheRoot) {
|
|
312
|
+
let names = [];
|
|
313
|
+
try {
|
|
314
|
+
names = fs
|
|
315
|
+
.readdirSync(cacheRoot, { withFileTypes: true })
|
|
316
|
+
.filter((d) => d.isDirectory() && /^\d+\.\d+\.\d+/.test(d.name))
|
|
317
|
+
.map((d) => d.name);
|
|
318
|
+
} catch {
|
|
319
|
+
return [];
|
|
320
|
+
}
|
|
321
|
+
const cmp = (a, b) => {
|
|
322
|
+
const pa = a.split('.').map(Number);
|
|
323
|
+
const pb = b.split('.').map(Number);
|
|
324
|
+
for (let i = 0; i < 3; i += 1) if ((pa[i] || 0) !== (pb[i] || 0)) return (pa[i] || 0) - (pb[i] || 0);
|
|
325
|
+
return 0;
|
|
326
|
+
};
|
|
327
|
+
return names.sort(cmp);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 下一份包(含两级 registry 回退)并写缓存。
|
|
332
|
+
*
|
|
333
|
+
* @param {object} o
|
|
334
|
+
* @param {Function} o.fetchImpl
|
|
335
|
+
* @param {Array} o.plan `registryPlan()` 的结果
|
|
336
|
+
* @param {string|null} o.wanted 指定版本
|
|
337
|
+
* @param {string} o.cacheRoot
|
|
338
|
+
* @param {number} [o.timeoutMs]
|
|
339
|
+
* @param {(s:string)=>void} [o.log] 人类日志出口(JSON 模式下会被命令层丢掉)
|
|
340
|
+
* @returns {Promise<object>} {version, dist, tarball, integrity, sizeBytes, fileCount, registry, timings, cache}
|
|
341
|
+
*/
|
|
342
|
+
export async function downloadToCache({ fetchImpl, plan, wanted = null, cacheRoot, timeoutMs = 20000, log = null }) {
|
|
343
|
+
const failures = [];
|
|
344
|
+
for (const reg of plan) {
|
|
345
|
+
const t0 = Date.now();
|
|
346
|
+
let meta;
|
|
347
|
+
try {
|
|
348
|
+
meta = await fetchMetadata(fetchImpl, reg.base, { timeoutMs, log });
|
|
349
|
+
} catch (e) {
|
|
350
|
+
const ms = Date.now() - t0;
|
|
351
|
+
const reason = e.httpStatus ? `HTTP ${e.httpStatus}` : e.message || String(e);
|
|
352
|
+
failures.push(`${reg.label} → ${reason}(${ms}ms)`);
|
|
353
|
+
if (log) log(` ✗ ${reg.label} 读元数据失败:${reason}(${ms}ms)`);
|
|
354
|
+
if (e.httpStatus === 404) {
|
|
355
|
+
// 404 = 这个源上没有这个包(而不是"源挂了");下一级照试,但把话说清楚
|
|
356
|
+
if (log) log(' 原因:源上没有该包(404),继续下一个源');
|
|
357
|
+
}
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const picked = pickVersion(meta.json, wanted);
|
|
362
|
+
if (log) {
|
|
363
|
+
log(
|
|
364
|
+
` ✓ ${reg.label} 元数据 ${meta.ms}ms(${meta.attempts} 次请求)· ${PACKAGE_NAME}@${picked.version}` +
|
|
365
|
+
`${picked.source === 'latest' ? '(dist-tags.latest)' : '(--version 指定)'}`,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const tarball = picked.dist.tarball;
|
|
370
|
+
if (!tarball) {
|
|
371
|
+
failures.push(`${reg.label} → 元数据缺 dist.tarball`);
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// 下载到临时文件:校验通过再解包,失败立刻删(不留半个包在缓存里)
|
|
376
|
+
const tmpDir = path.join(cacheRoot, '.tmp');
|
|
377
|
+
fs.mkdirSync(tmpDir, { recursive: true });
|
|
378
|
+
const tmpFile = path.join(tmpDir, `${picked.version}-${process.pid}-${Date.now()}.tgz`);
|
|
379
|
+
const t1 = Date.now();
|
|
380
|
+
let buf;
|
|
381
|
+
try {
|
|
382
|
+
const got = await httpGet(fetchImpl, tarball, { timeoutMs, log });
|
|
383
|
+
buf = Buffer.from(await got.res.arrayBuffer());
|
|
384
|
+
fs.writeFileSync(tmpFile, buf);
|
|
385
|
+
const dlMs = Date.now() - t1;
|
|
386
|
+
if (log) log(` ✓ 下载 ${(buf.length / 1024).toFixed(1)}KB ${dlMs}ms ← ${tarball}`);
|
|
387
|
+
|
|
388
|
+
// ★ integrity 不符在这里抛:**不 try 下一个 registry**,直接冒到命令层
|
|
389
|
+
const checked = verifyIntegrity(buf, picked.dist);
|
|
390
|
+
if (log) log(` ✓ integrity 校验通过(${checked.algo})`);
|
|
391
|
+
|
|
392
|
+
const finalDir = path.join(cacheRoot, picked.version);
|
|
393
|
+
const stageDir = path.join(cacheRoot, `.stage-${picked.version}-${process.pid}-${Date.now()}`);
|
|
394
|
+
fs.rmSync(stageDir, { recursive: true, force: true });
|
|
395
|
+
const extracted = extractTarGz(buf, path.join(stageDir, 'pkg'), {
|
|
396
|
+
stripPrefix: 'package/',
|
|
397
|
+
allowTop: ALLOWED_TOP,
|
|
398
|
+
});
|
|
399
|
+
const counts = countFiles(path.join(stageDir, 'pkg'));
|
|
400
|
+
const metaOut = {
|
|
401
|
+
package: PACKAGE_NAME,
|
|
402
|
+
version: picked.version,
|
|
403
|
+
integrity: checked.integrity,
|
|
404
|
+
shasum: checked.shasum,
|
|
405
|
+
algo: checked.algo,
|
|
406
|
+
tarball,
|
|
407
|
+
registry: reg.id,
|
|
408
|
+
registry_base: reg.base,
|
|
409
|
+
size_bytes: buf.length,
|
|
410
|
+
file_count: counts.files,
|
|
411
|
+
content_bytes: counts.bytes,
|
|
412
|
+
skipped_entries: extracted.skipped.length,
|
|
413
|
+
downloaded_at: new Date().toISOString(),
|
|
414
|
+
};
|
|
415
|
+
fs.writeFileSync(path.join(stageDir, 'meta.json'), `${JSON.stringify(metaOut, null, 2)}\n`);
|
|
416
|
+
|
|
417
|
+
fs.rmSync(finalDir, { recursive: true, force: true });
|
|
418
|
+
fs.renameSync(stageDir, finalDir);
|
|
419
|
+
writeLatestPointer(cacheRoot, picked.version);
|
|
420
|
+
|
|
421
|
+
return {
|
|
422
|
+
...metaOut,
|
|
423
|
+
cache: { hit: false, dir: finalDir, pkgDir: path.join(finalDir, 'pkg') },
|
|
424
|
+
timings: { metaMs: meta.ms, downloadMs: dlMs, totalMs: Date.now() - t0 },
|
|
425
|
+
registryLabel: reg.label,
|
|
426
|
+
};
|
|
427
|
+
} catch (e) {
|
|
428
|
+
// integrity 错 / 解包错:删临时文件后**直接失败**(不换源重试)
|
|
429
|
+
try {
|
|
430
|
+
fs.rmSync(tmpFile, { force: true });
|
|
431
|
+
} catch {
|
|
432
|
+
/* 忽略 */
|
|
433
|
+
}
|
|
434
|
+
if (e.kind === 'integrity' || e.code === 'EINTEGRITY') throw e;
|
|
435
|
+
const ms = Date.now() - t0;
|
|
436
|
+
const reason = e.httpStatus ? `HTTP ${e.httpStatus}` : e.message || String(e);
|
|
437
|
+
failures.push(`${reg.label} → ${reason}(${ms}ms)`);
|
|
438
|
+
if (log) log(` ✗ ${reg.label} 下载失败:${reason}(${ms}ms)`);
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
throw new CliError(
|
|
444
|
+
`下载 ${PACKAGE_NAME} 失败,所有 registry 都不可用:\n - ${failures.join('\n - ')}`,
|
|
445
|
+
{
|
|
446
|
+
kind: 'network',
|
|
447
|
+
hint:
|
|
448
|
+
'检查网络/代理;国内网络可试 `--registry npmmirror`;' +
|
|
449
|
+
`也可手工用浏览器打开 ${metadataUrl(plan[0] ? plan[0].base : REGISTRY_DEFAULT.npm)} 确认源是否可达`,
|
|
450
|
+
},
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** 把 `srcDir` 整棵拷到 `destDir`(destDir 必须已清空或不存在) */
|
|
455
|
+
export function copyTree(srcDir, destDir) {
|
|
456
|
+
fs.mkdirSync(destDir, { recursive: true });
|
|
457
|
+
const walk = (from, to) => {
|
|
458
|
+
for (const it of fs.readdirSync(from, { withFileTypes: true })) {
|
|
459
|
+
const a = path.join(from, it.name);
|
|
460
|
+
const b = path.join(to, it.name);
|
|
461
|
+
if (it.isDirectory()) {
|
|
462
|
+
fs.mkdirSync(b, { recursive: true });
|
|
463
|
+
walk(a, b);
|
|
464
|
+
} else if (it.isFile()) {
|
|
465
|
+
fs.copyFileSync(a, b);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
walk(srcDir, destDir);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* 装完自检:
|
|
474
|
+
* 1. 有 `tools/check-base.mjs` 且带 `starter/` → 真跑它(这是基础库自己的验收脚本);
|
|
475
|
+
* 2. 否则退回最弱的检查:`components.json.files[]` 是否都在。
|
|
476
|
+
* @returns {{ok:boolean,mode:string,detail:string,exitCode:number|null,stdout:string}}
|
|
477
|
+
*/
|
|
478
|
+
export function selfCheck(targetDir) {
|
|
479
|
+
const checker = path.join(targetDir, 'tools', 'check-base.mjs');
|
|
480
|
+
const starter = path.join(targetDir, 'starter');
|
|
481
|
+
if (fs.existsSync(checker) && fs.existsSync(starter)) {
|
|
482
|
+
const r = spawnSync(process.execPath, [checker, 'starter', '--quiet'], {
|
|
483
|
+
cwd: targetDir,
|
|
484
|
+
encoding: 'utf8',
|
|
485
|
+
timeout: 60000,
|
|
486
|
+
});
|
|
487
|
+
const outText = `${r.stdout || ''}${r.stderr || ''}`.trim();
|
|
488
|
+
const tail = outText
|
|
489
|
+
.split('\n')
|
|
490
|
+
.map((s) => s.trim())
|
|
491
|
+
.filter(Boolean)
|
|
492
|
+
.slice(-3)
|
|
493
|
+
.join(' / ');
|
|
494
|
+
return {
|
|
495
|
+
ok: r.status === 0,
|
|
496
|
+
mode: 'check-base',
|
|
497
|
+
detail: tail || `exit ${r.status}`,
|
|
498
|
+
exitCode: r.status,
|
|
499
|
+
stdout: outText,
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const compFile = path.join(targetDir, 'components.json');
|
|
504
|
+
const comp = readJsonSafe(compFile);
|
|
505
|
+
if (!comp) return { ok: false, mode: 'components.json', detail: 'components.json 缺失或不是 JSON', exitCode: null, stdout: '' };
|
|
506
|
+
const files = Array.isArray(comp.files) ? comp.files : [];
|
|
507
|
+
const missing = files.filter((f) => !fs.existsSync(path.join(targetDir, 'assets', f)));
|
|
508
|
+
return {
|
|
509
|
+
ok: missing.length === 0,
|
|
510
|
+
mode: 'components.json',
|
|
511
|
+
detail: missing.length ? `缺 ${missing.length} 个 assets 文件:${missing.slice(0, 3).join(', ')}` : `assets 文件 ${files.length} 个齐全`,
|
|
512
|
+
exitCode: null,
|
|
513
|
+
stdout: '',
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** 目标目录名:包自带 `template.json` 的 key(`_base`),兜底 `_base` */
|
|
518
|
+
export function packageDirName(pkgDir) {
|
|
519
|
+
const tpl = readJsonSafe(path.join(pkgDir, 'template.json'));
|
|
520
|
+
const key = tpl && typeof tpl.key === 'string' ? tpl.key.trim() : '';
|
|
521
|
+
return key || '_base';
|
|
522
|
+
}
|