dsh-git-push 2.15.0 → 2.15.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.
@@ -131,6 +131,7 @@ export const TOOL_REGISTRY = [
131
131
  { flag: 'no-dry-run', type: 'boolean', desc: '关闭 dry-run(= dryRun:false;真发还需 --confirm)' },
132
132
  { flag: 'waitVisible', type: 'boolean', desc: '发布后等待 registry 可见(默认 false:上传成功即为成功,可见延迟是已知现象;true 时每分钟查一次、最多 5 分钟)' },
133
133
  { flag: 'json', type: 'boolean', desc: 'JSON 输出(CLI 用;与其它工具同口径)' },
134
+ { flag: 'skipSmoke', type: 'boolean', desc: '跳过发布前冒烟(默认跑:pack→干净目录安装→import 公开面→解析依赖;关掉会失去“装得上”这层门禁)' },
134
135
  ],
135
136
  },
136
137
  {
@@ -292,6 +292,8 @@ async function callNpmPublishJob(args = {}, jobs, exec, log, env = {}, cfg = {})
292
292
  dryRun,
293
293
  tag: args.tag || 'latest',
294
294
  otp: args.otp || '',
295
+ // 发布前冒烟默认开;慢/离线环境可关(关掉就等于放弃「装得上」这一层门禁)
296
+ skipSmoke: args.skipSmoke === true || args.noSmoke === true,
295
297
  confirm: args.confirm === true,
296
298
  // 可选:发布后等待 registry 可见(默认不等——上传成功即为成功,可见延迟是已知现象)
297
299
  waitVisible: args.waitVisible === true,
@@ -13,6 +13,7 @@ import { join } from 'node:path';
13
13
  import { credentialsDir, readNpmToken } from '../core/credentials.js';
14
14
  import { DEFAULT_REGISTRY, NPM_TIMEOUT_MS, normalizeRegistry, runNpm } from './registry.js';
15
15
  import { npmArgs, writeNpmrc } from './npmrc.js';
16
+ import { smokeTestPackage } from './smoke.js';
16
17
 
17
18
  export const PUBLISH_LOG_FILE = 'npm-publish.log';
18
19
  export const PUBLISH_TIMEOUT_MS = 300_000;
@@ -117,6 +118,9 @@ function appendPublishLog(line, { workspaceRoot = '' } = {}) {
117
118
  export async function publishPackage({
118
119
  dir = '', registry = DEFAULT_REGISTRY, workspaceRoot = '',
119
120
  dryRun = true, tag = 'latest', otp = '', confirm = false, timeoutMs = PUBLISH_TIMEOUT_MS,
121
+ // 发布前冒烟(默认开):pack → 干净目录装 → import 公开面 → 解析依赖。
122
+ // 干跑只给「会打进哪些文件」,不证明「装上能不能用」——这类问题只能在真装一次时暴露。
123
+ skipSmoke = false,
120
124
  // 可选:发布成功后等待 registry 可见。默认 false —— 上传成功即为成功(可见延迟是已知现象)
121
125
  waitVisible = false,
122
126
  } = {}) {
@@ -147,6 +151,19 @@ export async function publishPackage({
147
151
  if (!pack.ok) return { ok: false, blocked: 'pack-failed', name: info.name, version: info.version, error: pack.error, packStdout: String(pack.stdout || '').slice(-2000) };
148
152
  if (!pack.files.length) return { ok: false, blocked: 'empty-pack', name: info.name, version: info.version, error: 'npm pack 清单为空(检查 files 字段)' };
149
153
 
154
+ // 体检④:**发布前冒烟**——真装一次并加载公开面。失败直接拦,不写 registry。
155
+ let smoke = null;
156
+ if (!skipSmoke) {
157
+ smoke = await smokeTestPackage({ dir: info.dir, workspaceRoot, registry: reg, timeoutMs });
158
+ if (!smoke.ok) {
159
+ return {
160
+ ok: false, blocked: 'smoke-failed', name: info.name, version: info.version,
161
+ error: `发布前冒烟不通过:${(smoke.failures || []).slice(0, 3).join(';')}`,
162
+ smoke,
163
+ };
164
+ }
165
+ }
166
+
150
167
  const rc = writeNpmrc({ workspaceRoot, registry: reg, token });
151
168
  if (!rc.ok) return { ok: false, error: rc.error || 'npmrc 写入失败' };
152
169
  const args = ['publish', ...npmArgs({ npmrcFile: rc.file, registry: reg }), '--tag', tg];
@@ -182,6 +199,8 @@ export async function publishPackage({
182
199
  packList: pack.files,
183
200
  packSize: pack.size,
184
201
  tarball: pack.filename,
202
+ // 发布前冒烟结果(装了没、公开面能不能 import、依赖闭包全不全)
203
+ smoke: smoke ? { ok: smoke.ok, tgz: smoke.tgz, bytes: smoke.bytes, entries: smoke.entries, deps: smoke.deps, failures: smoke.failures } : null,
185
204
  published: !dryRun && !!r.ok, // 成功判定:npm 上传成功即为成功(不要求 registry 已可见)
186
205
  // 附加信息(不影响 published):registry 是否已能看到新版本;发布延迟(几分钟)是已知现象
187
206
  visible,
@@ -0,0 +1,162 @@
1
+ /**
2
+ * 发布前冒烟:证明「这个包**装得上、加载得了、跑得起来**」——而不是只看 `npm publish --dry-run`
3
+ * 的文件清单。
4
+ *
5
+ * 为什么需要(真实教训):干跑只告诉你「哪些文件会进包」,不告诉你「装上后能不能 import」。
6
+ * 我们踩过的两类坑都在干跑里看不见:
7
+ * · 依赖漏声明 / 子路径依赖缺失(`js-yaml`、`tree-sitter-wasms/out/*.wasm`)——装完才 ERR_MODULE_NOT_FOUND;
8
+ * · `exports` 收窄后内部子路径 `import('pkg/lib/x.js')` 直接 ERR_PACKAGE_PATH_NOT_EXPORTED,
9
+ * 而公开面(`.` / `./audit` / `./client`)其实需要逐个 have-to-import 才算验过。
10
+ *
11
+ * 做法(全部在临时目录里,不污染仓库):
12
+ * ① `npm pack` 出真实 tarball;② 建一个干净 consumer 工程并 `npm i <tgz>`(走真实依赖解析);
13
+ * ③ 按 package.json 的 `exports` 公开面逐个 import;④ 逐个解析声明的 dependencies;
14
+ * ⑤ 清理临时目录,回执里给出每步结果与失败原因。
15
+ *
16
+ * 不做的事:不跑单测、不启宿主。它只回答「包本身能不能被正常消费」;
17
+ * 「宿主能不能加载插件」属于隔离实例验证(test_env)的范畴,成本高得多,不做进默认门禁。
18
+ */
19
+ import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
20
+ import { join } from 'node:path';
21
+ import { tmpdir } from 'node:os';
22
+
23
+ import { runNpm } from './registry.js';
24
+ import { npmArgs } from './npmrc.js';
25
+
26
+ const SMOKE_TIMEOUT_MS = 240000;
27
+
28
+ /**
29
+ * 由 package.json 推导「必须能 import 的公开面」。
30
+ * exports 存对象时取所有 key(跳过 './package.json'——它不是模块,只能被读取);
31
+ * 没有 exports 时退化为包名根入口。
32
+ */
33
+ /** 浏览器端入口(如 `./client`):只能在宿主前端环境加载,Node 里 import 必然失败,冒烟时跳过。 */
34
+ export function isBrowserEntry(key) {
35
+ // `./client`、`./client/index`、`./lib/client.js` 都算浏览器端入口
36
+ return /(^|\/)client(\/|\.js)?(\/|$)|browser/i.test(String(key).replace(/^\.\//, ''));
37
+ }
38
+
39
+ export function entrySpecsOf(pkg) {
40
+ const name = String(pkg?.name || '');
41
+ if (!name) return [];
42
+ const exp = pkg.exports;
43
+ if (typeof exp === 'string') return [name];
44
+ if (exp && typeof exp === 'object') {
45
+ const subs = Object.keys(exp)
46
+ .filter((k) => k === '.' || k.startsWith('./'))
47
+ .filter((k) => k !== './package.json')
48
+ .map((k) => (k === '.' ? name : `${name}/${k.slice(2)}`));
49
+ return subs.length ? subs : [name];
50
+ }
51
+ return [name];
52
+ }
53
+
54
+ /** 冒烟主流程。 */
55
+ export async function smokeTestPackage({ dir = '', workspaceRoot = '', registry = '', timeoutMs = SMOKE_TIMEOUT_MS } = {}) {
56
+ const steps = [];
57
+ const failures = [];
58
+ let tmp = '';
59
+ try {
60
+ const pkg = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
61
+ const name = String(pkg.name || '');
62
+ const version = String(pkg.version || '');
63
+ if (!name) return { ok: false, steps, failures: ['package.json 缺 name'] };
64
+
65
+ tmp = mkdtempSync(join(tmpdir(), 'dshgp-smoke-'));
66
+ // ① 打真实 tarball
67
+ const packDest = join(tmp, 'pack');
68
+ const consumer = join(tmp, 'consumer');
69
+ mkdirSync(packDest, { recursive: true });
70
+ mkdirSync(consumer, { recursive: true });
71
+ const packArgs = ['pack', '--pack-destination', packDest, ...npmArgs({ registry })];
72
+ const packed = runNpm(packArgs, { cwd: dir, timeoutMs, env: { NO_UPDATE_NOTIFIER: '1' } });
73
+ if (!packed.ok) {
74
+ failures.push(`npm pack 失败:${String(packed.error || packed.stderr || '').slice(0, 300)}`);
75
+ return { ok: false, steps, failures };
76
+ }
77
+ const tgzName = readdirSync(packDest).find((f) => f.endsWith('.tgz'));
78
+ if (!tgzName) { failures.push('npm pack 没产出 tarball'); return { ok: false, steps, failures }; }
79
+ const tgz = join(packDest, tgzName);
80
+ const bytes = statSync(tgz).size;
81
+ steps.push({ step: 'pack', ok: true, tgz: tgzName, bytes });
82
+
83
+ // ② 干净 consumer 里真实安装
84
+ writeFileSync(join(consumer, 'package.json'), JSON.stringify({ name: 'smoke-consumer', private: true, version: '0.0.0' }, null, 2), 'utf8');
85
+ const installed = runNpm(['install', '--no-audit', '--no-fund', tgz], { cwd: consumer, timeoutMs, env: { NO_UPDATE_NOTIFIER: '1' } });
86
+ if (!installed.ok) {
87
+ failures.push(`npm install <tgz> 失败:${String(installed.error || installed.stderr || '').slice(0, 400)}`);
88
+ return { ok: false, steps, failures, tgz: tgzName, bytes };
89
+ }
90
+ steps.push({ step: 'install', ok: true });
91
+
92
+ // ③ 公开面逐个 import + ④ 依赖逐个解析(同一进程里做,省一次启动)
93
+ const probe = join(consumer, 'probe.mjs');
94
+ const deps = Object.keys(pkg.dependencies || {});
95
+ // nodeEntries = 可在 Node 里加载的公开面;browserEntries = 仅前端可加载(跳过但如实列出)
96
+ const nodeEntries = [];
97
+ const browserEntries = [];
98
+ for (const key of Object.keys(pkg.exports && typeof pkg.exports === 'object' ? pkg.exports : { '.': null })) {
99
+ const spec = key === '.' ? name : `${name}/${key.slice(2)}`;
100
+ if (key !== '.' && isBrowserEntry(key)) browserEntries.push(spec);
101
+ else if (!key.startsWith('./') || key === './package.json') continue;
102
+ else nodeEntries.push(spec);
103
+ }
104
+ writeFileSync(probe, [
105
+ 'import { createRequire } from "node:module";',
106
+ 'import { existsSync } from "node:fs";',
107
+ 'import { join } from "node:path";',
108
+ `const specs = ${JSON.stringify(nodeEntries)};`,
109
+ `const deps = ${JSON.stringify(deps)};`,
110
+ 'const out = { entries: [], deps: [] };',
111
+ 'for (const s of specs) {',
112
+ ' try { await import(s); out.entries.push({ spec: s, ok: true }); }',
113
+ ' catch (e) { out.entries.push({ spec: s, ok: false, error: String(e.code || "") + " " + String(e.message || e).slice(0, 200) }); }',
114
+ '}',
115
+ 'const req = createRequire(import.meta.url);',
116
+ 'for (const d of deps) {',
117
+ ' // 判据是「**依赖真的装进来了**」,不是「它的 main 能解析」:',
118
+ ' // 有些包只发布子路径(如 tree-sitter-wasms 的 main 指向未发布目录,但 out/*.wasm 可用),',
119
+ ' // 用 require.resolve(包名) 判会把正常包判成缺依赖。',
120
+ ' const installed = existsSync(join(process.cwd(), "node_modules", d, "package.json"));',
121
+ ' if (!installed) { out.deps.push({ dep: d, ok: false, error: "未安装(依赖闭包不全)" }); continue; }',
122
+ ' if (d.startsWith("node:")) { out.deps.push({ dep: d, ok: true }); continue; }',
123
+ ' try { req.resolve(d); out.deps.push({ dep: d, ok: true }); }',
124
+ ' catch (e) { out.deps.push({ dep: d, ok: true, warn: `已安装,但 bare 名不可解析(可能只导出子路径):${e.code || ""}` }); }',
125
+ '}',
126
+ 'console.log(JSON.stringify(out));',
127
+ ].join('\n'), 'utf8');
128
+ // 直接用 node 跑探针脚本:比 `npm exec` 稳(后者在部分版本会给 stdout 加包装/额外行)
129
+ let parsed = null;
130
+ try {
131
+ const { execFileSync } = await import('node:child_process');
132
+ const stdout = execFileSync(process.execPath, [probe], { cwd: consumer, encoding: 'utf8', timeout: timeoutMs });
133
+ parsed = JSON.parse(String(stdout).trim().split('\n').pop());
134
+ } catch (e) {
135
+ failures.push(`加载探针执行失败:${String(e.message || e).slice(0, 300)}`);
136
+ return { ok: false, steps, failures, tgz: tgzName, bytes };
137
+ }
138
+ for (const it of parsed.entries || []) {
139
+ if (it.ok) steps.push({ step: 'import', spec: it.spec, ok: true });
140
+ else { steps.push({ step: 'import', spec: it.spec, ok: false }); failures.push(`公开面 import 失败:${it.spec} → ${it.error}`); }
141
+ }
142
+ for (const it of parsed.deps || []) {
143
+ if (it.ok) steps.push({ step: 'dep', dep: it.dep, ok: true });
144
+ else { steps.push({ step: 'dep', dep: it.dep, ok: false }); failures.push(`依赖解析失败:${it.dep} → ${it.error}`); }
145
+ }
146
+ return {
147
+ ok: failures.length === 0,
148
+ tgz: tgzName, bytes,
149
+ entries: nodeEntries.length + browserEntries.length,
150
+ deps: deps.length,
151
+ // 浏览器端入口(如 ./client)不在 Node 里 import,如实列出而不是静默当通过
152
+ browserEntries,
153
+ steps, failures,
154
+ hint: failures.length ? '包本身消费不了,先修再发' : '装得上、公开面能加载、依赖闭包完整',
155
+ };
156
+ } catch (e) {
157
+ failures.push(`冒烟异常:${String(e?.message || e).slice(0, 300)}`);
158
+ return { ok: false, steps, failures };
159
+ } finally {
160
+ if (tmp) { try { rmSync(tmp, { recursive: true, force: true }); } catch { /* 临时目录清理失败不影响结论 */ } }
161
+ }
162
+ }
package/lib/self/index.js CHANGED
@@ -9,7 +9,7 @@
9
9
  */
10
10
 
11
11
 
12
- export const VERSION = '2.15.0'; // 2.15.0:挂载盘采集走 git 快路径 + git 钩子探针。①采集提速:CIFS 上「231 个文本文件」的文件系统遍历实测 103.8s(sys 29s,全耗在跨网 stat),改走 `git ls-files --cached --others --exclude-standard` 后 0.1s(同一仓库实测),语义与原遍历对齐(已跟踪 + 未跟踪未忽略;.gitignore/.auditignore 照旧生效;非 git 目录仍走原遍历;可用 DSH_AUDIT_NO_GIT_FASTPATH=1 关闭);顺带修掉旧遍历漏判文件级 ignore 的问题。②钩子探针:提交/预演回执里列出本次会触发的钩子,识别「解释器不存在 / 引用了已不存在的机器路径」的坏钩子,仅在这种情况下给 LEFTHOOK=0 —— 正常钩子(lint/格式化)一律照跑,不替用户关。历史:2.14.2 mkdir 作用域判定修正。
12
+ export const VERSION = '2.15.1'; // 2.15.1:npm_publish 增加**发布前冒烟**(默认执行)。干跑只回答「哪些文件会进包」,不回答「装上能不能用」——我们踩过的两类坑都在那之后:依赖漏声明/子路径依赖缺失(js-yaml、tree-sitter-wasms/out/*.wasm)、exports 收窄后内部路径 import 直接 ERR_PACKAGE_PATH_NOT_EXPORTED。现在发布前会真 npm pack → 干净目录 npm i <tgz> → 按 exports 公开面逐个 import → 校验依赖是否真被装进来,失败即拦(blocked: smoke-failed),回执带 smoke 明细;skipSmoke:true 可关(不建议)。浏览器端入口(./client)在 Node 里必然 window is not defined,标为 browserEntries 跳过而非判失败。历史:2.15.0 采集快路径 + 钩子探针 + 改名工具。
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,不再显示「已提交,未推送」)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-git-push",
3
- "version": "2.15.0",
3
+ "version": "2.15.1",
4
4
  "description": "DSH git 自动提交推送插件——统一函数入口架构(规则/审计/git/自身/侧边栏/评分/豁免/上下文/HTTP/测试十大总入口)",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",