create-clis 0.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 (104) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +113 -0
  3. package/README.zh-CN.md +113 -0
  4. package/assets/manifest.json +99 -0
  5. package/assets/template/.editorconfig +12 -0
  6. package/assets/template/.gitattributes +18 -0
  7. package/assets/template/.github/ISSUE_TEMPLATE/bug_report.yml +86 -0
  8. package/assets/template/.github/ISSUE_TEMPLATE/feature_request.yml +45 -0
  9. package/assets/template/.github/PULL_REQUEST_TEMPLATE.md +26 -0
  10. package/assets/template/.github/workflows/ci.yml +42 -0
  11. package/assets/template/.github/workflows/release.yml +65 -0
  12. package/assets/template/.gitignorerc.json +21 -0
  13. package/assets/template/.nvmrc +1 -0
  14. package/assets/template/.oxlintrc.json +52 -0
  15. package/assets/template/.prettierignore +11 -0
  16. package/assets/template/.prettierrc +11 -0
  17. package/assets/template/.releaserc.json +17 -0
  18. package/assets/template/.vscode/extensions.json +8 -0
  19. package/assets/template/.vscode/launch.json +13 -0
  20. package/assets/template/.vscode/settings.json +14 -0
  21. package/assets/template/AGENTS.md +25 -0
  22. package/assets/template/CODE_OF_CONDUCT.md +91 -0
  23. package/assets/template/CONTRIBUTING.md +85 -0
  24. package/assets/template/CONTRIBUTING.zh-CN.md +85 -0
  25. package/assets/template/LICENSE +21 -0
  26. package/assets/template/README.md +22 -0
  27. package/assets/template/README.zh-CN.md +22 -0
  28. package/assets/template/SECURITY.md +21 -0
  29. package/assets/template/_gitignore +409 -0
  30. package/assets/template/_npmrc +6 -0
  31. package/assets/template/bunfig.toml +16 -0
  32. package/assets/template/docs/README.md +30 -0
  33. package/assets/template/docs/adrs/README.md +7 -0
  34. package/assets/template/docs/designs/README.md +11 -0
  35. package/assets/template/docs/designs/tech-debt.md +22 -0
  36. package/assets/template/docs/development.md +59 -0
  37. package/assets/template/lefthook.yml +55 -0
  38. package/assets/template/package.json +38 -0
  39. package/assets/template/packages/{{NAME}}/CHANGELOG.md +3 -0
  40. package/assets/template/packages/{{NAME}}/LICENSE +21 -0
  41. package/assets/template/packages/{{NAME}}/README.md +33 -0
  42. package/assets/template/packages/{{NAME}}/README.zh-CN.md +33 -0
  43. package/assets/template/packages/{{NAME}}/docs/README.md +21 -0
  44. package/assets/template/packages/{{NAME}}/package.json +40 -0
  45. package/assets/template/packages/{{NAME}}/scripts/build.ts +82 -0
  46. package/assets/template/packages/{{NAME}}/scripts/verify-release.test.ts +214 -0
  47. package/assets/template/packages/{{NAME}}/scripts/verify-release.ts +152 -0
  48. package/assets/template/packages/{{NAME}}/src/index.ts +15 -0
  49. package/assets/template/packages/{{NAME}}/tsconfig.build.json +12 -0
  50. package/assets/template/packages/{{NAME}}/tsconfig.json +5 -0
  51. package/assets/template/packages/{{NAME}}-cli/CHANGELOG.md +3 -0
  52. package/assets/template/packages/{{NAME}}-cli/LICENSE +21 -0
  53. package/assets/template/packages/{{NAME}}-cli/README.md +35 -0
  54. package/assets/template/packages/{{NAME}}-cli/README.zh-CN.md +35 -0
  55. package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}} +30 -0
  56. package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.cmd +30 -0
  57. package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.d.mts +23 -0
  58. package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.mjs +212 -0
  59. package/assets/template/packages/{{NAME}}-cli/docs/README.md +21 -0
  60. package/assets/template/packages/{{NAME}}-cli/package.json +48 -0
  61. package/assets/template/packages/{{NAME}}-cli/scripts/release-artifact.ts +141 -0
  62. package/assets/template/packages/{{NAME}}-cli/scripts/release.fixtures.ts +70 -0
  63. package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.test.ts +444 -0
  64. package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.ts +72 -0
  65. package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.test.ts +126 -0
  66. package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.ts +81 -0
  67. package/assets/template/packages/{{NAME}}-cli/src/cli.ts +36 -0
  68. package/assets/template/packages/{{NAME}}-cli/tsconfig.json +4 -0
  69. package/assets/template/scripts/ci.ts +163 -0
  70. package/assets/template/scripts/install-git-hooks.mjs +122 -0
  71. package/assets/template/scripts/install-git-hooks.test.ts +418 -0
  72. package/assets/template/scripts/lib/git-env.ts +21 -0
  73. package/assets/template/scripts/lib/release-verify.ts +431 -0
  74. package/assets/template/scripts/lib/tmp-root.ts +17 -0
  75. package/assets/template/scripts/lib/workspace.test.ts +92 -0
  76. package/assets/template/scripts/lib/workspace.ts +100 -0
  77. package/assets/template/scripts/lint-doc-examples.test.ts +129 -0
  78. package/assets/template/scripts/lint-doc-examples.ts +249 -0
  79. package/assets/template/scripts/lint-doc-references.test.ts +158 -0
  80. package/assets/template/scripts/lint-doc-references.ts +308 -0
  81. package/assets/template/scripts/lint-doc-shared.ts +59 -0
  82. package/assets/template/scripts/lint-stray-backups.test.ts +53 -0
  83. package/assets/template/scripts/lint-stray-backups.ts +39 -0
  84. package/assets/template/scripts/npm-trust-guard.test.ts +253 -0
  85. package/assets/template/scripts/npm-trust-guard.ts +533 -0
  86. package/assets/template/scripts/safe-install.ts +39 -0
  87. package/assets/template/scripts/transcription/api-harness.ts +219 -0
  88. package/assets/template/scripts/transcription/compare.ts +421 -0
  89. package/assets/template/scripts/transcription/corpus.ts +208 -0
  90. package/assets/template/scripts/transcription/fixture.ts +290 -0
  91. package/assets/template/scripts/transcription/make-mutants.ts +425 -0
  92. package/assets/template/scripts/transcription/report.ts +86 -0
  93. package/assets/template/scripts/transcription/run-conformance.ts +344 -0
  94. package/assets/template/scripts/transcription/validate-coverage.test.ts +119 -0
  95. package/assets/template/scripts/transcription/validate-coverage.ts +144 -0
  96. package/assets/template/scripts/transcription/validate.ts +366 -0
  97. package/assets/template/tsconfig.json +46 -0
  98. package/assets/template/turbo.jsonc +60 -0
  99. package/bin/create-clis +35 -0
  100. package/bin/create-clis.cmd +32 -0
  101. package/bin/create-clis.mjs +219 -0
  102. package/dist/create-clis.js +87682 -0
  103. package/dist/manifest.json +7 -0
  104. package/package.json +57 -0
