dsh-git-push 2.16.0 → 2.16.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.
@@ -171,7 +171,9 @@ export async function startLocalRegistry({ storeDir, port = 0 } = {}) {
171
171
  * @param {{dir:string, tag?:string, packageName?:string, entrySpecs?:string[], timeoutMs?:number}} o
172
172
  * @returns {Promise<{ok:boolean, steps:object[], failures:string[], packument?:object}>}
173
173
  */
174
- export async function runLocalPreview({ dir, tag = 'beta', packageName = '', entrySpecs = [], timeoutMs = 300000 } = {}) {
174
+ export async function runLocalPreview({
175
+ dir, tag = 'beta', packageName = '', entrySpecs = [], bins = [], expectedVersion = '', timeoutMs = 300000,
176
+ } = {}) {
175
177
  const steps = [];
176
178
  const failures = [];
177
179
  const reg = await startLocalRegistry();
@@ -201,12 +203,34 @@ export async function runLocalPreview({ dir, tag = 'beta', packageName = '', ent
201
203
  steps.push({ step: 'install', ok: inst.code === 0 });
202
204
  if (inst.code !== 0) { failures.push(`npm install(本地 registry)失败:${(inst.err || inst.out).slice(-400)}`); return { ok: false, steps, failures }; }
203
205
 
206
+ // ① 库入口:按 exports/main 逐个 import
204
207
  if (entrySpecs.length) {
205
208
  const script = `for (const s of ${JSON.stringify(entrySpecs)}) { await import(s); }\nconsole.log('ok');\n`;
206
209
  const probe = await run(process.execPath, ['--input-type=module', '-e', script], { cwd: consumer, env, timeoutMs });
207
210
  steps.push({ step: 'import', ok: probe.code === 0, specs: entrySpecs });
208
211
  if (probe.code !== 0) failures.push(`从本地 registry 安装后加载失败:${(probe.err || '').slice(0, 400)}`);
209
212
  }
213
+ // ② bin 入口:**真跑一次**(纯 CLI 包唯一有效的验证方式)。
214
+ // 为什么不能只 import:纯 CLI 包没有库入口,import 必然失败;而它的真实用法是 `npx <命令>`。
215
+ // 真跑 bin 还能抓到 import 永远测不到的故障:bin 判据用了字面路径、而 npm 装的是符号链接,
216
+ // 导致执行静默 exit 0 且零输出(历史事故形态)。
217
+ for (const b of bins) {
218
+ const binPath = join(consumer, 'node_modules', name, b.path);
219
+ const ver = await run(process.execPath, [binPath, '--version'], { cwd: consumer, env, timeoutMs: 60000 });
220
+ const verOut = `${ver.out || ''}${ver.err || ''}`.trim();
221
+ const verOk = ver.code === 0 && (!expectedVersion || verOut.includes(expectedVersion));
222
+ steps.push({ step: 'bin-version', ok: verOk, bin: b.command, output: verOut.slice(0, 120) });
223
+ if (!verOk) {
224
+ failures.push(`bin「${b.command} --version」未通过(退出码 ${ver.code}${expectedVersion ? `,期望输出含 ${expectedVersion}` : ''}):${verOut.slice(0, 300) || '(无输出)'}`);
225
+ }
226
+ const help = await run(process.execPath, [binPath, '--help'], { cwd: consumer, env, timeoutMs: 60000 });
227
+ steps.push({ step: 'bin-help', ok: help.code === 0, bin: b.command, code: help.code });
228
+ if (help.code !== 0) failures.push(`bin「${b.command} --help」退出码 ${help.code}(应为 0)`);
229
+ }
230
+ // ③ 既无库入口也无 bin:如实说「无可验证入口」,而不是含糊的「加载失败」
231
+ if (!entrySpecs.length && !bins.length) {
232
+ failures.push('该包既没有库入口(main/exports)也没有 bin——没有任何可验证入口,无法判断安装后是否可用');
233
+ }
210
234
  return { ok: failures.length === 0, steps, failures, packument: { name, distTags: doc['dist-tags'], versions: Object.keys(doc.versions || {}) } };
211
235
  } catch (e) {
212
236
  failures.push(`本地彩排异常:${String(e?.message || e).slice(0, 300)}`);
@@ -13,7 +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, entrySpecsOf, isBrowserEntry } from './smoke.js';
16
+ import { smokeTestPackage, entrySpecsOf, isBrowserEntry, classifyPackageForm } from './smoke.js';
17
17
  import { runLocalPreview } from './local-registry.js';
18
18
 
19
19
  export const PUBLISH_LOG_FILE = 'npm-publish.log';
@@ -172,11 +172,17 @@ export async function publishPackage({
172
172
  let preview = null;
173
173
  if (localPreview) {
174
174
  const pkgForPreview = JSON.parse(readFileSync(join(info.dir, 'package.json'), 'utf8'));
175
- const specs = entrySpecsOf(pkgForPreview).filter((sp) => {
175
+ // 按**包的真实形态**决定验证方式:库包验 import,纯 CLI 包验 bin(真跑 --version/--help)。
176
+ // 历史实现一律 import,把 bin-only 的 CLI 包误判成「安装后加载失败」。
177
+ const form = classifyPackageForm(pkgForPreview);
178
+ const specs = form.entrySpecs.filter((sp) => {
176
179
  const key = sp === pkgForPreview.name ? '.' : `./${sp.slice(String(pkgForPreview.name).length + 1)}`;
177
180
  return !isBrowserEntry(key);
178
181
  });
179
- preview = await runLocalPreview({ dir: info.dir, tag: tg, packageName: info.name, entrySpecs: specs, timeoutMs });
182
+ preview = await runLocalPreview({
183
+ dir: info.dir, tag: tg, packageName: info.name,
184
+ entrySpecs: specs, bins: form.bins, expectedVersion: info.version, timeoutMs,
185
+ });
180
186
  if (!preview.ok) {
181
187
  return {
182
188
  ok: false, blocked: 'local-preview-failed', name: info.name, version: info.version,
package/lib/npm/smoke.js CHANGED
@@ -36,9 +36,44 @@ export function isBrowserEntry(key) {
36
36
  return /(^|\/)client(\/|\.js)?(\/|$)|browser/i.test(String(key).replace(/^\.\//, ''));
37
37
  }
38
38
 
39
+ /** 从 package.json 取「可执行的 bin 入口」(bin 可能是字符串或对象)。 */
40
+ export function binEntriesOf(pkg, packageName = '') {
41
+ const bin = pkg?.bin;
42
+ const name = String(packageName || pkg?.name || '');
43
+ const list = [];
44
+ if (typeof bin === 'string' && bin.trim()) list.push({ command: name, path: bin.trim() });
45
+ else if (bin && typeof bin === 'object') {
46
+ for (const [cmd, rel] of Object.entries(bin)) {
47
+ if (typeof rel === 'string' && rel.trim()) list.push({ command: cmd, path: rel.trim() });
48
+ }
49
+ }
50
+ return list;
51
+ }
52
+
53
+ /**
54
+ * 判断包的**真实形态**,据此选择验证方式。
55
+ *
56
+ * 为什么必须分形态:纯 CLI 服务包(只有 `bin`,没有 `main`/`exports`)设计上就没有库入口,
57
+ * 对它 `import('<包名>')` 必然失败——但包本身完全正常(真实用法是 `npx <命令>`)。
58
+ * 历史实现一律用 import 探测公开面,于是把这类包误判成「安装后加载失败」,
59
+ * 报错还会让调用方以为包坏了。
60
+ * @returns {{kind:'library'|'cli'|'both'|'none', entrySpecs:string[], bins:Array<{command:string,path:string}>}}
61
+ */
62
+ export function classifyPackageForm(pkg) {
63
+ const name = String(pkg?.name || '');
64
+ const bins = binEntriesOf(pkg, name);
65
+ const hasLib = Boolean(pkg?.exports) || Boolean(pkg?.main);
66
+ const entrySpecs = hasLib && name ? entrySpecsOf(pkg) : [];
67
+ const kind = hasLib && bins.length ? 'both' : hasLib ? 'library' : bins.length ? 'cli' : 'none';
68
+ return { kind, entrySpecs, bins };
69
+ }
70
+
39
71
  export function entrySpecsOf(pkg) {
40
72
  const name = String(pkg?.name || '');
41
73
  if (!name) return [];
74
+ // 没有 main/exports 的包(纯 CLI)**没有库入口**:返回空数组,让调用方走 bin 验证。
75
+ // 历史实现无条件返回 [name],对 bin-only 包 import 必然失败(Cannot find package .../index.js)。
76
+ if (!pkg.exports && !pkg.main) return [];
42
77
  const exp = pkg.exports;
43
78
  if (typeof exp === 'string') return [name];
44
79
  if (exp && typeof exp === 'object') {
@@ -135,6 +170,22 @@ export async function smokeTestPackage({ dir = '', workspaceRoot = '', registry
135
170
  failures.push(`加载探针执行失败:${String(e.message || e).slice(0, 300)}`);
136
171
  return { ok: false, steps, failures, tgz: tgzName, bytes };
137
172
  }
173
+ // bin 入口:装完真跑一次(纯 CLI 包没有库入口,import 测不到;见 classifyPackageForm 说明)
174
+ const bins = binEntriesOf(pkg, name);
175
+ for (const b of bins) {
176
+ try {
177
+ const { execFileSync } = await import('node:child_process');
178
+ const out = String(execFileSync(process.execPath, [join(consumer, 'node_modules', name, b.path), '--version'], {
179
+ cwd: consumer, encoding: 'utf8', timeout: 60000, stdio: ['ignore', 'pipe', 'pipe'],
180
+ })).trim();
181
+ const ok = !version || out.includes(version);
182
+ steps.push({ step: 'bin-version', bin: b.command, ok, output: out.slice(0, 120) });
183
+ if (!ok) failures.push(`bin「${b.command} --version」输出未包含版本 ${version}:${out.slice(0, 200)}`);
184
+ } catch (e) {
185
+ steps.push({ step: 'bin-version', bin: b.command, ok: false });
186
+ failures.push(`bin「${b.command} --version」执行失败:${String(e?.message || e).slice(0, 200)}`);
187
+ }
188
+ }
138
189
  for (const it of parsed.entries || []) {
139
190
  if (it.ok) steps.push({ step: 'import', spec: it.spec, ok: true });
140
191
  else { steps.push({ step: 'import', spec: it.spec, ok: false }); failures.push(`公开面 import 失败:${it.spec} → ${it.error}`); }
package/lib/self/index.js CHANGED
@@ -9,7 +9,7 @@
9
9
  */
10
10
 
11
11
 
12
- export const VERSION = '2.16.0'; // 2.16.0:建仓路径改用 CIFS 安全初始化 + 可选首推。① 抽公共:init-compat 新增 ensureUsableLocalRepo/isUsableRepo —— 判「.git 是否真可用」(含仓库根校验,避免把父仓库误判成本目录的仓库),不可用且残缺则 repair、没有则 create,复用 clone 已在用的 CIFS 降级(手写 .git 绕 chmod);② 建仓:ensureRemoteRepo 用它替代 existsSync('.git') 判据,origin 设置失败**如实报错**(原来会谎报成功);③ 首推:git_remote_create 新增 pushInitial —— 本地无提交时先本地提交(身份用 -c 传入,不写 config),再走已有 Git Data API 上传(transport.pushInitialContent → pushViaApi),CIFS 上全程不需 git remote add/config。历史:2.15.3 本地 registry 彩排。
12
+ export const VERSION = '2.16.1'; // 2.16.1:发布前彩排/冒烟按**包形态**验证(纯 CLI 包不再被判死)。历史实现一律 import('<包名>') 探测公开面:只有 bin、没有 main/exports 的纯 CLI 包必然报「Cannot find package .../index.js」,把好包误判成坏包、报错还写「加载失败」。现改为 classifyPackageForm 判形态:有库入口→import;只有 bin→**真跑** bin --version(断言输出含版本)与 --help(断言退出码 0);两者皆无→明确报「没有任何可验证入口」。真跑 bin 还能抓到 import 测不到的故障(bin 判据用字面路径、npm 装的是符号链接 → 执行静默 exit 0 零输出)。历史:2.16.0 建仓 CIFS 安全初始化。
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.16.0",
3
+ "version": "2.16.1",
4
4
  "description": "DSH git 自动提交推送插件——统一函数入口架构(规则/审计/git/自身/侧边栏/评分/豁免/上下文/HTTP/测试十大总入口)",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",