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.
- package/LICENSE +21 -0
- package/README.md +113 -0
- package/README.zh-CN.md +113 -0
- package/assets/manifest.json +99 -0
- package/assets/template/.editorconfig +12 -0
- package/assets/template/.gitattributes +18 -0
- package/assets/template/.github/ISSUE_TEMPLATE/bug_report.yml +86 -0
- package/assets/template/.github/ISSUE_TEMPLATE/feature_request.yml +45 -0
- package/assets/template/.github/PULL_REQUEST_TEMPLATE.md +26 -0
- package/assets/template/.github/workflows/ci.yml +42 -0
- package/assets/template/.github/workflows/release.yml +65 -0
- package/assets/template/.gitignorerc.json +21 -0
- package/assets/template/.nvmrc +1 -0
- package/assets/template/.oxlintrc.json +52 -0
- package/assets/template/.prettierignore +11 -0
- package/assets/template/.prettierrc +11 -0
- package/assets/template/.releaserc.json +17 -0
- package/assets/template/.vscode/extensions.json +8 -0
- package/assets/template/.vscode/launch.json +13 -0
- package/assets/template/.vscode/settings.json +14 -0
- package/assets/template/AGENTS.md +25 -0
- package/assets/template/CODE_OF_CONDUCT.md +91 -0
- package/assets/template/CONTRIBUTING.md +85 -0
- package/assets/template/CONTRIBUTING.zh-CN.md +85 -0
- package/assets/template/LICENSE +21 -0
- package/assets/template/README.md +22 -0
- package/assets/template/README.zh-CN.md +22 -0
- package/assets/template/SECURITY.md +21 -0
- package/assets/template/_gitignore +409 -0
- package/assets/template/_npmrc +6 -0
- package/assets/template/bunfig.toml +16 -0
- package/assets/template/docs/README.md +30 -0
- package/assets/template/docs/adrs/README.md +7 -0
- package/assets/template/docs/designs/README.md +11 -0
- package/assets/template/docs/designs/tech-debt.md +22 -0
- package/assets/template/docs/development.md +59 -0
- package/assets/template/lefthook.yml +55 -0
- package/assets/template/package.json +38 -0
- package/assets/template/packages/{{NAME}}/CHANGELOG.md +3 -0
- package/assets/template/packages/{{NAME}}/LICENSE +21 -0
- package/assets/template/packages/{{NAME}}/README.md +33 -0
- package/assets/template/packages/{{NAME}}/README.zh-CN.md +33 -0
- package/assets/template/packages/{{NAME}}/docs/README.md +21 -0
- package/assets/template/packages/{{NAME}}/package.json +40 -0
- package/assets/template/packages/{{NAME}}/scripts/build.ts +82 -0
- package/assets/template/packages/{{NAME}}/scripts/verify-release.test.ts +214 -0
- package/assets/template/packages/{{NAME}}/scripts/verify-release.ts +152 -0
- package/assets/template/packages/{{NAME}}/src/index.ts +15 -0
- package/assets/template/packages/{{NAME}}/tsconfig.build.json +12 -0
- package/assets/template/packages/{{NAME}}/tsconfig.json +5 -0
- package/assets/template/packages/{{NAME}}-cli/CHANGELOG.md +3 -0
- package/assets/template/packages/{{NAME}}-cli/LICENSE +21 -0
- package/assets/template/packages/{{NAME}}-cli/README.md +35 -0
- package/assets/template/packages/{{NAME}}-cli/README.zh-CN.md +35 -0
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}} +30 -0
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.cmd +30 -0
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.d.mts +23 -0
- package/assets/template/packages/{{NAME}}-cli/bin/{{BIN_NAME}}.mjs +212 -0
- package/assets/template/packages/{{NAME}}-cli/docs/README.md +21 -0
- package/assets/template/packages/{{NAME}}-cli/package.json +48 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/release-artifact.ts +141 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/release.fixtures.ts +70 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.test.ts +444 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/verify-release.ts +72 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.test.ts +126 -0
- package/assets/template/packages/{{NAME}}-cli/scripts/write-dist-manifest.ts +81 -0
- package/assets/template/packages/{{NAME}}-cli/src/cli.ts +36 -0
- package/assets/template/packages/{{NAME}}-cli/tsconfig.json +4 -0
- package/assets/template/scripts/ci.ts +163 -0
- package/assets/template/scripts/install-git-hooks.mjs +122 -0
- package/assets/template/scripts/install-git-hooks.test.ts +418 -0
- package/assets/template/scripts/lib/git-env.ts +21 -0
- package/assets/template/scripts/lib/release-verify.ts +431 -0
- package/assets/template/scripts/lib/tmp-root.ts +17 -0
- package/assets/template/scripts/lib/workspace.test.ts +92 -0
- package/assets/template/scripts/lib/workspace.ts +100 -0
- package/assets/template/scripts/lint-doc-examples.test.ts +129 -0
- package/assets/template/scripts/lint-doc-examples.ts +249 -0
- package/assets/template/scripts/lint-doc-references.test.ts +158 -0
- package/assets/template/scripts/lint-doc-references.ts +308 -0
- package/assets/template/scripts/lint-doc-shared.ts +59 -0
- package/assets/template/scripts/lint-stray-backups.test.ts +53 -0
- package/assets/template/scripts/lint-stray-backups.ts +39 -0
- package/assets/template/scripts/npm-trust-guard.test.ts +253 -0
- package/assets/template/scripts/npm-trust-guard.ts +533 -0
- package/assets/template/scripts/safe-install.ts +39 -0
- package/assets/template/scripts/transcription/api-harness.ts +219 -0
- package/assets/template/scripts/transcription/compare.ts +421 -0
- package/assets/template/scripts/transcription/corpus.ts +208 -0
- package/assets/template/scripts/transcription/fixture.ts +290 -0
- package/assets/template/scripts/transcription/make-mutants.ts +425 -0
- package/assets/template/scripts/transcription/report.ts +86 -0
- package/assets/template/scripts/transcription/run-conformance.ts +344 -0
- package/assets/template/scripts/transcription/validate-coverage.test.ts +119 -0
- package/assets/template/scripts/transcription/validate-coverage.ts +144 -0
- package/assets/template/scripts/transcription/validate.ts +366 -0
- package/assets/template/tsconfig.json +46 -0
- package/assets/template/turbo.jsonc +60 -0
- package/bin/create-clis +35 -0
- package/bin/create-clis.cmd +32 -0
- package/bin/create-clis.mjs +219 -0
- package/dist/create-clis.js +87682 -0
- package/dist/manifest.json +7 -0
- package/package.json +57 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 变异生成器 (反向验收的自证件): 从两包源码 (坐标由 workspaces 派生, 见 scripts/lib/workspace.ts) 复制一份实现
|
|
3
|
+
* 并注入单点缺陷, 生成到 mutants/gen-<id>/; 副本自带 node_modules 链接 (指向 api 副本), 可独立运行。
|
|
4
|
+
*
|
|
5
|
+
* 用途 (变异自证): 语料必须能抓住每一个 mutant; 抓不住 = 语料盲区 (或该 mutant 定义过弱),
|
|
6
|
+
* 不是「实现没问题」。注入面见下方 MUTANTS 清单 (重跑纪律见 docs/sweep/protocol/README.md「维护规则」),
|
|
7
|
+
* 每个 mutant 只做一行级补丁。
|
|
8
|
+
*
|
|
9
|
+
* 幂等: 每次重建自己的 gen-* 目录, 并清掉清单之外的陈旧 gen-*; 锚点失配 (源码在漂移) 时显式
|
|
10
|
+
* 失败并报出文件, 严禁静默产出一份「没注入缺陷」的副本 (那会把反向验收变成永远绿的空转)。
|
|
11
|
+
*
|
|
12
|
+
* 用法: bun scripts/transcription/make-mutants.ts [--help]
|
|
13
|
+
*
|
|
14
|
+
* 产物结构: mutants/gen-<id>/<两包目录>/src/*.ts (注入后的实现, 目录名由 workspaces 派生)
|
|
15
|
+
* + mutants/gen-<id>/scripts/transcription/api-harness.ts (套件参考 harness 的副本, 指向副本源码)
|
|
16
|
+
* + mutants/gen-<id>/tsconfig.json (空文件: 切断对仓库根 tsconfig paths 的继承, 防包名 import 被劫持回真单源)
|
|
17
|
+
* + mutants/gen-<id>/<api 包目录>/package.json (改写为源码导出式; cli 侧照抄真包, 副本自足可跑)
|
|
18
|
+
* + mutants/gen-<id>/mutant.json (注入记录)。
|
|
19
|
+
* 运行某 mutant (cli 面): bun scripts/transcription/mutants/gen-<id>/<cli 包目录>/src/cli.ts
|
|
20
|
+
* 运行某 mutant (api 面): --api-target "bun scripts/transcription/mutants/gen-<id>/scripts/transcription/api-harness.ts"
|
|
21
|
+
* (两者均与 run-conformance.ts 配合)。
|
|
22
|
+
*/
|
|
23
|
+
import {
|
|
24
|
+
mkdir,
|
|
25
|
+
readFile,
|
|
26
|
+
readdir,
|
|
27
|
+
rm,
|
|
28
|
+
stat,
|
|
29
|
+
symlink,
|
|
30
|
+
writeFile,
|
|
31
|
+
} from 'node:fs/promises';
|
|
32
|
+
import { dirname, join, resolve } from 'node:path';
|
|
33
|
+
import { fileURLToPath } from 'node:url';
|
|
34
|
+
|
|
35
|
+
import { resolveCliAndApi } from '../lib/workspace.ts';
|
|
36
|
+
|
|
37
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
38
|
+
/** 仓库根 (scripts/transcription/ 上溯两级): 两包源码与配置的定位基准 */
|
|
39
|
+
const REPO_ROOT = resolve(HERE, '..', '..');
|
|
40
|
+
/** 两包坐标: 由根 package.json 的 workspaces 派生 (包目录 / 包名变更时本文件零修改) */
|
|
41
|
+
const { api: API_PKG, cli: CLI_PKG } = resolveCliAndApi(REPO_ROOT);
|
|
42
|
+
/** 被测源: 两包的 src (mutant 是整仓实现的注入副本) 与各自 package.json (副本自足可跑) */
|
|
43
|
+
const API_SRC_DIR = join(REPO_ROOT, API_PKG.dir, 'src');
|
|
44
|
+
const CLI_SRC_DIR = join(REPO_ROOT, CLI_PKG.dir, 'src');
|
|
45
|
+
const API_PKG_JSON = join(REPO_ROOT, API_PKG.dir, 'package.json');
|
|
46
|
+
const CLI_PKG_JSON = join(REPO_ROOT, CLI_PKG.dir, 'package.json');
|
|
47
|
+
/** api 包名: cli 副本经同名 node_modules 链接解析到 api 副本 (就近优先于仓库根真包) */
|
|
48
|
+
const API_PKG_NAME = API_PKG.name;
|
|
49
|
+
|
|
50
|
+
/** mutant 产物根 (本脚本独占命名空间; 其下 gen-* 已由 .gitignore 忽略) */
|
|
51
|
+
const MUTANTS_DIR = join(HERE, 'mutants');
|
|
52
|
+
|
|
53
|
+
/** 生成目录前缀 (本脚本的产物命名空间, 清理只动这个前缀) */
|
|
54
|
+
const GEN_PREFIX = 'gen-';
|
|
55
|
+
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
// mutant 清单 (每个 mutant 只注入一处; effect 与 expectCaughtBy 是变异自证时的核对依据)
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
interface PatchSpec {
|
|
61
|
+
/** 源文件名 (两包 src/ 下, 含 .ts; 名字在两包内不重名, 定位见 locatePatchTarget) */
|
|
62
|
+
file: string;
|
|
63
|
+
/** 注入锚点: 必须在目标文件中恰好出现一次, 否则脚本失败 (源码漂移的显式信号) */
|
|
64
|
+
find: string;
|
|
65
|
+
replace: string;
|
|
66
|
+
/** 这行补丁在行为面上的效果 (人读) */
|
|
67
|
+
effect: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface MutantSpec {
|
|
71
|
+
id: string;
|
|
72
|
+
title: string;
|
|
73
|
+
/** 预期抓住它的语料类型; 变异自证时逐条核对 */
|
|
74
|
+
expectCaughtBy: string;
|
|
75
|
+
patches: PatchSpec[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const MUTANTS: MutantSpec[] = [
|
|
79
|
+
{
|
|
80
|
+
id: 'prune-negated',
|
|
81
|
+
title: '剪枝谓词取反',
|
|
82
|
+
expectCaughtBy: '任一命中 node_modules 的用例 (stdoutExact / fs 断言)',
|
|
83
|
+
patches: [
|
|
84
|
+
{
|
|
85
|
+
file: 'scan-parallel.ts',
|
|
86
|
+
find: ' if (name === NODE_MODULES) {',
|
|
87
|
+
replace: ' if (name !== NODE_MODULES) {',
|
|
88
|
+
effect:
|
|
89
|
+
'命中判定反转: 普通目录被当作命中记录并剪枝, node_modules 反被下钻, 清单全面错乱',
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
id: 'exit-swallowed',
|
|
95
|
+
title: '退出码吞掉',
|
|
96
|
+
expectCaughtBy:
|
|
97
|
+
'期望 exitCode 为 1 的用例 (参数错误 / 配置损坏 / 删除失败 / 无配置 --yes)',
|
|
98
|
+
patches: [
|
|
99
|
+
{
|
|
100
|
+
file: 'cli.ts',
|
|
101
|
+
find: 'process.exitCode = await main();',
|
|
102
|
+
replace: 'await main();',
|
|
103
|
+
effect: 'main 的返回值被丢弃, 全部错误路径退出码退化为 0 (恒成功)',
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
id: 'sort-missing',
|
|
109
|
+
title: '排序缺失',
|
|
110
|
+
expectCaughtBy: '≥2 个命中的用例 (stdoutExact 逐字节比较), 或复现性三连跑',
|
|
111
|
+
patches: [
|
|
112
|
+
{
|
|
113
|
+
file: 'scan-parallel.ts',
|
|
114
|
+
find: ' const hits = [...hitsByRealTarget.values()].sort(compareTarget);',
|
|
115
|
+
replace: ' const hits = [...hitsByRealTarget.values()];',
|
|
116
|
+
effect:
|
|
117
|
+
'命中序退化为并发完成序 (不确定), 与「按 target 升序」的输出契约相悖',
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
id: 'exclude-silent',
|
|
123
|
+
title: '排除静默失效',
|
|
124
|
+
expectCaughtBy:
|
|
125
|
+
'使用 exclude (配置 exclude 或 --exclude) 的用例: 被排除子树重新出现在清单里',
|
|
126
|
+
patches: [
|
|
127
|
+
{
|
|
128
|
+
file: 'scan-parallel.ts',
|
|
129
|
+
find: ' if (exclude.has(name)) {',
|
|
130
|
+
replace: ' if (false) {',
|
|
131
|
+
effect:
|
|
132
|
+
'排除判定被短路: 名单静默失效, 被排除子树照常扫出, 且无任何提示',
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
id: 'message-removed',
|
|
138
|
+
title: '提示语删改',
|
|
139
|
+
expectCaughtBy:
|
|
140
|
+
'空结果用例 (stdoutExact / stdoutContains 对「未发现」文案的断言)',
|
|
141
|
+
patches: [
|
|
142
|
+
{
|
|
143
|
+
file: 'render.ts',
|
|
144
|
+
find: '`${NEUTRAL_BLOCK} 未发现 node_modules`',
|
|
145
|
+
replace: '`${NEUTRAL_BLOCK} 无结果`',
|
|
146
|
+
effect: '空结果中性提示文案被改, 用户面文案与契约不符',
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
id: 'size-unit-wrong',
|
|
152
|
+
title: '体积计数单位错',
|
|
153
|
+
expectCaughtBy:
|
|
154
|
+
'带体积数字断言的用例; 注意: 仅 du 可用平台 (unix) 生效, Windows 走 js 候选时抓不住',
|
|
155
|
+
patches: [
|
|
156
|
+
{
|
|
157
|
+
file: 'size-du.ts',
|
|
158
|
+
find: ' sizes.set(path, Number(sizeKiB) * 1024);',
|
|
159
|
+
replace: ' sizes.set(path, Number(sizeKiB) * 1000);',
|
|
160
|
+
effect: 'du 的 Ki 单位换算错误 (1024 → 1000), 展示体积全线偏移',
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
},
|
|
164
|
+
];
|
|
165
|
+
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
// 实现
|
|
168
|
+
// ---------------------------------------------------------------------------
|
|
169
|
+
|
|
170
|
+
const HELP_TEXT = [
|
|
171
|
+
'用法: bun scripts/transcription/make-mutants.ts',
|
|
172
|
+
'',
|
|
173
|
+
' --help 显示本帮助',
|
|
174
|
+
'',
|
|
175
|
+
'产出: mutants/gen-<id>/<两包目录>/src/*.ts + mutant.json (幂等重建; 目录名由 workspaces 派生)',
|
|
176
|
+
].join('\n');
|
|
177
|
+
|
|
178
|
+
/** 统计 needle 在 text 中的出现次数 (不重叠) */
|
|
179
|
+
function countOccurrences(text: string, needle: string): number {
|
|
180
|
+
let count = 0;
|
|
181
|
+
let index = text.indexOf(needle);
|
|
182
|
+
while (index !== -1) {
|
|
183
|
+
count += 1;
|
|
184
|
+
index = text.indexOf(needle, index + needle.length);
|
|
185
|
+
}
|
|
186
|
+
return count;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** 找 needle 首次出现的行号 (从 1 起) */
|
|
190
|
+
const lineOf = (text: string, needle: string): number =>
|
|
191
|
+
text.slice(0, text.indexOf(needle)).split('\n').length;
|
|
192
|
+
|
|
193
|
+
/** 取 src 下应复制的文件清单: 全部 .ts, 排除测试文件; 排序保证复制次序确定 */
|
|
194
|
+
async function listSourceFiles(srcDir: string): Promise<string[]> {
|
|
195
|
+
const entries = await readdir(srcDir, { withFileTypes: true });
|
|
196
|
+
return entries
|
|
197
|
+
.filter(
|
|
198
|
+
(entry) =>
|
|
199
|
+
entry.isFile() &&
|
|
200
|
+
entry.name.endsWith('.ts') &&
|
|
201
|
+
!entry.name.endsWith('.test.ts'),
|
|
202
|
+
)
|
|
203
|
+
.map((entry) => entry.name)
|
|
204
|
+
.sort();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
interface AppliedPatch {
|
|
208
|
+
file: string;
|
|
209
|
+
anchorLine: number;
|
|
210
|
+
find: string;
|
|
211
|
+
replace: string;
|
|
212
|
+
effect: string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 在 mutant 的两包副本中定位补丁目标文件 (文件名在两包内不重名)。
|
|
217
|
+
* 外部副作用:只读 (stat)。
|
|
218
|
+
*/
|
|
219
|
+
async function locatePatchTarget(
|
|
220
|
+
outDir: string,
|
|
221
|
+
file: string,
|
|
222
|
+
): Promise<string> {
|
|
223
|
+
for (const pkg of [API_PKG, CLI_PKG]) {
|
|
224
|
+
const candidate = join(outDir, pkg.dir, 'src', file);
|
|
225
|
+
try {
|
|
226
|
+
await stat(candidate);
|
|
227
|
+
return candidate;
|
|
228
|
+
} catch {
|
|
229
|
+
// 继续找另一包
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
throw new Error(`补丁目标不在两包副本中: ${file}`);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 生成单个 mutant: 先清掉旧目录, 复制两包源码与 package.json, 建 api 副本链接, 再逐个应用补丁。
|
|
237
|
+
* 外部副作用:删除并重建 mutants/gen-<id>/。
|
|
238
|
+
*
|
|
239
|
+
* ### 数据追踪示例
|
|
240
|
+
* ```text
|
|
241
|
+
* Input(真实 Payload)
|
|
242
|
+
* mutant = { id: 'exit-swallowed', patches: [{ file: 'cli.ts', find: 'process.exitCode = await main();', … }] }
|
|
243
|
+
* sourceFiles = ['cli.ts', 'render.ts', …] (<cli 包目录>/src 下全部非测试 .ts; api 侧同法)
|
|
244
|
+
*
|
|
245
|
+
* 步骤 1:重建目录并复制两包源码 + package.json
|
|
246
|
+
* gen-exit-swallowed/<api 包目录>/src/ = api 源文件全量副本
|
|
247
|
+
* gen-exit-swallowed/<cli 包目录>/src/ = cli 源文件全量副本
|
|
248
|
+
* gen-exit-swallowed/tsconfig.json = {} (断对仓库根 paths 的继承)
|
|
249
|
+
* gen-exit-swallowed/node_modules/<api 包名> -> ../../<api 包目录> (符号链接)
|
|
250
|
+
*
|
|
251
|
+
* 步骤 2:逐补丁校验锚点唯一后替换 (目标文件按名在两包副本中定位)
|
|
252
|
+
* 锚点出现 1 次 → 替换并记录 anchorLine (第 649 行)
|
|
253
|
+
*
|
|
254
|
+
* Output(数据契约)
|
|
255
|
+
* return 已注入的实现目录 + 注入记录 (写盘 mutant.json)
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
async function makeMutant(mutant: MutantSpec): Promise<AppliedPatch[]> {
|
|
259
|
+
const outDir = join(MUTANTS_DIR, `${GEN_PREFIX}${mutant.id}`);
|
|
260
|
+
await rm(outDir, { recursive: true, force: true });
|
|
261
|
+
await mkdir(join(outDir, API_PKG.dir, 'src'), {
|
|
262
|
+
recursive: true,
|
|
263
|
+
});
|
|
264
|
+
await mkdir(join(outDir, CLI_PKG.dir, 'src'), {
|
|
265
|
+
recursive: true,
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
for (const [srcDir, pkgDir] of [
|
|
269
|
+
[API_SRC_DIR, join(outDir, API_PKG.dir)],
|
|
270
|
+
[CLI_SRC_DIR, join(outDir, CLI_PKG.dir)],
|
|
271
|
+
] as const) {
|
|
272
|
+
for (const name of await listSourceFiles(srcDir)) {
|
|
273
|
+
await writeFile(
|
|
274
|
+
join(pkgDir, 'src', name),
|
|
275
|
+
await readFile(join(srcDir, name)),
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
// 套件的 API 参考 harness 副本: 按仓库同相对结构放入, 使 api 面语料可直接以
|
|
280
|
+
// scripts/transcription/mutants/gen-<id>/scripts/transcription/api-harness.ts 为 --api-target
|
|
281
|
+
// (其相对 import 自动解析到本副本内的 API 源码)。
|
|
282
|
+
await mkdir(join(outDir, 'scripts', 'transcription'), { recursive: true });
|
|
283
|
+
await writeFile(
|
|
284
|
+
join(outDir, 'scripts', 'transcription', 'api-harness.ts'),
|
|
285
|
+
await readFile(join(HERE, 'api-harness.ts')),
|
|
286
|
+
);
|
|
287
|
+
// 空 tsconfig 就近切断对仓库根 tsconfig 的 paths 继承: 否则 bun 会把副本 cli.ts 的包名
|
|
288
|
+
// import (即 API 包名) 解析到真 API 单源 (paths 优先于 node_modules 链接),
|
|
289
|
+
// 变异自证静默失效 (2026-09-30 实测: 缺此文件时 bun 载体对注入副本零抓取)。node 载体不读
|
|
290
|
+
// paths 可免此劫持, 但受「node_modules 下 .ts 拒绝类型剥离、副本无 dist」限制, 故变异自证
|
|
291
|
+
// 的运行时以 bun 载体为准。
|
|
292
|
+
await writeFile(join(outDir, 'tsconfig.json'), '{}\n');
|
|
293
|
+
// 副本 API 的 package.json 改写为源码导出: bun 直跑副本 .ts, 不经真包的 exports→dist
|
|
294
|
+
// (副本不含构建产物, 照抄真包 exports 会让 cli 副本的包名 import 解析失败, 2026-09-30 实测)。
|
|
295
|
+
const apiPkg = JSON.parse(await readFile(API_PKG_JSON, 'utf8')) as {
|
|
296
|
+
name?: unknown;
|
|
297
|
+
};
|
|
298
|
+
await writeFile(
|
|
299
|
+
join(outDir, API_PKG.dir, 'package.json'),
|
|
300
|
+
`${JSON.stringify(
|
|
301
|
+
{
|
|
302
|
+
name: typeof apiPkg.name === 'string' ? apiPkg.name : API_PKG_NAME,
|
|
303
|
+
type: 'module',
|
|
304
|
+
exports: { '.': './src/index.ts' },
|
|
305
|
+
},
|
|
306
|
+
null,
|
|
307
|
+
2,
|
|
308
|
+
)}\n`,
|
|
309
|
+
);
|
|
310
|
+
await writeFile(
|
|
311
|
+
join(outDir, CLI_PKG.dir, 'package.json'),
|
|
312
|
+
await readFile(CLI_PKG_JSON),
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
// cli 副本对 API 包名的解析必须落在 api 副本上 (而非宿主仓库的真包);
|
|
316
|
+
// 链接置于副本根 node_modules, 按就近解析优先于仓库根的真实 workspace 链接。
|
|
317
|
+
const apiLink = join(outDir, 'node_modules', API_PKG_NAME);
|
|
318
|
+
await mkdir(dirname(apiLink), { recursive: true });
|
|
319
|
+
await symlink(
|
|
320
|
+
process.platform === 'win32'
|
|
321
|
+
? join(outDir, API_PKG.dir)
|
|
322
|
+
: join('..', '..', API_PKG.dir),
|
|
323
|
+
apiLink,
|
|
324
|
+
process.platform === 'win32' ? 'junction' : 'dir',
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
const applied: AppliedPatch[] = [];
|
|
328
|
+
for (const patch of mutant.patches) {
|
|
329
|
+
const target = await locatePatchTarget(outDir, patch.file);
|
|
330
|
+
const text = await readFile(target, 'utf8');
|
|
331
|
+
const occurrences = countOccurrences(text, patch.find);
|
|
332
|
+
if (occurrences !== 1) {
|
|
333
|
+
throw new Error(
|
|
334
|
+
`mutant ${mutant.id}: 锚点失配 (${patch.file}, 出现 ${occurrences} 次, 期望 1 次): ${JSON.stringify(patch.find)}\n` +
|
|
335
|
+
' 源码已漂移: 更新 make-mutants.ts 里的锚点后重跑, 严禁带着失配继续。',
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
const anchorLine = lineOf(text, patch.find);
|
|
339
|
+
await writeFile(target, text.split(patch.find).join(patch.replace));
|
|
340
|
+
applied.push({ ...patch, anchorLine });
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const manifest = {
|
|
344
|
+
id: mutant.id,
|
|
345
|
+
title: mutant.title,
|
|
346
|
+
expectCaughtBy: mutant.expectCaughtBy,
|
|
347
|
+
sourceDir: `${API_PKG.dir}/src + ${CLI_PKG.dir}/src`,
|
|
348
|
+
runHint: `bun ${join(MUTANTS_DIR, `${GEN_PREFIX}${mutant.id}`, CLI_PKG.dir, 'src', 'cli.ts')}`,
|
|
349
|
+
patches: applied,
|
|
350
|
+
};
|
|
351
|
+
await writeFile(
|
|
352
|
+
join(outDir, 'mutant.json'),
|
|
353
|
+
`${JSON.stringify(manifest, null, 2)}\n`,
|
|
354
|
+
);
|
|
355
|
+
return applied;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** 清掉清单之外的陈旧 gen-* 目录 (本脚本自己的产物命名空间, 不留垃圾) */
|
|
359
|
+
async function sweepStale(knownIds: Set<string>): Promise<string[]> {
|
|
360
|
+
const removed: string[] = [];
|
|
361
|
+
let entries;
|
|
362
|
+
try {
|
|
363
|
+
entries = await readdir(MUTANTS_DIR, { withFileTypes: true });
|
|
364
|
+
} catch {
|
|
365
|
+
return removed;
|
|
366
|
+
}
|
|
367
|
+
for (const entry of entries) {
|
|
368
|
+
if (!entry.isDirectory() || !entry.name.startsWith(GEN_PREFIX)) continue;
|
|
369
|
+
if (knownIds.has(entry.name.slice(GEN_PREFIX.length))) continue;
|
|
370
|
+
await rm(join(MUTANTS_DIR, entry.name), { recursive: true, force: true });
|
|
371
|
+
removed.push(entry.name);
|
|
372
|
+
}
|
|
373
|
+
return removed;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
async function main(): Promise<number> {
|
|
377
|
+
const argv = process.argv.slice(2);
|
|
378
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
379
|
+
const arg = argv[index];
|
|
380
|
+
if (arg === '--help' || arg === '-h') {
|
|
381
|
+
process.stdout.write(`${HELP_TEXT}\n`);
|
|
382
|
+
return 0;
|
|
383
|
+
}
|
|
384
|
+
process.stderr.write(`参数错误: 未知参数 ${arg}\n\n${HELP_TEXT}\n`);
|
|
385
|
+
return 1;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
for (const srcDir of [API_SRC_DIR, CLI_SRC_DIR]) {
|
|
389
|
+
try {
|
|
390
|
+
await readdir(srcDir);
|
|
391
|
+
} catch (error) {
|
|
392
|
+
process.stderr.write(
|
|
393
|
+
`源目录不可读: ${srcDir} (${(error as Error).message})\n`,
|
|
394
|
+
);
|
|
395
|
+
return 1;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const removed = await sweepStale(new Set(MUTANTS.map((mutant) => mutant.id)));
|
|
400
|
+
for (const name of removed) {
|
|
401
|
+
process.stdout.write(`清理陈旧产物: ${name}\n`);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
for (const mutant of MUTANTS) {
|
|
405
|
+
const applied = await makeMutant(mutant);
|
|
406
|
+
process.stdout.write(`gen-${mutant.id} (${mutant.title})\n`);
|
|
407
|
+
for (const patch of applied) {
|
|
408
|
+
process.stdout.write(
|
|
409
|
+
` ${patch.file}:${patch.anchorLine} ${patch.effect}\n`,
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
process.stdout.write(
|
|
415
|
+
`\n合计 ${MUTANTS.length} 个 mutant → ${join(MUTANTS_DIR, `gen-*/${CLI_PKG.dir}/src/cli.ts`)}\n`,
|
|
416
|
+
);
|
|
417
|
+
return 0;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
try {
|
|
421
|
+
process.exitCode = await main();
|
|
422
|
+
} catch (error) {
|
|
423
|
+
process.stderr.write(`${(error as Error).message}\n`);
|
|
424
|
+
process.exitCode = 1;
|
|
425
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 转写契约套件 · 报告层。
|
|
3
|
+
*
|
|
4
|
+
* 职责: 定义用例报告的数据结构 (FailureItem / CaseReport) 与人读 / JSON 两种渲染。
|
|
5
|
+
* 报告字段与 JSON 形状是对外契约面 (转写方按它对照缺口), 只增不改。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface FailureItem {
|
|
9
|
+
/**
|
|
10
|
+
* 断言类别 (枚举, 由 compare.ts 产出; 新增断言原语时须同步本行):
|
|
11
|
+
* 两面共用: timeout | spawn | fs;
|
|
12
|
+
* cli 面: exitCode | stdoutExact | stdoutContains | stderrContains
|
|
13
|
+
* | stdoutMustNotContain | stderrMustNotContain;
|
|
14
|
+
* api 面: harness | result | error | events
|
|
15
|
+
*/
|
|
16
|
+
kind: string;
|
|
17
|
+
/** 单行摘要 (面向人读) */
|
|
18
|
+
message: string;
|
|
19
|
+
/** stdoutExact 失败时的期望 / 实际全文, 供 diff 工具消费 */
|
|
20
|
+
expected?: string;
|
|
21
|
+
actual?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface CaseReport {
|
|
25
|
+
id: string;
|
|
26
|
+
specRefs: string[];
|
|
27
|
+
status: 'pass' | 'fail';
|
|
28
|
+
durationMs: number;
|
|
29
|
+
failures: FailureItem[];
|
|
30
|
+
/** 失败时的 stdout / stderr 原文 (诊断附件; 通过时为 undefined, 避免报告膨胀) */
|
|
31
|
+
diagnostics?: { stdout: string; stderr: string };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 行内容裁剪 (失败摘要保持单行可读) */
|
|
35
|
+
export const clip = (text: string, max = 120): string =>
|
|
36
|
+
text.length <= max ? text : `${text.slice(0, max)}…`;
|
|
37
|
+
|
|
38
|
+
export function printHumanReport(
|
|
39
|
+
corpusDir: string,
|
|
40
|
+
target: string,
|
|
41
|
+
reports: CaseReport[],
|
|
42
|
+
): void {
|
|
43
|
+
const lines: string[] = [];
|
|
44
|
+
lines.push(`转写一致性验收 (target: "${target}" · 语料: ${corpusDir})`);
|
|
45
|
+
lines.push('');
|
|
46
|
+
for (const report of reports) {
|
|
47
|
+
const mark = report.status === 'pass' ? '✓' : '✗';
|
|
48
|
+
lines.push(
|
|
49
|
+
` ${mark} ${report.id} [${report.specRefs.join(', ')}] ${report.durationMs}ms`,
|
|
50
|
+
);
|
|
51
|
+
for (const failure of report.failures) {
|
|
52
|
+
lines.push(` ${failure.message}`);
|
|
53
|
+
}
|
|
54
|
+
if (
|
|
55
|
+
report.diagnostics !== undefined &&
|
|
56
|
+
report.diagnostics.stderr.trim() !== ''
|
|
57
|
+
) {
|
|
58
|
+
lines.push(
|
|
59
|
+
` [stderr] ${clip(report.diagnostics.stderr.trim(), 300)}`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const failed = reports.filter((report) => report.status === 'fail').length;
|
|
64
|
+
lines.push('');
|
|
65
|
+
lines.push(
|
|
66
|
+
`合计 ${reports.length} 条: ${reports.length - failed} 通过, ${failed} 失败`,
|
|
67
|
+
);
|
|
68
|
+
process.stdout.write(`${lines.join('\n')}\n`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function printJsonReport(
|
|
72
|
+
corpusDir: string,
|
|
73
|
+
target: string,
|
|
74
|
+
reports: CaseReport[],
|
|
75
|
+
): void {
|
|
76
|
+
const failed = reports.filter((report) => report.status === 'fail').length;
|
|
77
|
+
const payload = {
|
|
78
|
+
target,
|
|
79
|
+
corpusDir,
|
|
80
|
+
total: reports.length,
|
|
81
|
+
passed: reports.length - failed,
|
|
82
|
+
failed,
|
|
83
|
+
cases: reports,
|
|
84
|
+
};
|
|
85
|
+
process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
|
|
86
|
+
}
|