create-clis 0.1.0 → 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.
Files changed (87) hide show
  1. package/README.md +22 -14
  2. package/README.zh-CN.md +22 -14
  3. package/assets/manifest.json +7 -2
  4. package/assets/template/.github/ISSUE_TEMPLATE/bug_report.yml +15 -29
  5. package/assets/template/.github/ISSUE_TEMPLATE/feature_request.yml +6 -12
  6. package/assets/template/.github/PULL_REQUEST_TEMPLATE.md +3 -3
  7. package/assets/template/.github/workflows/ci.yml +69 -7
  8. package/assets/template/.github/workflows/release.yml +23 -23
  9. package/assets/template/.gitignorerc.json +1 -8
  10. package/assets/template/.oxlintrc.json +1 -6
  11. package/assets/template/.prettierignore +0 -1
  12. package/assets/template/.releaserc.json +6 -1
  13. package/assets/template/.vscode/launch.json +1 -1
  14. package/assets/template/.vscode/settings.json +1 -0
  15. package/assets/template/.vscode/tasks.json +45 -0
  16. package/assets/template/AGENTS.md +4 -4
  17. package/assets/template/CONTRIBUTING.md +6 -4
  18. package/assets/template/CONTRIBUTING.zh-CN.md +6 -4
  19. package/assets/template/_gitignore +7 -405
  20. package/assets/template/bunfig.toml +2 -11
  21. package/assets/template/docs/README.md +6 -6
  22. package/assets/template/docs/adrs/README.md +4 -2
  23. package/assets/template/docs/development.md +15 -9
  24. package/assets/template/lefthook.yml +20 -30
  25. package/assets/template/package.json +5 -6
  26. package/assets/template/packages/{{NAME}}/README.md +4 -4
  27. package/assets/template/packages/{{NAME}}/README.zh-CN.md +2 -2
  28. package/assets/template/packages/{{NAME}}/package.json +3 -1
  29. package/assets/template/packages/{{NAME}}/scripts/build.ts +50 -14
  30. package/assets/template/packages/{{NAME}}/scripts/verify-release.test.ts +140 -15
  31. package/assets/template/packages/{{NAME}}/scripts/verify-release.ts +27 -14
  32. package/assets/template/packages/{{NAME}}/tsconfig.build.json +1 -1
  33. package/assets/template/packages/{{NAME}}-cli/README.md +6 -6
  34. package/assets/template/packages/{{NAME}}-cli/README.zh-CN.md +2 -2
  35. package/assets/template/packages/{{NAME}}-cli/bin/bunfig.toml +3 -0
  36. package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}} +43 -21
  37. package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.cmd +8 -17
  38. package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.d.mts +6 -3
  39. package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.mjs +189 -107
  40. package/assets/template/packages/{{NAME}}-cli/package.json +3 -2
  41. package/assets/template/packages/{{NAME}}-cli/scripts/release-artifact.ts +24 -73
  42. package/assets/template/packages/{{NAME}}-cli/scripts/release.fixtures.ts +8 -3
  43. package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.test.ts +16 -10
  44. package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.ts +8 -52
  45. package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.test.ts +3 -3
  46. package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.ts +8 -68
  47. package/assets/template/scripts/ci.ts +285 -64
  48. package/assets/template/scripts/install-git-hooks.mjs +10 -0
  49. package/assets/template/scripts/install-git-hooks.test.ts +1 -1
  50. package/assets/template/scripts/lib/npm-trust-parse.ts +169 -0
  51. package/assets/template/scripts/lib/release-cli.ts +299 -0
  52. package/assets/template/scripts/lib/release-verify.ts +148 -15
  53. package/assets/template/scripts/lib/workspace.test.ts +63 -1
  54. package/assets/template/scripts/lib/workspace.ts +132 -42
  55. package/assets/template/scripts/lint-dependency-direction.test.ts +131 -0
  56. package/assets/template/scripts/lint-dependency-direction.ts +130 -0
  57. package/assets/template/scripts/lint-doc-examples.test.ts +45 -9
  58. package/assets/template/scripts/lint-doc-examples.ts +227 -50
  59. package/assets/template/scripts/lint-doc-references.test.ts +36 -0
  60. package/assets/template/scripts/lint-doc-references.ts +142 -57
  61. package/assets/template/scripts/lint-doc-shared.ts +26 -23
  62. package/assets/template/scripts/lint-stray-backups.test.ts +3 -1
  63. package/assets/template/scripts/lint-stray-backups.ts +7 -3
  64. package/assets/template/scripts/npm-trust-guard.test.ts +56 -8
  65. package/assets/template/scripts/npm-trust-guard.ts +116 -200
  66. package/assets/template/scripts/safe-install.ts +28 -23
  67. package/assets/template/scripts/transcription/api-harness.ts +28 -206
  68. package/assets/template/scripts/transcription/compare.test.ts +84 -0
  69. package/assets/template/scripts/transcription/compare.ts +4 -4
  70. package/assets/template/scripts/transcription/corpus.ts +9 -3
  71. package/assets/template/scripts/transcription/fixture.ts +2 -4
  72. package/assets/template/scripts/transcription/make-mutants.ts +116 -37
  73. package/assets/template/scripts/transcription/run-conformance.ts +57 -34
  74. package/assets/template/scripts/transcription/validate-coverage.test.ts +11 -14
  75. package/assets/template/scripts/transcription/validate-coverage.ts +8 -10
  76. package/assets/template/scripts/transcription/validate.ts +18 -3
  77. package/assets/template/tsconfig.json +2 -20
  78. package/assets/template/turbo.jsonc +7 -52
  79. package/bin/bunfig.toml +3 -0
  80. package/bin/create-clis.mjs +165 -104
  81. package/dist/create-clis.js +48 -87681
  82. package/dist/manifest.json +3 -3
  83. package/package.json +9 -5
  84. package/assets/template/packages/{{NAME}}/docs/README.md +0 -21
  85. package/assets/template/packages/{{NAME}}-cli/docs/README.md +0 -21
  86. package/bin/create-clis +0 -35
  87. package/bin/create-clis.cmd +0 -32