@@ -0,0 +1,253 @@
1
+ /**
2
+ * 发布凭据闸门单测: 纯函数层 (远端 URL 派生 / npm trust 输出解析 / 登记对账 / OIDC 环境判定 /
3
+ * 预检核对面过滤)。
4
+ */
5
+ import { describe, expect, test } from 'bun:test';
6
+
7
+ import type { WorkspacePackage } from './lib/workspace.ts';
8
+ import {
9
+ classifyOidcEnv,
10
+ deriveRepoFromRemoteUrl,
11
+ diffTrust,
12
+ filterPublishable,
13
+ parseTrustListOutput,
14
+ ptyWrapArgs,
15
+ } from './npm-trust-guard.ts';
16
+
17
+ /** 终端控制符经 fromCharCode 构造: 防源码里的转义序列在落盘时被求值成真实不可见字符 */
18
+ const ESC = String.fromCharCode(27);
19
+ const green = (text: string): string => `${ESC}[32m${text}${ESC}[39m`;
20
+
21
+ describe('deriveRepoFromRemoteUrl', () => {
22
+ test('SSH 形态', () => {
23
+ expect(deriveRepoFromRemoteUrl('git@github.com:{{OWNER}}/{{NAME}}.git')).toBe(
24
+ '{{OWNER}}/{{NAME}}',
25
+ );
26
+ });
27
+
28
+ test('HTTPS 形态 (带与不带 .git 后缀)', () => {
29
+ expect(deriveRepoFromRemoteUrl('{{REPO_URL}}.git')).toBe(
30
+ '{{OWNER}}/{{NAME}}',
31
+ );
32
+ expect(deriveRepoFromRemoteUrl('{{REPO_URL}}')).toBe(
33
+ '{{OWNER}}/{{NAME}}',
34
+ );
35
+ });
36
+
37
+ test('非 GitHub 远端与空串返回 null', () => {
38
+ expect(deriveRepoFromRemoteUrl('git@gitlab.com:foo/bar.git')).toBeNull();
39
+ expect(deriveRepoFromRemoteUrl('')).toBeNull();
40
+ });
41
+ });
42
+
43
+ describe('parseTrustListOutput', () => {
44
+ test('解析实测形态 (键值行, 值带 ANSI 着色)', () => {
45
+ const raw = [
46
+ `type: ${green('github')}`,
47
+ `id: ${green('625b8bc6-f738-4aac-9a92-70ba2f01ce80')}`,
48
+ `file: ${green('release.yml')}`,
49
+ `repository: ${green('{{OWNER}}/{{NAME}}')}`,
50
+ `environment: ${green('release')}`,
51
+ 'permissions: publish, stage publish',
52
+ ].join('\n');
53
+ expect(parseTrustListOutput(raw)).toEqual({
54
+ type: 'github',
55
+ id: '625b8bc6-f738-4aac-9a92-70ba2f01ce80',
56
+ file: 'release.yml',
57
+ repository: '{{OWNER}}/{{NAME}}',
58
+ environment: 'release',
59
+ });
60
+ });
61
+
62
+ test('混入认证提示等噪音行不影响解析; 缺行字段落成 null', () => {
63
+ const raw = [
64
+ 'npm notice npm tokens that bypass 2FA are being restricted',
65
+ 'Authenticate your account at:',
66
+ 'https://www.npmjs.com/auth/cli/xxx',
67
+ 'Press ENTER to open in the browser...',
68
+ '',
69
+ 'type: github',
70
+ 'repository: {{OWNER}}/{{NAME}}',
71
+ 'file: release.yml',
72
+ ].join('\n');
73
+ expect(parseTrustListOutput(raw)).toEqual({
74
+ type: 'github',
75
+ id: null,
76
+ file: 'release.yml',
77
+ repository: '{{OWNER}}/{{NAME}}',
78
+ environment: null,
79
+ });
80
+ });
81
+
82
+ test('未见 repository 行视为形状不符 (解析不出可信条目)', () => {
83
+ expect(parseTrustListOutput('npm error code EOTP\n')).toBeNull();
84
+ expect(parseTrustListOutput('')).toBeNull();
85
+ });
86
+
87
+ test('spinner 的 CSI 光标控制序列 (非 m 结尾) 不残留进键值行', () => {
88
+ const csi = (tail: string): string => `${ESC}[1G${ESC}[0K${tail}`;
89
+ const raw = `${csi('type: github')}\r\n${csi('repository: {{OWNER}}/{{NAME}}')}\r\n${csi('file: release.yml')}\r\n`;
90
+ expect(parseTrustListOutput(raw)).toEqual({
91
+ type: 'github',
92
+ id: null,
93
+ file: 'release.yml',
94
+ repository: '{{OWNER}}/{{NAME}}',
95
+ environment: null,
96
+ });
97
+ });
98
+
99
+ test('script pty 的 ^D + 退格开场与 CRLF 行尾不影响解析', () => {
100
+ const raw =
101
+ [
102
+ `${String.fromCharCode(4, 8, 8)}type: github`,
103
+ 'repository: {{OWNER}}/{{NAME}}',
104
+ 'file: release.yml',
105
+ ].join('\r\n') + '\r\n';
106
+ expect(parseTrustListOutput(raw)).toEqual({
107
+ type: 'github',
108
+ id: null,
109
+ file: 'release.yml',
110
+ repository: '{{OWNER}}/{{NAME}}',
111
+ environment: null,
112
+ });
113
+ });
114
+ });
115
+
116
+ describe('diffTrust', () => {
117
+ const expected = {
118
+ repository: '{{OWNER}}/{{NAME}}',
119
+ file: 'release.yml',
120
+ environment: 'release',
121
+ };
122
+
123
+ test('全一致返回空列表', () => {
124
+ expect(
125
+ diffTrust(
126
+ {
127
+ type: 'github',
128
+ id: 'x',
129
+ file: 'release.yml',
130
+ repository: '{{OWNER}}/{{NAME}}',
131
+ environment: 'release',
132
+ },
133
+ expected,
134
+ ),
135
+ ).toEqual([]);
136
+ });
137
+
138
+ test('repository 失配逐条列出 (改名事故的典型形态)', () => {
139
+ // 失配样本刻意取中性名 (不与仓库自身的名字重合): 它是「另一个仓库名」的任意代表,
140
+ // 判定链只比较两值是否相等, 取值本身无语义; 与仓库重名反而会让模板侧的两个值塌缩成同一个
141
+ const problems = diffTrust(
142
+ {
143
+ type: 'github',
144
+ id: 'x',
145
+ file: 'release.yml',
146
+ repository: '{{OWNER}}/legacy-repo',
147
+ environment: 'release',
148
+ },
149
+ expected,
150
+ );
151
+ expect(problems).toHaveLength(1);
152
+ expect(problems[0]).toContain('{{OWNER}}/legacy-repo');
153
+ expect(problems[0]).toContain('{{OWNER}}/{{NAME}}');
154
+ });
155
+
156
+ test('file 与 environment 失配一并列出 (缺失值有占位)', () => {
157
+ const problems = diffTrust(
158
+ {
159
+ type: 'github',
160
+ id: 'x',
161
+ file: 'publish.yml',
162
+ repository: '{{OWNER}}/{{NAME}}',
163
+ environment: null,
164
+ },
165
+ expected,
166
+ );
167
+ expect(problems).toHaveLength(2);
168
+ expect(problems[1]).toContain('(未设置)');
169
+ });
170
+ });
171
+
172
+ describe('classifyOidcEnv', () => {
173
+ test('Actions 环境且 OIDC 变量齐备 → ready', () => {
174
+ expect(
175
+ classifyOidcEnv({
176
+ GITHUB_ACTIONS: 'true',
177
+ ACTIONS_ID_TOKEN_REQUEST_URL: 'https://example.test/oidc',
178
+ ACTIONS_ID_TOKEN_REQUEST_TOKEN: 'token',
179
+ }),
180
+ ).toBe('ready');
181
+ });
182
+
183
+ test('Actions 环境缺 OIDC 变量 → missing-permission (工作流漏配 id-token)', () => {
184
+ expect(classifyOidcEnv({ GITHUB_ACTIONS: 'true' })).toBe(
185
+ 'missing-permission',
186
+ );
187
+ });
188
+
189
+ test('本地环境 → not-ci', () => {
190
+ expect(classifyOidcEnv({})).toBe('not-ci');
191
+ });
192
+ });
193
+
194
+ describe('ptyWrapArgs', () => {
195
+ test('darwin 用 script -q /dev/null 包裹 (退出码透传已实测)', () => {
196
+ expect(ptyWrapArgs('darwin', 'npm', ['trust', 'list', '@x/y'])).toEqual({
197
+ command: 'script',
198
+ args: ['-q', '/dev/null', 'npm', 'trust', 'list', '@x/y'],
199
+ });
200
+ });
201
+
202
+ test('linux 用 script -qec 命令串 (实用输入受控无空白)', () => {
203
+ expect(
204
+ ptyWrapArgs('linux', 'npm', ['trust', 'github', '--repo', '{{OWNER}}/{{NAME}}']),
205
+ ).toEqual({
206
+ command: 'script',
207
+ args: ['-qec', 'npm trust github --repo {{OWNER}}/{{NAME}}', '/dev/null'],
208
+ });
209
+ });
210
+
211
+ test('win32 无 script, 降级直跑原命令', () => {
212
+ expect(ptyWrapArgs('win32', 'npm.cmd', ['trust', 'list'])).toEqual({
213
+ command: 'npm.cmd',
214
+ args: ['trust', 'list'],
215
+ });
216
+ });
217
+ });
218
+
219
+ describe('filterPublishable', () => {
220
+ /** 最小 WorkspacePackage 夹具 (过滤只看 manifest.private) */
221
+ const pkg = (name: string, isPrivate = false): WorkspacePackage => ({
222
+ name,
223
+ dir: `packages/${name}`,
224
+ manifest: isPrivate ? { name, private: true } : { name },
225
+ });
226
+
227
+ test('private 包被排除, 非 private 包保留 (核对面与发布链 ignorePrivate 同源)', () => {
228
+ const packages = [
229
+ pkg('@x/api'),
230
+ pkg('@x/cli'),
231
+ pkg('@x/private-lib', true),
232
+ ];
233
+ expect(filterPublishable(packages).map((item) => item.name)).toEqual([
234
+ '@x/api',
235
+ '@x/cli',
236
+ ]);
237
+ });
238
+
239
+ test('private 字段缺失或非 true (如 false) 均视为可发布, 不误伤', () => {
240
+ const packages: WorkspacePackage[] = [
241
+ pkg('@x/api'),
242
+ {
243
+ name: '@x/flag',
244
+ dir: 'packages/flag',
245
+ manifest: { name: '@x/flag', private: false },
246
+ },
247
+ ];
248
+ expect(filterPublishable(packages).map((item) => item.name)).toEqual([
249
+ '@x/api',
250
+ '@x/flag',
251
+ ]);
252
+ });
253
+ });