dsh-git-push 2.18.0 → 2.18.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/lib/npm/smoke.js +67 -8
- package/lib/npm/unpublish.js +31 -7
- package/lib/self/index.js +1 -1
- package/package.json +1 -1
package/lib/npm/smoke.js
CHANGED
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
* 不做的事:不跑单测、不启宿主。它只回答「包本身能不能被正常消费」;
|
|
17
17
|
* 「宿主能不能加载插件」属于隔离实例验证(test_env)的范畴,成本高得多,不做进默认门禁。
|
|
18
18
|
*/
|
|
19
|
+
import { extname } from "node:path";
|
|
20
|
+
|
|
21
|
+
/** 能被 Node 直接当模块加载的扩展名(空扩展名按无后缀 ESM/CJS 处理)。 */
|
|
22
|
+
const MODULE_EXTS = new Set(['', '.js', '.mjs', '.cjs', '.jsx', '.ts', '.tsx']);
|
|
19
23
|
import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
20
24
|
import { join } from 'node:path';
|
|
21
25
|
import { tmpdir } from 'node:os';
|
|
@@ -31,6 +35,68 @@ const SMOKE_TIMEOUT_MS = 240000;
|
|
|
31
35
|
* 没有 exports 时退化为包名根入口。
|
|
32
36
|
*/
|
|
33
37
|
/** 浏览器端入口(如 `./client`):只能在宿主前端环境加载,Node 里 import 必然失败,冒烟时跳过。 */
|
|
38
|
+
/**
|
|
39
|
+
* 从 package.json 里挑出「本次冒烟该 import 哪些公开面」。
|
|
40
|
+
*
|
|
41
|
+
* 【判据】
|
|
42
|
+
* · `exports` 的点出口(`"."`)= 主入口,**必须测**;没有 main/exports 的纯 CLI 包没有库入口,跳过
|
|
43
|
+
* · 子出口里只挑能当模块加载的目标:`.yml`/`.json`/`.css`/图片等资源虽然也登记在 exports,
|
|
44
|
+
* 但对它们 import 会得到 ERR_UNKNOWN_FILE_EXTENSION —— 那是误判,不是包的问题
|
|
45
|
+
* · 浏览器专属出口(前端 bundle)不在这里加载,单独归到 browserEntries 只是为了让报告如实列出
|
|
46
|
+
*
|
|
47
|
+
* @param {object} pkg package.json 内容
|
|
48
|
+
* @returns {{nodeEntries: string[], browserEntries: string[]}} 可直接交给 `import(spec)` 的规格名
|
|
49
|
+
*/
|
|
50
|
+
export function selectImportSpecs(pkg) {
|
|
51
|
+
const name = String(pkg?.name || '');
|
|
52
|
+
const nodeEntries = [];
|
|
53
|
+
const browserEntries = [];
|
|
54
|
+
if (!name) return { nodeEntries, browserEntries };
|
|
55
|
+
const hasLibraryEntry = Boolean(pkg?.main) || Boolean(pkg?.exports);
|
|
56
|
+
const exportMap = (pkg?.exports && typeof pkg.exports === 'object') ? pkg.exports : { '.': null };
|
|
57
|
+
for (const key of Object.keys(exportMap)) {
|
|
58
|
+
// 清单不是模块;对它 import 没有意义
|
|
59
|
+
if (key === './package.json') continue;
|
|
60
|
+
const spec = key === '.' ? name : `${name}/${key.slice(2)}`;
|
|
61
|
+
if (key !== '.' && isBrowserEntry(key)) { browserEntries.push(spec); continue; }
|
|
62
|
+
if (key === '.' && !hasLibraryEntry) continue;
|
|
63
|
+
const target = exportMap[key] === undefined || exportMap[key] === null
|
|
64
|
+
? (key === '.' ? pkg.main : key)
|
|
65
|
+
: exportMap[key];
|
|
66
|
+
if (!isModuleTarget(target)) continue;
|
|
67
|
+
nodeEntries.push(spec);
|
|
68
|
+
}
|
|
69
|
+
return { nodeEntries, browserEntries };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 判断一个 exports 目标是否可能是「可被 Node 加载的模块」。
|
|
74
|
+
*
|
|
75
|
+
* 【为什么需要】exports 不只为 JS 服务:资源文件(.yml/.json/.css/图片)也常登记在
|
|
76
|
+
* exports 里供消费方按子路径取用。冒烟若对这些子路径执行 import,会得到
|
|
77
|
+
* ERR_UNKNOWN_FILE_EXTENSION —— 那是包被误判,不是包有问题。
|
|
78
|
+
*
|
|
79
|
+
* @param {unknown} value exports 的值(字符串、条件对象,或 null)
|
|
80
|
+
* @returns {boolean} 是否值得尝试 import
|
|
81
|
+
*/
|
|
82
|
+
function isModuleTarget(value) {
|
|
83
|
+
// 没有显式目标(如 {'.': null}):按包名 import 主入口,交给 Node 判定
|
|
84
|
+
if (value === undefined || value === null) return true;
|
|
85
|
+
if (typeof value === 'string') {
|
|
86
|
+
if (value.endsWith('/')) return true; // 目录映射
|
|
87
|
+
const ext = extname(value).toLowerCase();
|
|
88
|
+
return MODULE_EXTS.has(ext);
|
|
89
|
+
}
|
|
90
|
+
if (typeof value === 'object') {
|
|
91
|
+
// 条件导出:取第一个存在的条件继续判断(import 优先,其次 default/require/node)
|
|
92
|
+
for (const condition of ['import', 'default', 'require', 'node']) {
|
|
93
|
+
if (value[condition] !== undefined) return isModuleTarget(value[condition]);
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
|
|
34
100
|
export function isBrowserEntry(key) {
|
|
35
101
|
// `./client`、`./client/index`、`./lib/client.js` 都算浏览器端入口
|
|
36
102
|
return /(^|\/)client(\/|\.js)?(\/|$)|browser/i.test(String(key).replace(/^\.\//, ''));
|
|
@@ -128,14 +194,7 @@ export async function smokeTestPackage({ dir = '', workspaceRoot = '', registry
|
|
|
128
194
|
const probe = join(consumer, 'probe.mjs');
|
|
129
195
|
const deps = Object.keys(pkg.dependencies || {});
|
|
130
196
|
// nodeEntries = 可在 Node 里加载的公开面;browserEntries = 仅前端可加载(跳过但如实列出)
|
|
131
|
-
const nodeEntries =
|
|
132
|
-
const browserEntries = [];
|
|
133
|
-
for (const key of Object.keys(pkg.exports && typeof pkg.exports === 'object' ? pkg.exports : { '.': null })) {
|
|
134
|
-
const spec = key === '.' ? name : `${name}/${key.slice(2)}`;
|
|
135
|
-
if (key !== '.' && isBrowserEntry(key)) browserEntries.push(spec);
|
|
136
|
-
else if (!key.startsWith('./') || key === './package.json') continue;
|
|
137
|
-
else nodeEntries.push(spec);
|
|
138
|
-
}
|
|
197
|
+
const { nodeEntries, browserEntries } = selectImportSpecs(pkg);
|
|
139
198
|
writeFileSync(probe, [
|
|
140
199
|
'import { createRequire } from "node:module";',
|
|
141
200
|
'import { existsSync } from "node:fs";',
|
package/lib/npm/unpublish.js
CHANGED
|
@@ -96,8 +96,10 @@ export async function unpublishPackage({
|
|
|
96
96
|
name = '', version = '', wholePackage = false,
|
|
97
97
|
registry = DEFAULT_REGISTRY, workspaceRoot = '',
|
|
98
98
|
dryRun = true, confirm = false, timeoutMs = 120000,
|
|
99
|
+
// 可注入(测试用):npm 执行器、registry 查询、凭据
|
|
100
|
+
runNpmImpl = runNpm, fetchImpl = globalThis.fetch, token = '',
|
|
99
101
|
} = {}) {
|
|
100
|
-
const pre = await preflightUnpublish({ name, version, wholePackage, registry });
|
|
102
|
+
const pre = await preflightUnpublish({ name, version, wholePackage, registry, fetchImpl });
|
|
101
103
|
if (!pre.ok) return { ok: false, blocked: 'preflight-failed', error: pre.error };
|
|
102
104
|
if (dryRun) {
|
|
103
105
|
return {
|
|
@@ -109,17 +111,39 @@ export async function unpublishPackage({
|
|
|
109
111
|
if (confirm !== true) {
|
|
110
112
|
return { ok: false, blocked: 'need-confirm', ...pre, error: '真实撤包不可逆:需同时传 dryRun:false 与 confirm:true' };
|
|
111
113
|
}
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
+
const tokenValue = token || (() => { try { return readNpmToken({ workspaceRoot }); } catch { return ''; } })();
|
|
115
|
+
// 没有凭据必须当场拒绝:实测 npm 在**无认证**时会对 unpublish 发 PUT 拿到 404,
|
|
116
|
+
// 但**退出码仍是 0** 并打印「- pkg@ver」,看起来像撤成功(历史假成功就是这么来的)。
|
|
117
|
+
if (!tokenValue) {
|
|
118
|
+
return { ...pre, ok: false, blocked: 'no-token', error: '插件配置里没有可用的 npmToken(或未生效),无法撤包;请先配置 npm 凭据' };
|
|
119
|
+
}
|
|
120
|
+
const rc = writeNpmrc({ workspaceRoot, registry, token: tokenValue });
|
|
114
121
|
if (!rc.ok) return { ok: false, error: rc.error || 'npmrc 写入失败' };
|
|
115
122
|
const args = ['unpublish', wholePackage ? name : `${name}@${pre.version}`, ...npmArgs({ npmrcFile: rc.file, registry })];
|
|
116
123
|
if (wholePackage) args.push('--force');
|
|
117
|
-
const r =
|
|
124
|
+
const r = runNpmImpl(args, { timeoutMs, env: { NO_UPDATE_NOTIFIER: '1' } });
|
|
125
|
+
const npmText = String(r.error || r.stderr || r.stdout || '');
|
|
118
126
|
if (!r.ok) {
|
|
127
|
+
// granular token 的 2FA 限制是最常见的硬阻塞,单独给出可执行指引
|
|
128
|
+
const twoFactorBlocked = /granular access token/i.test(npmText) || /bypass two-factor/i.test(npmText);
|
|
129
|
+
return {
|
|
130
|
+
...pre, ok: false,
|
|
131
|
+
error: `npm unpublish 失败:${npmText.slice(0, 400)}`,
|
|
132
|
+
...(twoFactorBlocked
|
|
133
|
+
? { hint: '该 token 是「绕过 2FA 的 granular token」,npm 禁止它执行撤包。改用:① npm 网站手动撤(需 2FA);② 换 classic token 并带 --otp;③ 本机 npm login 后执行' }
|
|
134
|
+
: { hint: '若提示 72h/依赖者/下载量限制,说明不满足官方撤包条件——改用 npm deprecate 让使用者看到警告而不破坏构建' }),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
// **关键**:npm 的 unpublish 在若干失败情形下仍会 exit 0 并打印「- pkg@ver」。
|
|
138
|
+
// 唯一可信的判据是回读 registry:版本真的没了才算成功。
|
|
139
|
+
const after = await preflightUnpublish({ name, version: pre.version, wholePackage, registry, fetchImpl });
|
|
140
|
+
const stillThere = after.ok && !wholePackage && Array.isArray(after.versions) && after.versions.includes(pre.version);
|
|
141
|
+
if (stillThere) {
|
|
119
142
|
return {
|
|
120
|
-
ok: false,
|
|
121
|
-
error: `npm
|
|
122
|
-
|
|
143
|
+
...pre, ok: false, verified: false,
|
|
144
|
+
error: `npm 退出码为 0,但 registry 里 ${name}@${pre.version} 仍在——撤包未生效`,
|
|
145
|
+
npmOutput: npmText.slice(-600),
|
|
146
|
+
hint: '常见原因:token 无权限/需 2FA/被 granular token 策略禁止。请改用 npm 网站或 classic token + --otp',
|
|
123
147
|
};
|
|
124
148
|
}
|
|
125
149
|
return {
|
package/lib/self/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
export const VERSION = '2.18.
|
|
12
|
+
export const VERSION = '2.18.1'; // 2.18.1:修发布前冒烟的两个选项目缺陷——① `exports` 里的非 JS 资源(.yml/.json 等)曾被当模块 import,对正常包抛 ERR_UNKNOWN_FILE_EXTENSION,把好包拦在发布前;② 点出口 `"."` 曾被跳过,导致主入口从来没被测过、门禁形同虚设。现在按目标扩展名与条件导出挑出「真正能被 Node 加载」的入口,并给两侧行为补了单测。历史:2.18.0 新增 npm_unpublish(撤包,默认 dryRun)。
|
|
13
13
|
// 实测后果:该目录有 package.json ⇒ npm 去打包整棵 DSH 树、输出 2.1MB > spawnSync 默认 maxBuffer 1MB ⇒ ENOBUFS(npm_publish 的体检与真发全部失败);
|
|
14
14
|
// 该目录没有 package.json 时报 ENOENT。现 publish 的两个调用点显式传 cwd=目标包目录,并给 runNpm 加 16MB maxBuffer。
|
|
15
15
|
// 2.8.3:文案修正——「零改动 / 无新提交」明确为**正常成功路径**(注入用法、工具描述、README、skill 同批改),并修 job 输出自相矛盾(cleanSkipCommit 路径补 committed:false,不再显示「已提交,未推送」)
|