@@ -26,6 +26,22 @@
26
26
  */
27
27
  import { spawn } from 'node:child_process';
28
28
 
29
+ import {
30
+ type OidcEnvVerdict,
31
+ type TrustEntry,
32
+ type TrustExpectation,
33
+ classifyOidcEnv,
34
+ cleanText,
35
+ deriveRepoFromRemoteUrl,
36
+ diffTrust,
37
+ filterPublishable,
38
+ isValidTrustId,
39
+ parseTrustListEntries,
40
+ parseTrustListOutput,
41
+ ptyWrapArgs,
42
+ stripCsi,
43
+ stripPtyNoise,
44
+ } from './lib/npm-trust-parse.ts';
29
45
  import { readGitText } from './lib/release-verify.ts';
30
46
  import {
31
47
  type WorkspacePackage,
@@ -33,6 +49,20 @@ import {
33
49
  } from './lib/workspace.ts';
34
50
  import { REPO_ROOT } from './lint-doc-shared.ts';
35
51
 
52
+ export {
53
+ type OidcEnvVerdict,
54
+ type TrustEntry,
55
+ type TrustExpectation,
56
+ classifyOidcEnv,
57
+ deriveRepoFromRemoteUrl,
58
+ diffTrust,
59
+ filterPublishable,
60
+ isValidTrustId,
61
+ parseTrustListEntries,
62
+ parseTrustListOutput,
63
+ ptyWrapArgs,
64
+ };
65
+
36
66
  /** npm 官方 registry (OIDC 换证与 trust 管理端点只存在于官方源, 镜像源没有) */
37
67
  const OFFICIAL_REGISTRY = 'https://registry.npmjs.org/';
38
68
 
@@ -45,140 +75,6 @@ const RELEASE_ENVIRONMENT = 'release';
45
75
  /** OIDC 令牌 audience: npm 约定值, 与发布链 @semantic-release/npm 的换证请求一致 */
46
76
  const OIDC_AUDIENCE = 'npm:registry.npmjs.org';
47
77
 
48
- /** npm trust list 解析出的登记条目 (缺行字段落成 null) */
49
- export interface TrustEntry {
50
- type: string | null;
51
- id: string | null;
52
- file: string | null;
53
- environment: string | null;
54
- repository: string;
55
- }
56
-
57
- /** 期望的登记形态 (owner/repo 从 git origin 派生, 其余为常量) */
58
- export interface TrustExpectation {
59
- repository: string;
60
- file: string;
61
- environment: string;
62
- }
63
-
64
- /** OIDC 运行环境判定: ready = 可预检; missing-permission = Actions 里漏配 id-token; not-ci = 本地 */
65
- export type OidcEnvVerdict = 'ready' | 'missing-permission' | 'not-ci';
66
-
67
- /** 剥终端 CSI 序列 (颜色着色与 spinner 的光标控制; 参数段 [0-9;?]*, 末字符为任意字母) */
68
- const stripCsi = (text: string): string =>
69
- text.replace(
70
- new RegExp(String.fromCharCode(27) + '\\[[0-9;?]*[A-Za-z]', 'g'),
71
- '',
72
- );
73
-
74
- /** 剥控制字符并收拢空白 (npm 输出属低信任输入, 落进人读文案前先降噪) */
75
- const cleanText = (text: string): string =>
76
- text.replace(/[\p{Cc}]+/gu, ' ').trim();
77
-
78
- /**
79
- * 剥 pty 伪影: `script` 开场的 ^D + 退格序列、CRLF 行尾与孤立终端控制符。
80
- * 须在 stripCsi 之后调用: 本函数的控制符剥离面覆盖 ESC 本身, 先把完整 CSI 序列剥掉再除残留。
81
- */
82
- const stripPtyNoise = (text: string): string =>
83
- text
84
- .replace(/\r/g, '')
85
- // eslint-disable-next-line no-control-regex -- 此处刻意匹配 pty 残留控制符 (script 开场的 ^D 与退格), 属预期匹配
86
- .replace(/[\x00-\x08\x0b\x0c\x0e-\x1f]/g, '');
87
-
88
- /** 从 git remote 原文派生 owner/repo; 非 GitHub 远端或形状不符返回 null */
89
- export function deriveRepoFromRemoteUrl(raw: string): string | null {
90
- const cleaned = raw.trim().replace(/\.git$/, '');
91
- const match = cleaned.match(/github\.com[:/]([^/\s]+)\/([^/\s]+)$/);
92
- if (match === null || match[1] === undefined || match[2] === undefined) {
93
- return null;
94
- }
95
- return `${match[1]}/${match[2]}`;
96
- }
97
-
98
- /**
99
- * 解析 `npm trust list` 的人读输出 (键值行形态)。
100
- * 噪音行 (认证提示、npm notice) 天然不含键名冒号行, 不干扰解析; 未见 repository 行时返回 null
101
- * (认不出核心字段即视为形状不符, 不猜测)。
102
- */
103
- export function parseTrustListOutput(raw: string): TrustEntry | null {
104
- const text = stripPtyNoise(stripCsi(raw));
105
- const pick = (key: string): string | null => {
106
- const match = new RegExp(`^${key}:[ \\t]*(.+)$`, 'm').exec(text);
107
- if (match === null || match[1] === undefined) return null;
108
- return cleanText(match[1]);
109
- };
110
- const repository = pick('repository');
111
- if (repository === null || repository === '') return null;
112
- return {
113
- type: pick('type'),
114
- id: pick('id'),
115
- file: pick('file'),
116
- environment: pick('environment'),
117
- repository,
118
- };
119
- }
120
-
121
- /** 登记比对 (纯函数): 逐字段列出与期望值的差异, 全一致返回空列表 */
122
- export function diffTrust(
123
- actual: TrustEntry,
124
- expected: TrustExpectation,
125
- ): string[] {
126
- const problems: string[] = [];
127
- if (actual.repository !== expected.repository) {
128
- problems.push(
129
- `repository: ${actual.repository} → 期望 ${expected.repository}`,
130
- );
131
- }
132
- if (actual.file !== expected.file) {
133
- problems.push(
134
- `workflow 文件: ${actual.file ?? '(未设置)'} → 期望 ${expected.file}`,
135
- );
136
- }
137
- if (actual.environment !== expected.environment) {
138
- problems.push(
139
- `environment: ${actual.environment ?? '(未设置)'} → 期望 ${expected.environment}`,
140
- );
141
- }
142
- return problems;
143
- }
144
-
145
- /** OIDC 环境判定 (纯函数): 三分支各自对应不同出口, 防「权限漏配」被静默放过 */
146
- export function classifyOidcEnv(
147
- env: Record<string, string | undefined>,
148
- ): OidcEnvVerdict {
149
- if (env.GITHUB_ACTIONS !== 'true') return 'not-ci';
150
- return env.ACTIONS_ID_TOKEN_REQUEST_URL !== undefined &&
151
- env.ACTIONS_ID_TOKEN_REQUEST_TOKEN !== undefined
152
- ? 'ready'
153
- : 'missing-permission';
154
- }
155
-
156
- /**
157
- * 生成经 pty 包装的 npm 调用坐标。npm 的 web 认证交互 (npm 源码 lib/utils/auth.js 的 otplease)
158
- * 要求 stdin 与 stdout 都是 TTY, 非 TTY 会直接把 EOTP 抛给调用方 (附 URL 让你自己去认证, 会话内
159
- * 无法完成); `script` 起 pty 后既保交互又保捕获。
160
- * - darwin: `script -q /dev/null <cmd...>` (退出码透传, Darwin 25 实测);
161
- * - linux: `script -qec "<cmd 串>" /dev/null` (util-linux 语法; 调用点参数值全部受控:
162
- * 包名 / UUID / 常量, 无空白字符);
163
- * - 其余平台 (如 win32) 无 script, 降级直跑 (会话内认证可能不可用, 由 mainLocal 的预警兜底)。
164
- */
165
- export function ptyWrapArgs(
166
- platform: NodeJS.Platform,
167
- npm: string,
168
- args: string[],
169
- ): { command: string; args: string[] } {
170
- if (platform === 'darwin') {
171
- return { command: 'script', args: ['-q', '/dev/null', npm, ...args] };
172
- }
173
- if (platform === 'linux') {
174
- return {
175
- command: 'script',
176
- args: ['-qec', [npm, ...args].join(' '), '/dev/null'],
177
- };
178
- }
179
- return { command: npm, args };
180
- }
181
-
182
78
  /** npm 可执行名 (Windows 是 npm.cmd, execFile/spawn 不走 shell 时不会自动补扩展名) */
183
79
  const npmBin = (): string => (process.platform === 'win32' ? 'npm.cmd' : 'npm');
184
80
 
@@ -195,6 +91,7 @@ function runNpmCaptured(
195
91
  const child = spawn(wrapped.command, wrapped.args, {
196
92
  stdio: ['inherit', 'pipe', 'inherit'],
197
93
  env: { ...process.env, NO_COLOR: '1' },
94
+ shell: process.platform === 'win32',
198
95
  });
199
96
  let captured = '';
200
97
  child.stdout?.on('data', (chunk: Buffer) => {
@@ -208,20 +105,6 @@ function runNpmCaptured(
208
105
  });
209
106
  }
210
107
 
211
- /**
212
- * 预检核对面 (纯函数): 可发布 (非 private) 的包。
213
- * 核对面与发布链 (multi-semantic-release) 的「可发布包」定义保持单源一致: mrs 默认开启
214
- * ignorePrivate, private 包不进它的加载面。旧设计曾按根 package.json 的
215
- * multi-release.ignorePackages 豁免预检, 依据是「与发布链同源」; 该依据经 2026-10-01 实测
216
- * 证伪: mrs 把名单映射为 topo 的 workspacesExtra 负 glob (形如 !<包名>), 对包名形式不生效,
217
- * 被列入的包照旧被加载处理。包隔离以 npm 原生字段 private: true 为准 (首发前的新包即以此隔离)。
218
- */
219
- export function filterPublishable(
220
- packages: readonly WorkspacePackage[],
221
- ): WorkspacePackage[] {
222
- return packages.filter((pkg) => pkg.manifest.private !== true);
223
- }
224
-
225
108
  /** 可发布的 workspace 包 (预检核对面, 见 filterPublishable) */
226
109
  function publishablePackages(): WorkspacePackage[] {
227
110
  return filterPublishable(listWorkspacePackages(REPO_ROOT));
@@ -348,9 +231,11 @@ async function mainVerifyOidc(): Promise<void> {
348
231
  process.exitCode = 1;
349
232
  }
350
233
 
351
- /** 本地读单包登记: ok = 解析成功; error = npm 跑不起来 / 退出非零 / 输出形状不符 */
234
+ /** 本地读单包登记: ok = 解析成功; not-found = 尚未登记; error = 执行失败或输出异常 */
352
235
  type TrustRead =
353
- { kind: 'ok'; entry: TrustEntry } | { kind: 'error'; detail: string };
236
+ | { kind: 'ok'; entry: TrustEntry }
237
+ | { kind: 'not-found' }
238
+ | { kind: 'error'; detail: string };
354
239
 
355
240
  /** 跑 `npm trust list` 读登记 (经 pty 包装, 见 runNpmCaptured) */
356
241
  async function readTrustEntry(pkgName: string): Promise<TrustRead> {
@@ -374,6 +259,14 @@ async function readTrustEntry(pkgName: string): Promise<TrustRead> {
374
259
  }
375
260
  const entry = parseTrustListOutput(result.stdout);
376
261
  if (entry === null) {
262
+ const plain = stripPtyNoise(stripCsi(result.stdout));
263
+ if (
264
+ /no trusted publish/i.test(plain) ||
265
+ plain.trim() === '' ||
266
+ !/npm error/i.test(plain)
267
+ ) {
268
+ return { kind: 'not-found' };
269
+ }
377
270
  return {
378
271
  kind: 'error',
379
272
  detail: 'npm trust list 输出无法解析 (未见 repository 行)',
@@ -400,28 +293,12 @@ async function runNpmInteractive(args: string[]): Promise<boolean> {
400
293
  return true;
401
294
  }
402
295
 
403
- /**
404
- * 重建单包登记: 先 revoke 旧记录 (npm 侧一条包只支持一份登记, 不支持就地修改), 再按期望值创建。
405
- * 新登记取最小权限: 仅 publish (stage publish 未使用, 不勾)。
406
- */
407
- async function repairPackage(
296
+ /** 创建单包 Trusted Publisher 登记 (最小权限: 仅 --allow-publish) */
297
+ async function createTrustEntry(
408
298
  pkgName: string,
409
- entry: TrustEntry,
410
299
  expected: TrustExpectation,
411
300
  ): Promise<boolean> {
412
- process.stdout.write(` → 重建 ${pkgName} 的 Trusted Publisher 登记:\n`);
413
- // 两个动作都执行 (revoke 失败仍尝试 create), 结果取合
414
- const revoked =
415
- entry.id === null
416
- ? true
417
- : await runNpmInteractive([
418
- 'trust',
419
- 'revoke',
420
- pkgName,
421
- `--id=${entry.id}`,
422
- `--registry=${OFFICIAL_REGISTRY}`,
423
- ]);
424
- const created = await runNpmInteractive([
301
+ return runNpmInteractive([
425
302
  'trust',
426
303
  'github',
427
304
  pkgName,
@@ -434,12 +311,62 @@ async function repairPackage(
434
311
  '--allow-publish',
435
312
  `--registry=${OFFICIAL_REGISTRY}`,
436
313
  ]);
314
+ }
315
+
316
+ /**
317
+ * 重建或新建单包登记: 已有旧记录时先 revoke (ID 须通过白名单校验), 再按期望值创建。
318
+ * 首次登记 (entry === null) 时直接创建。
319
+ */
320
+ async function repairPackage(
321
+ pkgName: string,
322
+ entry: TrustEntry | null,
323
+ expected: TrustExpectation,
324
+ ): Promise<boolean> {
325
+ const verb = entry === null ? '新建' : '重建';
326
+ process.stdout.write(` → ${verb} ${pkgName} 的 Trusted Publisher 登记:\n`);
327
+ let revoked = true;
328
+ if (entry !== null && entry.id !== null) {
329
+ if (!isValidTrustId(entry.id)) {
330
+ process.stderr.write(
331
+ ` ✘ 拒绝 revoke: 登记 ID 含非法字符 (${cleanText(entry.id)})\n`,
332
+ );
333
+ return false;
334
+ }
335
+ revoked = await runNpmInteractive([
336
+ 'trust',
337
+ 'revoke',
338
+ pkgName,
339
+ `--id=${entry.id}`,
340
+ `--registry=${OFFICIAL_REGISTRY}`,
341
+ ]);
342
+ }
343
+ const created = await createTrustEntry(pkgName, expected);
437
344
  return revoked && created;
438
345
  }
439
346
 
347
+ /** 执行单包修复并复查是否与期望一致, 返回是否最终通过 */
348
+ async function applyRepairAndVerify(
349
+ pkgName: string,
350
+ entry: TrustEntry | null,
351
+ expected: TrustExpectation,
352
+ label: string,
353
+ ): Promise<boolean> {
354
+ const repaired = await repairPackage(pkgName, entry, expected);
355
+ const after = await readTrustEntry(pkgName);
356
+ const verified =
357
+ after.kind === 'ok' && diffTrust(after.entry, expected).length === 0;
358
+ if (verified) {
359
+ process.stdout.write(` ✔ ${pkgName}: ${label}复查通过\n`);
360
+ return repaired;
361
+ }
362
+ process.stderr.write(
363
+ ` ✘ ${pkgName}: ${label}复查未通过, 请人工检查 npm 侧配置\n`,
364
+ );
365
+ return false;
366
+ }
367
+
440
368
  /** 本地对账 (check) 与修复 (fix) 的主流程 */
441
369
  async function mainLocal(command: 'check' | 'fix'): Promise<void> {
442
- // npm 的认证交互要求 TTY; 非交互环境 (管道 / 钩子) 里会卡在等待输入, 先在入口预警
443
370
  if (process.stdin.isTTY !== true || process.stdout.isTTY !== true) {
444
371
  process.stderr.write(
445
372
  '提示: 当前不是交互式终端; npm 的网页认证需要 TTY, 请在交互式终端重跑本命令\n',
@@ -466,6 +393,16 @@ async function mainLocal(command: 'check' | 'fix'): Promise<void> {
466
393
  failed = true;
467
394
  continue;
468
395
  }
396
+ if (read.kind === 'not-found') {
397
+ process.stderr.write(`✘ ${pkg.name}: npm 侧尚未配置 Trusted Publisher\n`);
398
+ if (
399
+ command === 'check' ||
400
+ !(await applyRepairAndVerify(pkg.name, null, expected, '首次登记'))
401
+ ) {
402
+ failed = true;
403
+ }
404
+ continue;
405
+ }
469
406
  const problems = diffTrust(read.entry, expected);
470
407
  if (problems.length === 0) {
471
408
  process.stdout.write(`✔ ${pkg.name}: npm 侧登记与当前仓库一致\n`);
@@ -473,20 +410,10 @@ async function mainLocal(command: 'check' | 'fix'): Promise<void> {
473
410
  }
474
411
  process.stderr.write(`✘ ${pkg.name}: npm 侧登记与当前仓库不一致:\n`);
475
412
  for (const problem of problems) process.stderr.write(` ${problem}\n`);
476
- if (command === 'check') {
477
- failed = true;
478
- continue;
479
- }
480
- if (!(await repairPackage(pkg.name, read.entry, expected))) {
481
- failed = true;
482
- }
483
- const after = await readTrustEntry(pkg.name);
484
- if (after.kind === 'ok' && diffTrust(after.entry, expected).length === 0) {
485
- process.stdout.write(` ✔ ${pkg.name}: 重建后复查通过\n`);
486
- } else {
487
- process.stderr.write(
488
- ` ✘ ${pkg.name}: 重建后复查未通过, 请人工检查 npm 侧配置\n`,
489
- );
413
+ if (
414
+ command === 'check' ||
415
+ !(await applyRepairAndVerify(pkg.name, read.entry, expected, '重建后'))
416
+ ) {
490
417
  failed = true;
491
418
  }
492
419
  }
@@ -499,8 +426,11 @@ async function mainLocal(command: 'check' | 'fix'): Promise<void> {
499
426
  process.exitCode = 1;
500
427
  }
501
428
 
502
- /** 用法说明 */
503
- function printUsage(): void {
429
+ /** 入口: 分发子命令 (未知命令打用法并退 2) */
430
+ async function main(): Promise<void> {
431
+ const command = process.argv[2];
432
+ if (command === 'verify-oidc') return mainVerifyOidc();
433
+ if (command === 'check' || command === 'fix') return mainLocal(command);
504
434
  process.stderr.write(
505
435
  [
506
436
  '发布凭据闸门: 核对 npm 侧 Trusted Publisher 登记与当前仓库形态是否一致',
@@ -512,20 +442,6 @@ function printUsage(): void {
512
442
  '',
513
443
  ].join('\n'),
514
444
  );
515
- }
516
-
517
- /** 入口: 分发子命令 (未知命令打用法并退 2) */
518
- async function main(): Promise<void> {
519
- const command = process.argv[2];
520
- if (command === 'verify-oidc') {
521
- await mainVerifyOidc();
522
- return;
523
- }
524
- if (command === 'check' || command === 'fix') {
525
- await mainLocal(command);
526
- return;
527
- }
528
- printUsage();
529
445
  process.exitCode = 2;
530
446
  }
531
447
 
@@ -1,39 +1,44 @@
1
1
  /**
2
- * safe-install: 干净重装 — 清掉可再生的构建产物与锁文件, 再按 package.json 重新安装。
2
+ * safe-install: 干净重装 — 清掉全部工作区包的可再生构建产物、派生资产、缓存与锁文件,
3
+ * 再按 package.json 重新安装。
3
4
  *
4
- * 为什么: ① dist 是带"身份绑定"的构建产物 (见 ADR 0009), 陈旧产物会让入口优先跑到
5
- * 与源码不一致的字节; ② 锁文件漂移或手改后, 声明与实装可能脱钩。两步合起来把依赖态
6
- * 重置回"由 package.json 唯一决定"的干净点。
7
- * 形态参考个人项目 buffett 的 `safe-install` (`clean report dist bunlock && bun i`);
8
- * 这里以仓库内实现替代其全局 `clean` 工具, 保持本仓库对贡献者零私有依赖。
5
+ * 为什么: ① dist 与 assets 是构建期派生件, 陈旧产物会让入口跑到与源码不一致的字节;
6
+ * ② 锁文件漂移或依赖树深脏时, 全量清掉可再生面再装回, 能重置回「由 package.json 唯一决定」的干净点。
9
7
  */
10
8
  import { spawnSync } from 'node:child_process';
11
- import { rmSync } from 'node:fs';
9
+ import { existsSync, rmSync } from 'node:fs';
10
+ import { join } from 'node:path';
12
11
  import { fileURLToPath } from 'node:url';
13
12
 
13
+ import { listWorkspacePackages } from './lib/workspace.ts';
14
+
14
15
  const root = fileURLToPath(new URL('..', import.meta.url));
15
16
 
16
- /**
17
- * 本仓的可再生面: 构建产物、锁文件与依赖树本身
18
- * (与 buffett `clean` 的 `dist` / `bunlock` / `modules` 任务对应; 树小, 连树清才修得掉
19
- * bun install 只对齐、修不了的深脏: 被改过的包内容、平台错配残留等)。
20
- * 构建产物: CLI 包产物 (packages/{{NAME}}-cli/dist) 在本脚本清理面内;
21
- * API 包产物 (packages/{{NAME}}/dist) 不在清理面, 重建经 `bun run build`;
22
- * 依赖树走 hoisted linker (bunfig.toml), 集中在一处, 清根即够。
23
- */
24
- for (const target of [
25
- 'packages/{{NAME}}-cli/dist',
26
- 'bun.lock',
27
- 'node_modules',
28
- ]) {
29
- rmSync(`${root}${target}`, { recursive: true, force: true });
17
+ const targets: string[] = [];
18
+ for (const pkg of listWorkspacePackages(root)) {
19
+ for (const sub of ['dist', 'assets', '.turbo']) {
20
+ const rel = `${pkg.dir}/${sub}`;
21
+ if (existsSync(join(root, rel))) targets.push(rel);
22
+ }
23
+ }
24
+ for (const rootTarget of ['.turbo', 'bun.lock', 'node_modules']) {
25
+ if (existsSync(join(root, rootTarget))) targets.push(rootTarget);
26
+ }
27
+
28
+ for (const target of targets) {
29
+ rmSync(join(root, target), { recursive: true, force: true });
30
30
  console.log(`[safe-install] 已清理: ${target}`);
31
31
  }
32
32
 
33
33
  console.log('[safe-install] 重新安装依赖 (bun install)…');
34
34
  const result = spawnSync('bun', ['install'], { cwd: root, stdio: 'inherit' });
35
+ const code = result.status ?? 1;
36
+ if (code !== 0) {
37
+ console.error(`[safe-install] bun install 失败 (退出码 ${code})`);
38
+ process.exit(code);
39
+ }
35
40
 
36
41
  console.log(
37
- '[safe-install] CLI 包 dist 已清 (API 包 dist 不在清理面); 跑测试 / 推送前先 bun run build 重建产物',
42
+ '[safe-install] 重装完成; 跑测试 / 推送前先执行 bun run build 重建全部包的 dist 与 assets',
38
43
  );
39
- process.exit(result.status ?? 1);
44
+ process.exit(0);