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,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 构建侧入口: 给产物 dist/cli.js 写自证清单 dist/manifest.json。
|
|
3
|
+
*
|
|
4
|
+
* package.json 的 build = `bun build ... && bun scripts/write-dist-manifest.ts`: 产物与清单是
|
|
5
|
+
* 同一次构建的两半, 分开跑会留下「有产物无清单」的中间态, 而那种产物会被启动器拒收。
|
|
6
|
+
* 非 git 检出时不拒绝构建, 如实记 null (提交标识未知); 「发布必须来自干净检出」是
|
|
7
|
+
* verify-release.ts 的职责, 构建侧不越权。
|
|
8
|
+
*
|
|
9
|
+
* 用法: bun scripts/write-dist-manifest.ts
|
|
10
|
+
* 退出码: 0 清单已写入; 1 产物缺失或为空 (先跑构建)
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, statSync, writeFileSync } from 'node:fs';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
CLI_FILE,
|
|
17
|
+
type DistManifest,
|
|
18
|
+
type GitFacts,
|
|
19
|
+
MANIFEST_FILE,
|
|
20
|
+
PACKAGE_ROOT,
|
|
21
|
+
buildManifest,
|
|
22
|
+
collectGitFacts,
|
|
23
|
+
sha256File,
|
|
24
|
+
shortHash,
|
|
25
|
+
} from './release-artifact.ts';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 写清单: 算产物摘要 → 采 git 事实 → 落盘, 返回写入的清单; 产物缺失 / 不是常规文件 /
|
|
29
|
+
* 零字节时返回 null (缺失是要落退出码的拒绝路径, 不是异常, 故用返回值而非抛错)。
|
|
30
|
+
* 外部副作用:写入 dist/manifest.json。
|
|
31
|
+
*/
|
|
32
|
+
export const writeDistManifest = (
|
|
33
|
+
distDir: string,
|
|
34
|
+
git: GitFacts,
|
|
35
|
+
builtAt: string,
|
|
36
|
+
): DistManifest | null => {
|
|
37
|
+
const cliPath = join(distDir, CLI_FILE);
|
|
38
|
+
if (!existsSync(cliPath)) return null;
|
|
39
|
+
// 存在但 stat 失败 / 摘要读不出属环境异常 (权限、坏盘), 让它抛: 失败要响亮, 不与「缺失」混同
|
|
40
|
+
const info = statSync(cliPath);
|
|
41
|
+
if (!info.isFile() || info.size === 0) return null;
|
|
42
|
+
const manifest = buildManifest({
|
|
43
|
+
cliSha256: sha256File(cliPath),
|
|
44
|
+
git,
|
|
45
|
+
builtAt,
|
|
46
|
+
});
|
|
47
|
+
writeFileSync(
|
|
48
|
+
join(distDir, MANIFEST_FILE),
|
|
49
|
+
`${JSON.stringify(manifest, null, 2)}\n`,
|
|
50
|
+
);
|
|
51
|
+
return manifest;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** 人读的来源标签: 提交短哈希 + 脏净标注; 非 git 检出如实说明 (不假装有提交) */
|
|
55
|
+
const sourceLabel = (manifest: DistManifest): string => {
|
|
56
|
+
if (manifest.commit === null) return '非 git 检出 (未记录提交)';
|
|
57
|
+
const dirty = manifest.dirty === true ? ' (脏工作树)' : '';
|
|
58
|
+
return `提交 ${shortHash(manifest.commit)}${dirty}`;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** 入口: 写清单并落退出码 (0 已写入 / 1 产物缺失或为空) */
|
|
62
|
+
const main = (): number => {
|
|
63
|
+
const manifest = writeDistManifest(
|
|
64
|
+
join(PACKAGE_ROOT, 'dist'),
|
|
65
|
+
collectGitFacts(PACKAGE_ROOT),
|
|
66
|
+
new Date().toISOString(),
|
|
67
|
+
);
|
|
68
|
+
if (manifest === null) {
|
|
69
|
+
process.stderr.write(
|
|
70
|
+
`产物清单未写入: dist/${CLI_FILE} 缺失或为空, 先跑 bun build 产出产物\n`,
|
|
71
|
+
);
|
|
72
|
+
return 1;
|
|
73
|
+
}
|
|
74
|
+
process.stdout.write(
|
|
75
|
+
`产物清单已写入 dist/${MANIFEST_FILE}: ${sourceLabel(manifest)} · 摘要 ${shortHash(manifest.cliSha256)}\n`,
|
|
76
|
+
);
|
|
77
|
+
return 0;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// 被测试 import 时不得跑入口 (只有直接运行才落退出码)
|
|
81
|
+
if (import.meta.main) process.exitCode = main();
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 示例 CLI 入口 (模板骨架): 薄壳形态 —— 命令面在这里, 领域逻辑在 API 包 ({{SCOPE}}/{{NAME}})。
|
|
3
|
+
*
|
|
4
|
+
* 生成后的项目应把它替换为自己的命令实现 (打包与发布自证见 package.json 与 scripts/)。
|
|
5
|
+
* 退出码: 0 成功; 值解析与命令分发按需要扩展。
|
|
6
|
+
*/
|
|
7
|
+
import { greet } from '{{SCOPE}}/{{NAME}}';
|
|
8
|
+
|
|
9
|
+
const HELP_TEXT = [
|
|
10
|
+
'{{BIN_NAME}}: 示例命令行工具 (模板骨架)',
|
|
11
|
+
'',
|
|
12
|
+
'用法: {{BIN_NAME}} [--name <名字>] [--help]',
|
|
13
|
+
'',
|
|
14
|
+
'选项:',
|
|
15
|
+
' --name <名字> 要问候的名字 (缺省 world)',
|
|
16
|
+
' --help 打印本帮助',
|
|
17
|
+
].join('\n');
|
|
18
|
+
|
|
19
|
+
/** 取 `--name` 的取值; 缺省 world (演示最小解析, 替换时按需引入参数库) */
|
|
20
|
+
const parseName = (argv: readonly string[]): string => {
|
|
21
|
+
const index = argv.indexOf('--name');
|
|
22
|
+
if (index < 0) return 'world';
|
|
23
|
+
return argv[index + 1] ?? 'world';
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const main = (): number => {
|
|
27
|
+
const argv = process.argv.slice(2);
|
|
28
|
+
if (argv.includes('--help') || argv.includes('-h')) {
|
|
29
|
+
process.stdout.write(`${HELP_TEXT}\n`);
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
process.stdout.write(`${greet({ who: parseName(argv) })}\n`);
|
|
33
|
+
return 0;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
process.exitCode = main();
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地 CI 预演 (闸门清单的单一事实来源): 全部步骤在此一处定义, 其余消费方——手动全链、
|
|
3
|
+
* lefthook pre-push、CI 的两个 job、Release 的 verify job——一律经本入口调用, 不再各自枚举
|
|
4
|
+
* (此前清单散布五处, 每加一个闸门需五处同步; 见 docs/designs/tech-debt.md 的历史登记 TD-03)。
|
|
5
|
+
*
|
|
6
|
+
* 分组:
|
|
7
|
+
* - `--verify` 验证组 (ubuntu 可跑): build → typecheck → test → lint → format-check → 四闸门;
|
|
8
|
+
* - `--conformance` 验收组 (语料为 macOS 口径): build → conformance (bun + node 双 target);
|
|
9
|
+
* - (无旗标) 全链 = 验证组 + 验收组 (重复步骤去重);
|
|
10
|
+
* - `--only <step>` 单步执行 (步骤名见 `--help`);
|
|
11
|
+
* - `--help` 列出步骤与分组。
|
|
12
|
+
*
|
|
13
|
+
* 用法: bun scripts/ci.ts [--verify | --conformance | --only <step> | --help]
|
|
14
|
+
* 退出码: 0 全绿; 其余为首个失败步骤的退出码。
|
|
15
|
+
*
|
|
16
|
+
* test 步骤在隔离 HOME 下执行 (临时家目录经 makeTmpRoot 创建): 本机常备而 CI 缺失的
|
|
17
|
+
* 环境态依赖 (如 ~/tmp 目录、家目录配置) 不再被本机状态掩盖, 此类 CI 翻车本地预演即可
|
|
18
|
+
* 抓住 (2026-09-30 复盘: 环境态假设类失败)。
|
|
19
|
+
*/
|
|
20
|
+
import { spawnSync } from 'node:child_process';
|
|
21
|
+
import { rmSync } from 'node:fs';
|
|
22
|
+
import { join } from 'node:path';
|
|
23
|
+
|
|
24
|
+
import { makeTmpRoot } from './lib/tmp-root.ts';
|
|
25
|
+
import { resolveCliAndApi } from './lib/workspace.ts';
|
|
26
|
+
|
|
27
|
+
process.chdir(join(import.meta.dir, '..'));
|
|
28
|
+
|
|
29
|
+
/** 一个步骤: 展示名与命令参数 */
|
|
30
|
+
interface Step {
|
|
31
|
+
command: string;
|
|
32
|
+
args: string[];
|
|
33
|
+
/** 在隔离 HOME 下执行 (防「本地有而 CI 无」的环境态假设被本机状态掩盖) */
|
|
34
|
+
isolatedHome?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** CLI 入口坐标: 由 workspaces 派生 (chdir 之后相对仓库根) */
|
|
38
|
+
const { cli } = resolveCliAndApi(process.cwd());
|
|
39
|
+
const CLI_SRC = `${cli.dir}/src/cli.ts`;
|
|
40
|
+
|
|
41
|
+
/** 步骤表 (清单唯一来源): 键即 `--only` 接受的步骤名 */
|
|
42
|
+
const STEPS: Readonly<Record<string, Step>> = {
|
|
43
|
+
build: { command: 'bun', args: ['run', 'build'] },
|
|
44
|
+
typecheck: { command: 'bun', args: ['run', 'typecheck'] },
|
|
45
|
+
test: { command: 'bun', args: ['run', 'test'], isolatedHome: true },
|
|
46
|
+
lint: { command: 'bunx', args: ['oxlint'] },
|
|
47
|
+
'format-check': { command: 'bunx', args: ['prettier', '--check', '.'] },
|
|
48
|
+
'lint:refs': { command: 'bun', args: ['scripts/lint-doc-references.ts'] },
|
|
49
|
+
'lint:examples': { command: 'bun', args: ['scripts/lint-doc-examples.ts'] },
|
|
50
|
+
'lint:coverage': {
|
|
51
|
+
command: 'bun',
|
|
52
|
+
args: ['scripts/transcription/validate-coverage.ts'],
|
|
53
|
+
},
|
|
54
|
+
'lint:backups': { command: 'bun', args: ['scripts/lint-stray-backups.ts'] },
|
|
55
|
+
// 发布凭据预检 (OIDC 换证): 发布特有, 不进任何组——普通 CI job 与本机全链跑不到它,
|
|
56
|
+
// 仅 Release 的 release job (获批后) 经 `--only npm-trust` 调用; 非 OIDC 环境下手动跑时
|
|
57
|
+
// 脚本自行跳过。注意: 换证与登记逐字段核对 (含 environment 声明), 调用点必须在带
|
|
58
|
+
// environment 的 job 里, 否则会被 npm 以误导性的 404 (package not found) 拒绝
|
|
59
|
+
'npm-trust': {
|
|
60
|
+
command: 'bun',
|
|
61
|
+
args: ['scripts/npm-trust-guard.ts', 'verify-oidc'],
|
|
62
|
+
},
|
|
63
|
+
'conformance:bun': {
|
|
64
|
+
command: 'bun',
|
|
65
|
+
args: [
|
|
66
|
+
'scripts/transcription/run-conformance.ts',
|
|
67
|
+
'--target',
|
|
68
|
+
`bun ${CLI_SRC}`,
|
|
69
|
+
'--api-target',
|
|
70
|
+
'bun scripts/transcription/api-harness.ts',
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
'conformance:node': {
|
|
74
|
+
command: 'bun',
|
|
75
|
+
args: [
|
|
76
|
+
'scripts/transcription/run-conformance.ts',
|
|
77
|
+
'--target',
|
|
78
|
+
`node ${CLI_SRC}`,
|
|
79
|
+
'--api-target',
|
|
80
|
+
'node scripts/transcription/api-harness.ts',
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** 验证组 (ubuntu 可跑; CI 与 Release 的 verify job 即此组) */
|
|
86
|
+
const VERIFY_GROUP: readonly string[] = [
|
|
87
|
+
'build',
|
|
88
|
+
'typecheck',
|
|
89
|
+
'test',
|
|
90
|
+
'lint',
|
|
91
|
+
'format-check',
|
|
92
|
+
'lint:refs',
|
|
93
|
+
'lint:examples',
|
|
94
|
+
'lint:coverage',
|
|
95
|
+
'lint:backups',
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 验收组 (转写验收需 macOS 口径: 语料的 du 磁盘占用按 APFS 块分配录制)。
|
|
100
|
+
* 组内含 build: node target 直跑 CLI 源码时按 exports 解析到 API 包 dist,
|
|
101
|
+
* 而 node 不读 tsconfig paths (bun 独有), 无产物即全数 ERR_MODULE_NOT_FOUND (2026-09-29 CI 实翻)。
|
|
102
|
+
*/
|
|
103
|
+
const CONFORMANCE_GROUP: readonly string[] = [
|
|
104
|
+
'build',
|
|
105
|
+
'conformance:bun',
|
|
106
|
+
'conformance:node',
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
/** 跑一步 (stdio 透传), 失败即携其退出码终止 */
|
|
110
|
+
function runStep(name: string): void {
|
|
111
|
+
const step = STEPS[name]!;
|
|
112
|
+
process.stdout.write(`\n=== ${name} ===\n`);
|
|
113
|
+
const home = step.isolatedHome === true ? makeTmpRoot('ci-home-') : undefined;
|
|
114
|
+
if (home !== undefined) process.stdout.write(`隔离 HOME: ${home}\n`);
|
|
115
|
+
const env = home === undefined ? process.env : { ...process.env, HOME: home };
|
|
116
|
+
const result = spawnSync(step.command, step.args, { stdio: 'inherit', env });
|
|
117
|
+
if (home !== undefined) rmSync(home, { recursive: true, force: true });
|
|
118
|
+
if (result.status !== 0) process.exit(result.status ?? 1);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** 帮助文本 */
|
|
122
|
+
const HELP_TEXT = [
|
|
123
|
+
'本地 CI 预演 (闸门清单单一事实来源)',
|
|
124
|
+
'',
|
|
125
|
+
'用法: bun scripts/ci.ts [选项]',
|
|
126
|
+
' --verify 验证组: ' + VERIFY_GROUP.join(' → '),
|
|
127
|
+
' --conformance 验收组: ' + CONFORMANCE_GROUP.join(' → '),
|
|
128
|
+
' (无旗标) 全链 = 验证组 + 验收组 (去重)',
|
|
129
|
+
' --only <step> 单步执行',
|
|
130
|
+
' --help 本帮助',
|
|
131
|
+
'',
|
|
132
|
+
'步骤名: ' + Object.keys(STEPS).join(' / '),
|
|
133
|
+
].join('\n');
|
|
134
|
+
|
|
135
|
+
/** 解析入口旗标为待跑步骤序列 */
|
|
136
|
+
function resolveSteps(argv: string[]): string[] | null {
|
|
137
|
+
const onlyIndex = argv.indexOf('--only');
|
|
138
|
+
if (onlyIndex >= 0) {
|
|
139
|
+
const name = argv[onlyIndex + 1];
|
|
140
|
+
if (name === undefined || STEPS[name] === undefined) {
|
|
141
|
+
process.stderr.write(
|
|
142
|
+
`--only 缺步骤名或步骤不存在: ${String(name)}\n\n${HELP_TEXT}\n`,
|
|
143
|
+
);
|
|
144
|
+
process.exit(2);
|
|
145
|
+
}
|
|
146
|
+
return [name];
|
|
147
|
+
}
|
|
148
|
+
if (argv.includes('--verify')) return [...VERIFY_GROUP];
|
|
149
|
+
if (argv.includes('--conformance')) return [...CONFORMANCE_GROUP];
|
|
150
|
+
// 全链: 验证组 + 验收组中未出现过的步骤 (build 去重)
|
|
151
|
+
return [
|
|
152
|
+
...VERIFY_GROUP,
|
|
153
|
+
...CONFORMANCE_GROUP.filter((name) => !VERIFY_GROUP.includes(name)),
|
|
154
|
+
];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (process.argv.includes('--help')) {
|
|
158
|
+
process.stdout.write(`${HELP_TEXT}\n`);
|
|
159
|
+
} else {
|
|
160
|
+
const steps = resolveSteps(process.argv.slice(2))!;
|
|
161
|
+
for (const name of steps) runStep(name);
|
|
162
|
+
process.stdout.write('\nCI 预演全绿 ✓\n');
|
|
163
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 装 git 钩子 (package.json 的 prepare 入口).
|
|
4
|
+
*
|
|
5
|
+
* 守卫: 只在「本包自身的 git 仓库」执行 `lefthook install`.
|
|
6
|
+
*
|
|
7
|
+
* 为什么需要守卫: prepare 在本包被作为依赖安装时也会在宿主环境执行 (git 依赖的克隆态、
|
|
8
|
+
* vendor / 本地路径依赖形态), 而 `lefthook install` 以「当前目录所在的 git 仓库」为目标;
|
|
9
|
+
* 一旦包目录落在宿主仓库树内 (vendor 进宿主仓库、或经 install-links 落进 node_modules),
|
|
10
|
+
* 安装动作就会把钩子写进宿主仓库的 .git/hooks (消费者仓库被写 VCS 配置, 与消费者本意无关).
|
|
11
|
+
* 判定用「cwd 所在 git 根 === 包根」: 是本包仓库才装, 否则整支跳过.
|
|
12
|
+
*
|
|
13
|
+
* 各形态的判定结果:
|
|
14
|
+
* - 仓库检出态 (本包开发): 两者相同 → 装;
|
|
15
|
+
* - git 依赖的临时克隆态: 两者相同 → 装 (落在一次性克隆目录, 无副作用);
|
|
16
|
+
* - vendor / 解包进宿主仓库的子目录: git 根是宿主 → 跳过;
|
|
17
|
+
* - 经 npm install-links 落进宿主 node_modules: git 根是宿主 → 跳过;
|
|
18
|
+
* - 不在任何 git 仓库内 (tarball 解包目录等): git 根不可得 → 跳过.
|
|
19
|
+
*
|
|
20
|
+
* 安装期取用 lefthook 的顺序: PATH 上的 lefthook → lefthook.yml 的 `lefthook:` 配置值
|
|
21
|
+
* (经 bunx 取 pin 版; 与执行期钩子模板同源, 单一事实来源). lefthook 不列为依赖, 以免宿主
|
|
22
|
+
* 安装本包时连带装 lefthook 并触发它的 postinstall 往宿主仓库写钩子.
|
|
23
|
+
* 执行期由 lefthook.yml 的 `lefthook:` + `assert_lefthook_installed` 保证「兜底可取且失败响亮」
|
|
24
|
+
* (官方模板探测链不含 bunx, 且默认静默放行 —— 见 ADR 0005 决策第 1 条).
|
|
25
|
+
*
|
|
26
|
+
* 任何失败都不阻断安装: 装钩子是便利动作, 不是安装的必要条件 (与原 `|| true` 形态一致).
|
|
27
|
+
*/
|
|
28
|
+
import { spawnSync } from 'node:child_process';
|
|
29
|
+
import { readFileSync, realpathSync } from 'node:fs';
|
|
30
|
+
import { dirname, join, resolve } from 'node:path';
|
|
31
|
+
import { fileURLToPath } from 'node:url';
|
|
32
|
+
|
|
33
|
+
/** 包根: 本脚本位于 <pkg>/scripts/ 下, 上溯一级 */
|
|
34
|
+
const PKG_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
35
|
+
|
|
36
|
+
const IS_WINDOWS = process.platform === 'win32';
|
|
37
|
+
|
|
38
|
+
/** 统一 spawn 封装: Windows 上 .cmd/.bat 需经 shell 解析 */
|
|
39
|
+
const run = (cmd, args, options = {}) =>
|
|
40
|
+
spawnSync(cmd, args, { shell: IS_WINDOWS, ...options });
|
|
41
|
+
|
|
42
|
+
/** 命令是否可用 (以 `--version` 探测, 不存在时 status 为 null) */
|
|
43
|
+
const hasCommand = (cmd) =>
|
|
44
|
+
run(cmd, ['--version'], { stdio: 'ignore' }).status === 0;
|
|
45
|
+
|
|
46
|
+
/** 取真实路径用于比较 (路径不可解析时回退原值) */
|
|
47
|
+
const realpathOr = (value) => {
|
|
48
|
+
try {
|
|
49
|
+
return realpathSync(value);
|
|
50
|
+
} catch {
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** 包根所在 git 仓库的根 (不在仓库内或 git 不可用时为 null) */
|
|
56
|
+
const gitToplevel = () => {
|
|
57
|
+
const out = run('git', ['rev-parse', '--show-toplevel'], {
|
|
58
|
+
cwd: PKG_ROOT,
|
|
59
|
+
encoding: 'utf8',
|
|
60
|
+
});
|
|
61
|
+
return out.status === 0 ? out.stdout.trim() : null;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 从 lefthook.yml 读执行期的 lefthook 命令 (按空白切分为 argv; 取不到或为空时 null).
|
|
66
|
+
* 单一事实来源: 钩子模板里的兜底命令与安装期取用同出一处, 避免两处各写一个 pin 版本.
|
|
67
|
+
*/
|
|
68
|
+
const configuredLefthook = () => {
|
|
69
|
+
try {
|
|
70
|
+
const text = readFileSync(join(PKG_ROOT, 'lefthook.yml'), 'utf8');
|
|
71
|
+
const matched = /^lefthook:[ \t]*(\S.*)$/m.exec(text);
|
|
72
|
+
return matched ? matched[1].trim().split(/\s+/) : null;
|
|
73
|
+
} catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const main = () => {
|
|
79
|
+
// CI 下不装: 既不本地跑门禁, 也无交互终端 (与原 prepare 的 CI 短路一致)
|
|
80
|
+
if (process.env.CI) return;
|
|
81
|
+
|
|
82
|
+
const toplevel = gitToplevel();
|
|
83
|
+
if (toplevel === null || realpathOr(toplevel) !== realpathOr(PKG_ROOT)) {
|
|
84
|
+
// 被作为依赖安装且落点不在本包自己的仓库 → 目标是宿主仓库, 不碰
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 取用顺序: PATH 上的 lefthook (最快, 开发机常见) → lefthook.yml 的配置命令 (经 bunx 取 pin 版)
|
|
89
|
+
let argv = null;
|
|
90
|
+
if (hasCommand('lefthook')) {
|
|
91
|
+
argv = ['lefthook'];
|
|
92
|
+
} else {
|
|
93
|
+
const configured = configuredLefthook();
|
|
94
|
+
if (configured !== null && hasCommand(configured[0])) argv = configured;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (argv === null) {
|
|
98
|
+
console.error(
|
|
99
|
+
'[prepare] 未找到 lefthook, 已跳过钩子安装; 稍后可手动补装 (如 brew install lefthook) 再跑 lefthook install',
|
|
100
|
+
);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const result = run(argv[0], [...argv.slice(1), 'install'], {
|
|
105
|
+
cwd: PKG_ROOT,
|
|
106
|
+
stdio: 'inherit',
|
|
107
|
+
});
|
|
108
|
+
if (result.status !== 0) {
|
|
109
|
+
console.error(
|
|
110
|
+
'[prepare] lefthook 装钩子未成功, 已跳过; 稍后可手动补跑 lefthook install',
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
main();
|
|
117
|
+
} catch {
|
|
118
|
+
// 装钩子失败不阻断依赖安装 (与原 `|| true` 形态一致); 具体原因交给手动补跑时暴露
|
|
119
|
+
console.error(
|
|
120
|
+
'[prepare] 装钩子跳过 (执行期异常); 稍后可手动补跑 lefthook install',
|
|
121
|
+
);
|
|
122
|
+
}
|