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,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* install-git-hooks.mjs 的守卫契约测试 (C9 闭环, 见 ADR 0005 决策第 1 条):
|
|
3
|
+
*
|
|
4
|
+
* 一, 安装侧守卫: prepare 装钩子只允许发生在「本包自身的 git 仓库」, 其余落点
|
|
5
|
+
* (vendor 进宿主仓库 / install-links 落进 node_modules / 非仓库目录) 一律不调 lefthook,
|
|
6
|
+
* 以免 VCS 配置被写到宿主仓库. 手法: 临时目录树 + 按真实相对位置铺脚本 + PATH 前置假 lefthook.
|
|
7
|
+
*
|
|
8
|
+
* 二, 执行侧链: 生成的钩子必须带 bunx 兜底 (官方模板探测链不含 bunx, 且默认静默放行导致
|
|
9
|
+
* 门禁悄悄失效), 且找不到 lefthook 时要响亮失败. 手法: 需要真 lefthook 生成钩子的用例
|
|
10
|
+
* 以 hasLefthook 条件执行; 净化 PATH + 注入假 bunx, 断言钩子确实落到兜底分支并执行.
|
|
11
|
+
*/
|
|
12
|
+
import { afterEach, describe, expect, test } from 'bun:test';
|
|
13
|
+
|
|
14
|
+
import { spawnSync } from 'node:child_process';
|
|
15
|
+
import {
|
|
16
|
+
chmodSync,
|
|
17
|
+
copyFileSync,
|
|
18
|
+
existsSync,
|
|
19
|
+
mkdirSync,
|
|
20
|
+
mkdtempSync,
|
|
21
|
+
readFileSync,
|
|
22
|
+
readdirSync,
|
|
23
|
+
realpathSync,
|
|
24
|
+
rmSync,
|
|
25
|
+
symlinkSync,
|
|
26
|
+
writeFileSync,
|
|
27
|
+
} from 'node:fs';
|
|
28
|
+
import { tmpdir } from 'node:os';
|
|
29
|
+
import { join, resolve } from 'node:path';
|
|
30
|
+
|
|
31
|
+
import { cleanGitEnv } from './lib/git-env.ts';
|
|
32
|
+
|
|
33
|
+
const HERE = import.meta.dir;
|
|
34
|
+
const SCRIPT = resolve(HERE, 'install-git-hooks.mjs');
|
|
35
|
+
|
|
36
|
+
/** 文件以 `#!` 开头即脚本包装 (bunx / npm 装的 node shim), 复制到别处会丢相对依赖 */
|
|
37
|
+
const isScriptWrapper = (file: string): boolean => {
|
|
38
|
+
try {
|
|
39
|
+
return readFileSync(file).subarray(0, 2).toString() === '#!';
|
|
40
|
+
} catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 取一份「可搬运」的 lefthook 可执行文件: 复制到临时路径后仍能独立运行的那种。
|
|
47
|
+
* 搬运是「装完即删, 让模板写死的 install 期绝对路径失效」这一步的前提。
|
|
48
|
+
* 不能直接搬 `command -v lefthook` 的命中项: hook 环境里 bunx 把自己的
|
|
49
|
+
* node_modules/.bin 前置进 PATH, 命中的是官方包的 node 包装脚本 (靠相邻的 ../get-exe
|
|
50
|
+
* 找平台二进制), 单独复制出去会以 Cannot find module 失败 (钩子压根不落地); 且它转手
|
|
51
|
+
* spawn 的是平台二进制, 模板写死的也是那一份路径, 删包装脚本副本失效不了这一跳。
|
|
52
|
+
* 故命中包装脚本时改取其同包 node_modules 下的平台二进制。
|
|
53
|
+
* 两处都取不到时退回原命中项: 该环境下复制出去仍会失败, 用例响亮失败而非静默跳过。
|
|
54
|
+
*/
|
|
55
|
+
const resolveRelocatableLefthook = (): string => {
|
|
56
|
+
const found = spawnSync('sh', ['-c', 'command -v lefthook'], {
|
|
57
|
+
encoding: 'utf8',
|
|
58
|
+
env: cleanGitEnv(),
|
|
59
|
+
}).stdout.trim();
|
|
60
|
+
if (found === '' || !isScriptWrapper(found)) return found;
|
|
61
|
+
|
|
62
|
+
// <nm>/lefthook/bin/index.js -> <nm>: 平台二进制与 lefthook 包平级 (lefthook-<os>-<arch>)
|
|
63
|
+
const nodeModules = resolve(realpathSync(found), '..', '..', '..');
|
|
64
|
+
if (existsSync(nodeModules)) {
|
|
65
|
+
for (const entry of readdirSync(nodeModules)) {
|
|
66
|
+
if (!entry.startsWith('lefthook-')) continue;
|
|
67
|
+
const platform = join(nodeModules, entry, 'bin', 'lefthook');
|
|
68
|
+
if (existsSync(platform) && !isScriptWrapper(platform)) return platform;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return found;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const hasGit =
|
|
75
|
+
spawnSync('git', ['--version'], { stdio: 'ignore', env: cleanGitEnv() })
|
|
76
|
+
.status === 0;
|
|
77
|
+
const hasLefthook =
|
|
78
|
+
spawnSync('lefthook', ['--version'], { stdio: 'ignore', env: cleanGitEnv() })
|
|
79
|
+
.status === 0;
|
|
80
|
+
const isWindows = process.platform === 'win32';
|
|
81
|
+
|
|
82
|
+
/** 本仓 lefthook.yml 的执行链兜底命令 (单一事实来源: 生成钩子与安装取用同读这一处) */
|
|
83
|
+
const readConfiguredLefthook = (): string => {
|
|
84
|
+
const text = readFileSync(resolve(HERE, '..', 'lefthook.yml'), 'utf8');
|
|
85
|
+
const value = /^lefthook:[ \t]*(\S.*)$/m.exec(text)?.[1];
|
|
86
|
+
if (value === undefined) throw new Error('lefthook.yml 缺少 lefthook: 配置');
|
|
87
|
+
return value.trim();
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/** 现场清单 (每个用例自建自清) */
|
|
91
|
+
const sandboxes: string[] = [];
|
|
92
|
+
|
|
93
|
+
/** 建一个用例沙箱根 (mkdtemp 保证互不干扰) */
|
|
94
|
+
const makeSandbox = (): string => {
|
|
95
|
+
const root = mkdtempSync(join(tmpdir(), 'sweep-hooks-'));
|
|
96
|
+
sandboxes.push(root);
|
|
97
|
+
return root;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/** 在目录内执行 git 命令 (夹具自建仓库用) */
|
|
101
|
+
const git = (cwd: string, args: string[]) =>
|
|
102
|
+
spawnSync('git', args, {
|
|
103
|
+
cwd,
|
|
104
|
+
encoding: 'utf8',
|
|
105
|
+
env: { ...cleanGitEnv(), GIT_CONFIG_GLOBAL: '/dev/null' },
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 把被测脚本按真实相对位置 (<pkgRoot>/scripts/install-git-hooks.mjs) 铺进夹具,
|
|
110
|
+
* 返回脚本的绝对路径 (脚本据此上溯一级认包根).
|
|
111
|
+
*/
|
|
112
|
+
const placeScript = (pkgRoot: string): string => {
|
|
113
|
+
const dir = join(pkgRoot, 'scripts');
|
|
114
|
+
mkdirSync(dir, { recursive: true });
|
|
115
|
+
const target = join(dir, 'install-git-hooks.mjs');
|
|
116
|
+
writeFileSync(target, readFileSync(SCRIPT));
|
|
117
|
+
return target;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 夹具环境: PATH 前置一个假 lefthook (只把参数记进日志), 日志路径经 env 传给假二进制.
|
|
122
|
+
* 返回 { env, logPath }.
|
|
123
|
+
*/
|
|
124
|
+
const makeFakeLefthookEnv = (
|
|
125
|
+
root: string,
|
|
126
|
+
extraEnv: Record<string, string> = {},
|
|
127
|
+
) => {
|
|
128
|
+
const binDir = join(root, 'fake-bin');
|
|
129
|
+
mkdirSync(binDir, { recursive: true });
|
|
130
|
+
const logPath = join(root, 'lefthook-calls.log');
|
|
131
|
+
const fake = join(binDir, 'lefthook');
|
|
132
|
+
writeFileSync(fake, `#!/bin/sh\necho "$@" >> "${logPath}"\nexit 0\n`);
|
|
133
|
+
chmodSync(fake, 0o755);
|
|
134
|
+
const env: Record<string, string> = {
|
|
135
|
+
...cleanGitEnv(),
|
|
136
|
+
PATH: `${binDir}:${process.env.PATH ?? ''}`,
|
|
137
|
+
};
|
|
138
|
+
// 基线环境不携带 CI (用例要的 CI 场景经 extraEnv 显式注入)
|
|
139
|
+
delete env.CI;
|
|
140
|
+
Object.assign(env, extraEnv);
|
|
141
|
+
return { env, logPath };
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/** 运行被测脚本 (cwd 取包根, 与包管理器跑 prepare 的现场一致) */
|
|
145
|
+
const runScript = (script: string, cwd: string, env: Record<string, string>) =>
|
|
146
|
+
spawnSync(process.execPath, [script], { cwd, env, encoding: 'utf8' });
|
|
147
|
+
|
|
148
|
+
/** 读假 lefthook 的调用日志 (没被调用则为空串) */
|
|
149
|
+
const readCalls = (logPath: string): string =>
|
|
150
|
+
existsSync(logPath) ? readFileSync(logPath, 'utf8') : '';
|
|
151
|
+
|
|
152
|
+
afterEach(() => {
|
|
153
|
+
for (const dir of sandboxes.splice(0))
|
|
154
|
+
rmSync(dir, { recursive: true, force: true });
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe.skipIf(!hasGit || isWindows)('install-git-hooks 守卫', () => {
|
|
158
|
+
test('包根即 git 根 (本包检出态): 调 lefthook install', () => {
|
|
159
|
+
const root = makeSandbox();
|
|
160
|
+
const pkgRoot = join(root, 'pkg');
|
|
161
|
+
mkdirSync(pkgRoot, { recursive: true });
|
|
162
|
+
git(pkgRoot, ['init', '--quiet']);
|
|
163
|
+
|
|
164
|
+
const script = placeScript(pkgRoot);
|
|
165
|
+
const { env, logPath } = makeFakeLefthookEnv(root);
|
|
166
|
+
const result = runScript(script, pkgRoot, env);
|
|
167
|
+
|
|
168
|
+
expect(result.status).toBe(0);
|
|
169
|
+
expect(readCalls(logPath)).toContain('install');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('包根是 git 根的临时克隆态 (git 依赖安装现场): 调 lefthook install', () => {
|
|
173
|
+
const root = makeSandbox();
|
|
174
|
+
const cloneDir = join(root, 'git-clone-abc');
|
|
175
|
+
mkdirSync(cloneDir, { recursive: true });
|
|
176
|
+
git(cloneDir, ['init', '--quiet']);
|
|
177
|
+
|
|
178
|
+
const script = placeScript(cloneDir);
|
|
179
|
+
const { env, logPath } = makeFakeLefthookEnv(root);
|
|
180
|
+
const result = runScript(script, cloneDir, env);
|
|
181
|
+
|
|
182
|
+
expect(result.status).toBe(0);
|
|
183
|
+
expect(readCalls(logPath)).toContain('install');
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test('包目录 vendor 进宿主仓库 (无独立 .git): 不调 lefthook, 宿主仓库不被碰', () => {
|
|
187
|
+
const root = makeSandbox();
|
|
188
|
+
const hostRoot = join(root, 'host');
|
|
189
|
+
mkdirSync(hostRoot, { recursive: true });
|
|
190
|
+
git(hostRoot, ['init', '--quiet']);
|
|
191
|
+
|
|
192
|
+
// 宿主仓库树内的包副本, 且不带自己的 .git (tarball 解压 / 导出式 vendor)
|
|
193
|
+
const pkgRoot = join(hostRoot, 'vendor', '{{NAME}}');
|
|
194
|
+
mkdirSync(pkgRoot, { recursive: true });
|
|
195
|
+
const script = placeScript(pkgRoot);
|
|
196
|
+
|
|
197
|
+
const { env, logPath } = makeFakeLefthookEnv(root);
|
|
198
|
+
const result = runScript(script, pkgRoot, env);
|
|
199
|
+
|
|
200
|
+
expect(result.status).toBe(0);
|
|
201
|
+
expect(readCalls(logPath)).toBe('');
|
|
202
|
+
expect(existsSync(join(hostRoot, '.git', 'hooks', 'pre-commit'))).toBe(
|
|
203
|
+
false,
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test('包目录落进宿主 node_modules (install-links 形态): 不调 lefthook', () => {
|
|
208
|
+
const root = makeSandbox();
|
|
209
|
+
const hostRoot = join(root, 'host');
|
|
210
|
+
mkdirSync(hostRoot, { recursive: true });
|
|
211
|
+
git(hostRoot, ['init', '--quiet']);
|
|
212
|
+
|
|
213
|
+
const pkgRoot = join(
|
|
214
|
+
hostRoot,
|
|
215
|
+
'node_modules',
|
|
216
|
+
'@{{OWNER}}',
|
|
217
|
+
'{{NAME}}',
|
|
218
|
+
);
|
|
219
|
+
mkdirSync(pkgRoot, { recursive: true });
|
|
220
|
+
const script = placeScript(pkgRoot);
|
|
221
|
+
|
|
222
|
+
const { env, logPath } = makeFakeLefthookEnv(root);
|
|
223
|
+
const result = runScript(script, pkgRoot, env);
|
|
224
|
+
|
|
225
|
+
expect(result.status).toBe(0);
|
|
226
|
+
expect(readCalls(logPath)).toBe('');
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test('不在任何 git 仓库内: 不调 lefthook', () => {
|
|
230
|
+
const root = makeSandbox();
|
|
231
|
+
const pkgRoot = join(root, 'plain');
|
|
232
|
+
mkdirSync(pkgRoot, { recursive: true });
|
|
233
|
+
|
|
234
|
+
const script = placeScript(pkgRoot);
|
|
235
|
+
const { env, logPath } = makeFakeLefthookEnv(root);
|
|
236
|
+
const result = runScript(script, pkgRoot, env);
|
|
237
|
+
|
|
238
|
+
expect(result.status).toBe(0);
|
|
239
|
+
expect(readCalls(logPath)).toBe('');
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test('CI 环境变量在场: 一律不调 lefthook (即使包根即 git 根)', () => {
|
|
243
|
+
const root = makeSandbox();
|
|
244
|
+
const pkgRoot = join(root, 'pkg');
|
|
245
|
+
mkdirSync(pkgRoot, { recursive: true });
|
|
246
|
+
git(pkgRoot, ['init', '--quiet']);
|
|
247
|
+
|
|
248
|
+
const script = placeScript(pkgRoot);
|
|
249
|
+
const { env, logPath } = makeFakeLefthookEnv(root, { CI: 'true' });
|
|
250
|
+
const result = runScript(script, pkgRoot, env);
|
|
251
|
+
|
|
252
|
+
expect(result.status).toBe(0);
|
|
253
|
+
expect(readCalls(logPath)).toBe('');
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test('lefthook 不可用 (PATH 只有 git, 无 lefthook/bunx): 跳过但不阻断安装', () => {
|
|
257
|
+
const root = makeSandbox();
|
|
258
|
+
const pkgRoot = join(root, 'pkg');
|
|
259
|
+
mkdirSync(pkgRoot, { recursive: true });
|
|
260
|
+
git(pkgRoot, ['init', '--quiet']);
|
|
261
|
+
|
|
262
|
+
const script = placeScript(pkgRoot);
|
|
263
|
+
|
|
264
|
+
// PATH 只保留 git: 关掉全局 lefthook / bunx 的干扰, 同时保证脚本内的 git 探测可用
|
|
265
|
+
const gitPath = spawnSync('which', ['git'], {
|
|
266
|
+
encoding: 'utf8',
|
|
267
|
+
env: cleanGitEnv(),
|
|
268
|
+
}).stdout.trim();
|
|
269
|
+
const binDir = join(root, 'only-git-bin');
|
|
270
|
+
mkdirSync(binDir, { recursive: true });
|
|
271
|
+
symlinkSync(gitPath, join(binDir, 'git'));
|
|
272
|
+
|
|
273
|
+
const env: Record<string, string> = { ...cleanGitEnv(), PATH: binDir };
|
|
274
|
+
delete env.CI;
|
|
275
|
+
|
|
276
|
+
const result = runScript(script, pkgRoot, env);
|
|
277
|
+
|
|
278
|
+
expect(result.status).toBe(0);
|
|
279
|
+
expect(result.stderr).toContain('未找到 lefthook');
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// ---------------------------------------------------------------------------
|
|
284
|
+
// 二, 执行侧链: 生成的钩子必须带 bunx 兜底, 找不到 lefthook 时响亮失败
|
|
285
|
+
// ---------------------------------------------------------------------------
|
|
286
|
+
|
|
287
|
+
describe('lefthook.yml 执行链配置 (C9 收口)', () => {
|
|
288
|
+
test('lefthook.yml 配好执行链兜底与响亮失败键', () => {
|
|
289
|
+
const text = readFileSync(resolve(HERE, '..', 'lefthook.yml'), 'utf8');
|
|
290
|
+
expect(readConfiguredLefthook()).toContain('bunx lefthook@');
|
|
291
|
+
expect(text).toMatch(/^assert_lefthook_installed:\s*true$/m);
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
/** 建一个已装钩子的临时仓库 (配置头部由调用方给, 便于对照有无 lefthook: 配置) */
|
|
296
|
+
const makeHookRepo = (root: string, configHead: string) => {
|
|
297
|
+
const dir = join(root, 'repo');
|
|
298
|
+
mkdirSync(dir, { recursive: true });
|
|
299
|
+
git(dir, ['init', '--quiet']);
|
|
300
|
+
writeFileSync(
|
|
301
|
+
join(dir, 'lefthook.yml'),
|
|
302
|
+
`${configHead}\npre-commit:\n commands:\n marker:\n run: echo hook-marker-ran\n`,
|
|
303
|
+
);
|
|
304
|
+
const install = spawnSync('lefthook', ['install'], {
|
|
305
|
+
cwd: dir,
|
|
306
|
+
encoding: 'utf8',
|
|
307
|
+
env: { ...cleanGitEnv(), GIT_CONFIG_GLOBAL: '/dev/null' },
|
|
308
|
+
});
|
|
309
|
+
return { dir, install };
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
/** 净化 PATH (不含全局 lefthook; 只留系统基础目录, 可选前置注入 bin) */
|
|
313
|
+
const cleanPathEnv = (injectedBin: string | null): Record<string, string> => {
|
|
314
|
+
const env: Record<string, string> = {
|
|
315
|
+
...cleanGitEnv(),
|
|
316
|
+
PATH:
|
|
317
|
+
injectedBin === null ? '/usr/bin:/bin' : `${injectedBin}:/usr/bin:/bin`,
|
|
318
|
+
};
|
|
319
|
+
delete env.CI;
|
|
320
|
+
delete env.LEFTHOOK_BIN;
|
|
321
|
+
return env;
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
/** 直接执行仓库里的 pre-commit 钩子 */
|
|
325
|
+
const runHook = (dir: string, env: Record<string, string>) =>
|
|
326
|
+
spawnSync('sh', ['.git/hooks/pre-commit'], {
|
|
327
|
+
cwd: dir,
|
|
328
|
+
env,
|
|
329
|
+
encoding: 'utf8',
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
describe.skipIf(!hasGit || !hasLefthook || isWindows)(
|
|
333
|
+
'钩子执行链 (模板实测)',
|
|
334
|
+
() => {
|
|
335
|
+
test('生成的钩子: 探测顺序为 LEFTHOOK_BIN → bunx 兜底 → PATH, 且失败退非零', () => {
|
|
336
|
+
const root = makeSandbox();
|
|
337
|
+
const configured = readConfiguredLefthook();
|
|
338
|
+
const { dir } = makeHookRepo(
|
|
339
|
+
root,
|
|
340
|
+
`lefthook: ${configured}\nassert_lefthook_installed: true`,
|
|
341
|
+
);
|
|
342
|
+
|
|
343
|
+
const hook = readFileSync(join(dir, '.git/hooks/pre-commit'), 'utf8');
|
|
344
|
+
const binAt = hook.indexOf('"$LEFTHOOK_BIN" "$@"');
|
|
345
|
+
const fallbackAt = hook.indexOf(`${configured} "$@"`);
|
|
346
|
+
const pathAt = hook.indexOf('elif lefthook -h');
|
|
347
|
+
|
|
348
|
+
expect(hook).toContain(`elif test -n "${configured}"`);
|
|
349
|
+
expect(binAt).toBeGreaterThan(-1);
|
|
350
|
+
expect(fallbackAt).toBeGreaterThan(binAt);
|
|
351
|
+
expect(pathAt).toBeGreaterThan(fallbackAt);
|
|
352
|
+
expect(hook).toContain("Can't find lefthook in PATH");
|
|
353
|
+
expect(hook).toContain('exit 1');
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test('净化 PATH (无 lefthook) 触发钩子: 落 bunx 兜底分支并真正执行', () => {
|
|
357
|
+
const root = makeSandbox();
|
|
358
|
+
const configured = readConfiguredLefthook();
|
|
359
|
+
const { dir } = makeHookRepo(
|
|
360
|
+
root,
|
|
361
|
+
`lefthook: ${configured}\nassert_lefthook_installed: true`,
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
// 注入假 bunx (记录参数并成功退出), 代表兜底通道被真正走到
|
|
365
|
+
const binDir = join(root, 'fake-bunx-bin');
|
|
366
|
+
mkdirSync(binDir, { recursive: true });
|
|
367
|
+
const logPath = join(root, 'bunx-calls.log');
|
|
368
|
+
const fake = join(binDir, 'bunx');
|
|
369
|
+
writeFileSync(fake, `#!/bin/sh\necho "$@" >> "${logPath}"\nexit 0\n`);
|
|
370
|
+
chmodSync(fake, 0o755);
|
|
371
|
+
|
|
372
|
+
const env = cleanPathEnv(binDir);
|
|
373
|
+
expect(
|
|
374
|
+
spawnSync('sh', ['-c', 'command -v lefthook'], {
|
|
375
|
+
env,
|
|
376
|
+
encoding: 'utf8',
|
|
377
|
+
}).stdout,
|
|
378
|
+
).toBe('');
|
|
379
|
+
|
|
380
|
+
const result = runHook(dir, env);
|
|
381
|
+
|
|
382
|
+
const calls = readCalls(logPath);
|
|
383
|
+
expect(calls).toContain('run pre-commit');
|
|
384
|
+
expect(calls).toContain(configured.split(/\s+/)[1] ?? '');
|
|
385
|
+
expect(result.status).toBe(0);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
test('无 lefthook 且未配兜底命令: 钩子退非零 (响亮失败, 不静默放行)', () => {
|
|
389
|
+
const root = makeSandbox();
|
|
390
|
+
// 用真 lefthook 的临时副本装钩子 (探测链会写入副本的绝对路径), 装完即删副本:
|
|
391
|
+
// 这样 PATH 净化之外, 连「install 时的绝对路径」这一跳也失效, 才真正复现「本机无任何 lefthook」
|
|
392
|
+
const copyPath = join(root, 'lh-copy-lefthook');
|
|
393
|
+
copyFileSync(resolveRelocatableLefthook(), copyPath);
|
|
394
|
+
chmodSync(copyPath, 0o755);
|
|
395
|
+
|
|
396
|
+
const dir = join(root, 'repo');
|
|
397
|
+
mkdirSync(dir, { recursive: true });
|
|
398
|
+
git(dir, ['init', '--quiet']);
|
|
399
|
+
writeFileSync(
|
|
400
|
+
join(dir, 'lefthook.yml'),
|
|
401
|
+
'assert_lefthook_installed: true\npre-commit:\n commands:\n marker:\n run: echo hook-marker-ran\n',
|
|
402
|
+
);
|
|
403
|
+
spawnSync(copyPath, ['install'], {
|
|
404
|
+
cwd: dir,
|
|
405
|
+
encoding: 'utf8',
|
|
406
|
+
env: { ...cleanGitEnv(), GIT_CONFIG_GLOBAL: '/dev/null' },
|
|
407
|
+
});
|
|
408
|
+
rmSync(copyPath, { force: true });
|
|
409
|
+
|
|
410
|
+
const result = runHook(dir, cleanPathEnv(null));
|
|
411
|
+
|
|
412
|
+
expect(result.status).not.toBe(0);
|
|
413
|
+
expect(`${result.stdout}${result.stderr}`).toContain(
|
|
414
|
+
"Can't find lefthook in PATH",
|
|
415
|
+
);
|
|
416
|
+
});
|
|
417
|
+
},
|
|
418
|
+
);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 进程环境清洗: 剔掉 git 注入的「当前仓库坐标」。
|
|
3
|
+
*
|
|
4
|
+
* 背景: git 运行钩子时会把 GIT_DIR / GIT_WORK_TREE / GIT_INDEX_FILE 等坐标导出进进程
|
|
5
|
+
* 环境, 经 lefthook 一路透传给钩子里跑的命令 (如 bun test 与发布闸门)。任何以 cwd 定位
|
|
6
|
+
* 目标仓库的 git 调用, 若沿用未清洗的环境, 目标仓库都会被这份坐标顶掉: 临时仓库的
|
|
7
|
+
* init / add / commit 落进宿主仓库, 钩子装不进沙箱 .git/hooks (用例静默失败), 重则覆写
|
|
8
|
+
* 宿主自己的钩子。清洗后, git 只按调用点给的 cwd 认仓库。
|
|
9
|
+
*
|
|
10
|
+
* 构建侧 (release-artifact.ts) 与测试夹具共用本模块, 规则只此一处。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** 剔掉全部 GIT_ 前缀键, 保留其余环境变量 */
|
|
14
|
+
export const cleanGitEnv = (): Record<string, string> => {
|
|
15
|
+
const env: Record<string, string> = {};
|
|
16
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
17
|
+
if (value === undefined || key.startsWith('GIT_')) continue;
|
|
18
|
+
env[key] = value;
|
|
19
|
+
}
|
|
20
|
+
return env;
|
|
21
|
+
};
|