gatelight 0.18.0 → 0.19.1
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/.antigravity-plugin/plugin.json +2 -2
- package/.claude-plugin/plugin.json +2 -2
- package/.codex-plugin/mcp.json +10 -0
- package/.codex-plugin/mcp.sh +7 -0
- package/.codex-plugin/plugin.json +4 -3
- package/README.md +1 -1
- package/bin/codekeep +8 -0
- package/entry/command +1 -1
- package/hooks/hooks.json +1 -1
- package/package.json +2 -2
- package/setup.mjs +1 -1
- package/skills/setup-codekeep/SKILL.md +80 -0
- package/src/base/commandline.ts +2 -2
- package/src/base/confirm.ts +26 -10
- package/src/base/entry.ts +2 -2
- package/src/base/version.ts +1 -1
- package/src/cli.ts +3 -3
- package/src/codekeep.ts +8 -0
- package/src/keep/check.ts +296 -0
- package/src/keep/commands.ts +48 -0
- package/src/keep/config.ts +139 -0
- package/src/keep/eslint.ts +233 -0
- package/src/keep/forktree.ts +86 -0
- package/src/keep/full.ts +29 -0
- package/src/keep/index.ts +13 -0
- package/src/keep/loosen.ts +163 -0
- package/src/keep/prettier.ts +58 -0
- package/src/keep/secrets.ts +63 -0
- package/src/keep/session.ts +22 -0
- package/src/keep/tsconfig.ts +115 -0
- package/src/mcp.ts +89 -30
- package/src/modules.ts +3 -2
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// codekeep check:整组里"代码规范"的那一段(见 full.ts),也可以单独跑。依次做四件事,一件没过就停:
|
|
2
|
+
// 1. 配置写得对不对(.codekeep/config.json),写到的文件在不在;现在这一版 eslint、prettier、tsconfig 生效的配置算不算得出来。
|
|
3
|
+
// 2. 和分叉点比,规范改松了哪些(loosen.ts、eslint.ts、prettier.ts、tsconfig.ts;biome 只看配置文件改没改)。放松的条目交给底座的用户确认(askUser),
|
|
4
|
+
// 还没同意的这一段不过,照底座给的话让 agent 调确认工具。分叉点上还没有配置(首次接入)的不比、不问。
|
|
5
|
+
// 分叉点那一版算不出来的(工具在那一版上跑不起来),不当成没放松,列成"程序看不出"的一项,问。
|
|
6
|
+
// 3. 依次跑 checks 里的命令(从仓库根目录用 sh 跑),一条没过就停。
|
|
7
|
+
// 4. secrets 开着时用 gitleaks 扫这次改动(secrets.ts)。
|
|
8
|
+
// 退出码用底座的:0 通过;1 没通过;2 配置、环境问题(配置写错、工具没装、算不出分叉点)。
|
|
9
|
+
|
|
10
|
+
import { spawnSync } from 'node:child_process'
|
|
11
|
+
import { existsSync } from 'node:fs'
|
|
12
|
+
import { join } from 'node:path'
|
|
13
|
+
import { askUser, type ConfirmItem } from '../base/confirm.ts'
|
|
14
|
+
import { EXIT } from '../base/exit.ts'
|
|
15
|
+
import { PROJECT_ENV } from '../base/execute.ts'
|
|
16
|
+
import { filesNow, forkPoint } from '../base/git.ts'
|
|
17
|
+
import { judgingFile } from '../base/judging.ts'
|
|
18
|
+
import { hash } from '../base/records.ts'
|
|
19
|
+
import type { Repo } from '../base/repo.ts'
|
|
20
|
+
import { type Config, CONFIG_PATH, joined, KEEP_DIR, missingFiles, parseConfig } from './config.ts'
|
|
21
|
+
import { eslintItems, type FileRules, fileRules, isCode, loadEslint, stable } from './eslint.ts'
|
|
22
|
+
import { withForkTree } from './forktree.ts'
|
|
23
|
+
import { BIOME_CONFIGS, biomeItem, configItems, GITLEAKS_CONFIG, GITLEAKS_IGNORE, gitleaksItems, removedItem, suppressionsItem } from './loosen.ts'
|
|
24
|
+
import { checkedFiles, loadPrettier, prettierItem } from './prettier.ts'
|
|
25
|
+
import { scanSecrets } from './secrets.ts'
|
|
26
|
+
import { findTsc, type TsSettings, tsconfigItem, tsSettings } from './tsconfig.ts'
|
|
27
|
+
|
|
28
|
+
/** 弹窗开头说这是什么的半句。 */
|
|
29
|
+
const HEADING = '这次改动里放松代码规范的地方'
|
|
30
|
+
|
|
31
|
+
const firstLine = (error: unknown): string => (error instanceof Error ? error.message : String(error)).split('\n')[0] ?? ''
|
|
32
|
+
|
|
33
|
+
/** 现在这一版 eslint、prettier、tsconfig 生效的配置。files 是工作区里现有的文件。 */
|
|
34
|
+
interface NowSide {
|
|
35
|
+
files: string[]
|
|
36
|
+
eslint?: FileRules
|
|
37
|
+
prettier?: Set<string>
|
|
38
|
+
tsconfig: Map<string, TsSettings>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 现在这一版算出来;算不出来的、eslint 一个文件都不查的说清楚(配置、环境问题)。 */
|
|
42
|
+
async function nowSide(root: string, config: Config): Promise<NowSide | { problems: string[] }> {
|
|
43
|
+
const problems: string[] = []
|
|
44
|
+
const side: NowSide = { files: filesNow(root), tsconfig: new Map() }
|
|
45
|
+
if (config.eslint) {
|
|
46
|
+
try {
|
|
47
|
+
const eslint = await loadEslint(root, config.eslint.config)
|
|
48
|
+
side.eslint = await fileRules(eslint, root, config.eslint.config, side.files.filter(isCode))
|
|
49
|
+
if (![...side.eslint.values()].some(Boolean)) problems.push(`${config.eslint.config} 一个代码文件都没 lint 到(都被忽略了,或配置没覆盖它们)`)
|
|
50
|
+
} catch (error) {
|
|
51
|
+
problems.push(`eslint 生效的规则算不出来(${config.eslint.config}):${firstLine(error)}`)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (config.prettier) {
|
|
55
|
+
try {
|
|
56
|
+
side.prettier = await checkedFiles(await loadPrettier(root), root, side.files)
|
|
57
|
+
} catch (error) {
|
|
58
|
+
problems.push(`格式检查查哪些文件算不出来(prettier):${firstLine(error)}`)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const path of config.tsconfig) {
|
|
62
|
+
try {
|
|
63
|
+
side.tsconfig.set(path, tsSettings(findTsc(root, path), root, path))
|
|
64
|
+
} catch (error) {
|
|
65
|
+
problems.push(`${path} 生效的选项算不出来:${firstLine(error)}`)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return problems.length ? { problems } : side
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 放松的条目,和分叉点那一版算不出来的原因(只给 agent 看,不进弹窗)。 */
|
|
72
|
+
interface Loosened {
|
|
73
|
+
items: ConfirmItem[]
|
|
74
|
+
reasons: string[]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 分叉点那一版算不出来:列成"程序看不出"的一项。同意过一次后,现在这一版算出来的不变就不再问。 */
|
|
78
|
+
function unknownItem(kind: string, before: unknown, now: unknown, text: string): ConfirmItem {
|
|
79
|
+
return { before: stable([kind, before]), after: hash(stable([kind, now])), text: `${text}:分叉点那一版算不出来,程序看不出这次是改松还是改紧` }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 要跑工具才比得出来的:eslint 生效的规则、格式检查查哪些文件、tsconfig 的严格选项。分叉点那一版在临时 worktree 里算。 */
|
|
83
|
+
async function toolItems(root: string, fork: string, before: Config, now: Config, side: NowSide): Promise<Loosened> {
|
|
84
|
+
const result: Loosened = { items: [], reasons: [] }
|
|
85
|
+
const lint = Boolean(before.eslint && side.eslint)
|
|
86
|
+
const format = Boolean(before.prettier && side.prettier)
|
|
87
|
+
const tsconfigs = before.tsconfig.filter(path => now.tsconfig.includes(path))
|
|
88
|
+
if (!lint && !format && tsconfigs.length === 0) return result
|
|
89
|
+
const unknown = {
|
|
90
|
+
lint: (reason: string): void => {
|
|
91
|
+
result.reasons.push(`eslint 生效的规则(分叉点那一版):${reason}`)
|
|
92
|
+
result.items.push(unknownItem('eslint-unknown', before.eslint, [...side.eslint!], 'lint 规则'))
|
|
93
|
+
},
|
|
94
|
+
format: (reason: string): void => {
|
|
95
|
+
result.reasons.push(`格式检查查哪些文件(分叉点那一版):${reason}`)
|
|
96
|
+
result.items.push(unknownItem('prettier-unknown', true, [...side.prettier!].sort(), '格式检查查哪些文件'))
|
|
97
|
+
},
|
|
98
|
+
tsconfig: (path: string, reason: string): void => {
|
|
99
|
+
result.reasons.push(`${path}(分叉点那一版):${reason}`)
|
|
100
|
+
result.items.push(unknownItem('tsconfig-unknown', path, side.tsconfig.get(path), `类型检查的配置 ${path}`))
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
await withForkTree(root, fork, async tree => {
|
|
105
|
+
// 两边都有的文件:这次新加的没有"原来"
|
|
106
|
+
const shared = side.files.filter(file => existsSync(join(tree.root, file)))
|
|
107
|
+
if (lint) {
|
|
108
|
+
try {
|
|
109
|
+
const eslint = await loadEslint(root, now.eslint!.config)
|
|
110
|
+
const old = await fileRules(eslint, tree.root, before.eslint!.config, shared.filter(isCode))
|
|
111
|
+
result.items.push(...eslintItems(old, side.eslint!))
|
|
112
|
+
} catch (error) {
|
|
113
|
+
unknown.lint(firstLine(error))
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (format) {
|
|
117
|
+
try {
|
|
118
|
+
const old = await checkedFiles(await loadPrettier(root), tree.root, shared)
|
|
119
|
+
const item = prettierItem(old, side.prettier!, shared)
|
|
120
|
+
if (item) result.items.push(item)
|
|
121
|
+
} catch (error) {
|
|
122
|
+
unknown.format(firstLine(error))
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
for (const path of tsconfigs) {
|
|
126
|
+
try {
|
|
127
|
+
const item = tsconfigItem(path, tsSettings(findTsc(root, path), tree.root, path), side.tsconfig.get(path)!)
|
|
128
|
+
if (item) result.items.push(item)
|
|
129
|
+
} catch (error) {
|
|
130
|
+
unknown.tsconfig(path, firstLine(error))
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
} catch (error) {
|
|
135
|
+
// 临时 worktree 建不起来:都算不出来
|
|
136
|
+
const reason = `分叉点的临时 worktree 建不起来:${firstLine(error)}`
|
|
137
|
+
result.items = []
|
|
138
|
+
result.reasons = []
|
|
139
|
+
if (lint) unknown.lint(reason)
|
|
140
|
+
if (format) unknown.format(reason)
|
|
141
|
+
for (const path of tsconfigs) unknown.tsconfig(path, reason)
|
|
142
|
+
}
|
|
143
|
+
return result
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 和分叉点那一版比,放松了哪些。beforeText 是分叉点上的配置文件(没有就是首次接入,不比)。 */
|
|
147
|
+
async function loosened(root: string, fork: string, beforeText: string | undefined, now: Config, side: NowSide): Promise<Loosened> {
|
|
148
|
+
if (beforeText === undefined) return { items: [], reasons: [] }
|
|
149
|
+
const before = parseConfig(beforeText).config
|
|
150
|
+
if (!before) {
|
|
151
|
+
return {
|
|
152
|
+
items: [{ before: stable(['config-unknown', beforeText]), after: stable(['config-unknown', now]), text: `分叉点上的 ${CONFIG_PATH} 写得不对、读不出来,程序看不出这次规范是改松还是改紧` }],
|
|
153
|
+
reasons: [`分叉点上的 ${CONFIG_PATH} 读不出来:${parseConfig(beforeText).problems.join(';')}`],
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const tools = await toolItems(root, fork, before, now, side)
|
|
157
|
+
const items = [...configItems(before, now), ...tools.items]
|
|
158
|
+
if (before.eslint && now.eslint) {
|
|
159
|
+
const item = suppressionsItem(now.eslint.suppressions, judgingFile(root, fork, now.eslint.suppressions))
|
|
160
|
+
if (item) items.push(item)
|
|
161
|
+
}
|
|
162
|
+
if (before.secrets) items.push(...gitleaksItems(judgingFile(root, fork, GITLEAKS_IGNORE), judgingFile(root, fork, GITLEAKS_CONFIG)))
|
|
163
|
+
for (const path of BIOME_CONFIGS) {
|
|
164
|
+
const item = biomeItem(path, judgingFile(root, fork, path))
|
|
165
|
+
if (item) items.push(item)
|
|
166
|
+
}
|
|
167
|
+
return { items, reasons: tools.reasons }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 一项的头一句:第一行以冒号结尾的(下面逐行列的),带上第二行。 */
|
|
171
|
+
function headline(text: string): string {
|
|
172
|
+
const [first = '', second] = text.split('\n')
|
|
173
|
+
return first.endsWith(':') && second ? `${first}${second}` : first
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** 没同意的放松:说给 agent 的话。 */
|
|
177
|
+
function askLines(items: ConfirmItem[], ask: string[], reasons: string[]): string[] {
|
|
178
|
+
return [
|
|
179
|
+
`规范改松了 ${items.length} 项(和分叉点上的比),要用户同意:`,
|
|
180
|
+
...items.map(item => ` · ${headline(item.text)}`),
|
|
181
|
+
...(reasons.length ? ['分叉点那一版算不出来的原因(不用给用户看):', ...reasons.map(reason => ` · ${reason}`)] : []),
|
|
182
|
+
'用户不同意的,改回去,按规范改代码。',
|
|
183
|
+
...ask,
|
|
184
|
+
]
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** 跑 checks 里的命令,一条没过就停。返回没过的那条和原因。 */
|
|
188
|
+
function runChecks(root: string, config: Config): { name: string; cause: string } | undefined {
|
|
189
|
+
for (const check of config.checks) {
|
|
190
|
+
console.log(`== 规范检查:${check.name}(${check.command})`)
|
|
191
|
+
const r = spawnSync('/bin/sh', ['-c', check.command], { cwd: root, env: PROJECT_ENV, stdio: ['ignore', 'inherit', 'inherit'] })
|
|
192
|
+
if (r.status === 0) continue
|
|
193
|
+
const cause = r.status !== null ? `退出码 ${r.status}` : r.signal ? `被信号 ${r.signal} 终止` : `没能启动:${r.error?.message ?? ''}`
|
|
194
|
+
return { name: check.name, cause }
|
|
195
|
+
}
|
|
196
|
+
return undefined
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function check(repo: Repo): Promise<number> {
|
|
200
|
+
const { root } = repo
|
|
201
|
+
const fork = forkPoint(root)
|
|
202
|
+
if (!joined(root, fork.commit)) {
|
|
203
|
+
console.log(`这个仓库没有接入 codekeep(没有 ${KEEP_DIR}/ 目录),不检查。`)
|
|
204
|
+
return EXIT.ok
|
|
205
|
+
}
|
|
206
|
+
if ('problem' in fork) {
|
|
207
|
+
console.error(`没法检查:${fork.problem}(规范改没改松要和分叉点上的配置比)`)
|
|
208
|
+
return EXIT.usage
|
|
209
|
+
}
|
|
210
|
+
const judging = judgingFile(root, fork.commit, CONFIG_PATH)
|
|
211
|
+
|
|
212
|
+
// 配置删掉了:分叉点上有能用的配置的,删掉算放松,用户同意过就退出 codekeep
|
|
213
|
+
if (judging.now === undefined) {
|
|
214
|
+
const before = judging.before === undefined ? undefined : parseConfig(judging.before).config
|
|
215
|
+
if (!before) {
|
|
216
|
+
const hasDir = existsSync(join(root, KEEP_DIR))
|
|
217
|
+
console.error(
|
|
218
|
+
hasDir
|
|
219
|
+
? `codekeep check:有 ${KEEP_DIR}/ 目录但没有 ${CONFIG_PATH},照 gatelight:setup-codekeep 技能写好配置。`
|
|
220
|
+
: `codekeep check:工作区里删掉了 ${KEEP_DIR}/,分叉点上的 ${CONFIG_PATH} 又不在或写得不对:没有能用的配置,没法判断这次改松了什么。要继续用 codekeep 就恢复配置;要退出,先在默认分支上把配置改好,再删掉 ${KEEP_DIR}/。`,
|
|
221
|
+
)
|
|
222
|
+
return EXIT.usage
|
|
223
|
+
}
|
|
224
|
+
const item = removedItem(before, existsSync(join(root, KEEP_DIR)))
|
|
225
|
+
const asked = askUser(repo, 'keep', HEADING, [item])
|
|
226
|
+
if (!asked.ask) {
|
|
227
|
+
console.log('codekeep check:通过。配置删掉了,用户同意过:这份改动退出 codekeep,不再检查。')
|
|
228
|
+
return EXIT.ok
|
|
229
|
+
}
|
|
230
|
+
console.log(['codekeep check:没通过。', '', ...askLines([item], asked.ask.lines, [])].join('\n'))
|
|
231
|
+
return EXIT.fail
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// 1. 配置
|
|
235
|
+
const parsed = parseConfig(judging.now)
|
|
236
|
+
const problems = parsed.config ? missingFiles(root, parsed.config) : parsed.problems
|
|
237
|
+
if (!parsed.config || problems.length) {
|
|
238
|
+
console.error([`codekeep check:配置有问题(${CONFIG_PATH}),先改好再检查:`, ...problems.map(text => ` ✗ ${text}`)].join('\n'))
|
|
239
|
+
return EXIT.usage
|
|
240
|
+
}
|
|
241
|
+
const config = parsed.config
|
|
242
|
+
const side = await nowSide(root, config)
|
|
243
|
+
if ('problems' in side) {
|
|
244
|
+
console.error([`codekeep check:现在这一版的规范配置算不出来,先修好(依赖没装的先装好):`, ...side.problems.map(text => ` ✗ ${text}`)].join('\n'))
|
|
245
|
+
return EXIT.usage
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 2. 放松
|
|
249
|
+
const found = await loosened(root, fork.commit, judging.before, config, side)
|
|
250
|
+
const asked = askUser(repo, 'keep', HEADING, found.items)
|
|
251
|
+
if (asked.ask) {
|
|
252
|
+
console.log(['codekeep check:没通过。', '', ...askLines(asked.pending, asked.ask.lines, found.reasons)].join('\n'))
|
|
253
|
+
return EXIT.fail
|
|
254
|
+
}
|
|
255
|
+
const loosenedLine = found.items.length ? `规范改松 ${found.items.length} 项,用户都同意过` : '规范配置没有改松(和分叉点上的比)'
|
|
256
|
+
|
|
257
|
+
// 3. 规范检查
|
|
258
|
+
const failed = runChecks(root, config)
|
|
259
|
+
if (failed) {
|
|
260
|
+
console.log(
|
|
261
|
+
`\ncodekeep check:没通过,规范检查"${failed.name}"没过(${failed.cause})。按上面的输出改代码;不要为了过关改松规范(关规则、降级、调高上限、加忽略、加豁免),确实要改的先用白话跟用户讲清。`,
|
|
262
|
+
)
|
|
263
|
+
return EXIT.fail
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// 4. 密钥
|
|
267
|
+
if (config.secrets) {
|
|
268
|
+
const secrets = scanSecrets(root, fork.commit)
|
|
269
|
+
if ('missing' in secrets) {
|
|
270
|
+
console.error(
|
|
271
|
+
`\ncodekeep check:没通过,找不到 gitleaks(${CONFIG_PATH} 开着 secrets,要用它扫密钥)。这是装在这台机器上的工具,先问用户;同意后装:brew install gitleaks(没有 Homebrew 的见 https://github.com/gitleaks/gitleaks#installing)。`,
|
|
272
|
+
)
|
|
273
|
+
return EXIT.usage
|
|
274
|
+
}
|
|
275
|
+
if ('problem' in secrets) {
|
|
276
|
+
console.error(`\ncodekeep check:没通过,密钥没扫成:${secrets.problem}`)
|
|
277
|
+
return EXIT.usage
|
|
278
|
+
}
|
|
279
|
+
if ('leaks' in secrets) {
|
|
280
|
+
console.log(
|
|
281
|
+
[
|
|
282
|
+
'',
|
|
283
|
+
'codekeep check:没通过,gitleaks 在这次改动里扫出了疑似密钥(上面列出,已打码)。改法:',
|
|
284
|
+
' · 真的密钥、密码:从代码里拿掉,改成从环境变量或不进版本库的本地配置读,并告诉用户这个密钥可能已经泄露、要换掉。已经提交了的,要改写这个分支上的那几个提交才去得掉,先问用户。',
|
|
285
|
+
' · 测试用的假值:改成一眼看得出是假的写法(比如 test-password-not-real)。',
|
|
286
|
+
` · 确实不是密钥的:把那一条的 Fingerprint 加进 ${GITLEAKS_IGNORE}(算放松,整组会要用户确认)。`,
|
|
287
|
+
].join('\n'),
|
|
288
|
+
)
|
|
289
|
+
return EXIT.fail
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const names = config.checks.map(item => item.name).join('、')
|
|
294
|
+
console.log(`\ncodekeep check:通过。${loosenedLine};规范检查都过了(${names})${config.secrets ? ';这次改动里没扫出密钥' : ''}。`)
|
|
295
|
+
return EXIT.ok
|
|
296
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// codekeep 的命令:check。命令入口(src/codekeep.ts)分发到这里。
|
|
2
|
+
// check 也是整组里的一段(见 full.ts):接入了的仓库,testpace full 在测试之前跑它,不用另外登记。
|
|
3
|
+
|
|
4
|
+
import { EXIT } from '../base/exit.ts'
|
|
5
|
+
import { openRepo } from '../base/repo.ts'
|
|
6
|
+
import { check } from './check.ts'
|
|
7
|
+
import { CONFIG_PATH } from './config.ts'
|
|
8
|
+
|
|
9
|
+
const USAGE = `用法:
|
|
10
|
+
codekeep check 规范检查:${CONFIG_PATH} 写得对不对;和分叉点比规范改松了哪些(改松的要用户确认);
|
|
11
|
+
依次跑 checks 里的命令;secrets 开着时用 gitleaks 扫这次改动里的密钥。
|
|
12
|
+
接入了的仓库,testpace full 里自动跑它`
|
|
13
|
+
|
|
14
|
+
/** 退出码用底座的:0 通过;1 没通过;2 用法、配置或环境问题。 */
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 在 agent 的沙箱里跑不了的子命令和它要做的事(见 base/commandline.ts)。
|
|
18
|
+
* check 要跑项目的检查命令、在系统临时目录建分叉点的临时 worktree、写 .git/testpace/ 的确认记录,沙箱里会被拒。
|
|
19
|
+
*/
|
|
20
|
+
export function outside(argv: string[]): string | undefined {
|
|
21
|
+
return argv[0] === 'check' ? `跑 ${CONFIG_PATH} 里的检查命令和 gitleaks、在系统临时目录建分叉点的临时 worktree、写 .git/testpace/ 的记录` : undefined
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function main(argv: string[]): Promise<number> {
|
|
25
|
+
switch (argv[0]) {
|
|
26
|
+
case 'check': {
|
|
27
|
+
if (argv.length > 1) {
|
|
28
|
+
console.error(`不认识的参数:${argv.slice(1).join(' ')}\n${USAGE}`)
|
|
29
|
+
return EXIT.usage
|
|
30
|
+
}
|
|
31
|
+
const repo = openRepo(process.cwd())
|
|
32
|
+
if (!repo) {
|
|
33
|
+
console.error('codekeep 要在 git 仓库里运行。')
|
|
34
|
+
return EXIT.usage
|
|
35
|
+
}
|
|
36
|
+
return check(repo)
|
|
37
|
+
}
|
|
38
|
+
case undefined:
|
|
39
|
+
case '-h':
|
|
40
|
+
case '--help':
|
|
41
|
+
case 'help':
|
|
42
|
+
console.log(USAGE)
|
|
43
|
+
return argv[0] === undefined ? EXIT.usage : EXIT.ok
|
|
44
|
+
default:
|
|
45
|
+
console.error(`不认识的命令:${argv[0]}\n${USAGE}`)
|
|
46
|
+
return EXIT.usage
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// "代码规范"在项目里的配置:.codekeep/config.json。没有 .codekeep/ 目录的仓库没接入,它不出声、不加检查。
|
|
2
|
+
// checks 规范检查的命令(类型检查、lint、格式),直接写工具命令,不经过项目的脚本;整组里依次跑
|
|
3
|
+
// eslint 拿来比"生效的规则"的 eslint:config 是配置文件(按它对每个代码文件算生效的规则),
|
|
4
|
+
// suppressions 是批量豁免文件(eslint --suppress-all 生成的,不写就是仓库根目录的 eslint-suppressions.json)
|
|
5
|
+
// prettier 开着时比格式检查查哪些文件(prettier 在仓库根目录跑,查 git 收的文件)
|
|
6
|
+
// tsconfig 拿来比严格选项的 tsconfig 文件
|
|
7
|
+
// secrets 开着时整组里用 gitleaks 扫这次的提交和没提交的改动
|
|
8
|
+
// 这份配置是评判用的配置(base/judging.ts),走"用户同意后生效":改松的地方见 loosen.ts。
|
|
9
|
+
|
|
10
|
+
import { existsSync } from 'node:fs'
|
|
11
|
+
import { join, posix } from 'node:path'
|
|
12
|
+
import { fileNow, hasFile } from '../base/git.ts'
|
|
13
|
+
|
|
14
|
+
export const KEEP_DIR = '.codekeep'
|
|
15
|
+
export const CONFIG_PATH = `${KEEP_DIR}/config.json`
|
|
16
|
+
/** eslint 批量豁免文件的默认位置(eslint 自己的默认名字,相对仓库根目录)。 */
|
|
17
|
+
export const DEFAULT_SUPPRESSIONS = 'eslint-suppressions.json'
|
|
18
|
+
|
|
19
|
+
export interface Check {
|
|
20
|
+
name: string
|
|
21
|
+
command: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface EslintConfig {
|
|
25
|
+
config: string
|
|
26
|
+
suppressions: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface Config {
|
|
30
|
+
checks: Check[]
|
|
31
|
+
eslint?: EslintConfig
|
|
32
|
+
tsconfig: string[]
|
|
33
|
+
prettier: boolean
|
|
34
|
+
secrets: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface Parsed {
|
|
38
|
+
config?: Config
|
|
39
|
+
problems: string[]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 接入了:分叉点或工作区里有 .codekeep/。 */
|
|
43
|
+
export function joined(root: string, fork?: string): boolean {
|
|
44
|
+
return existsSync(join(root, KEEP_DIR)) || (fork !== undefined && hasFile(root, fork, KEEP_DIR))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const KEYS = ['checks', 'eslint', 'tsconfig', 'prettier', 'secrets']
|
|
48
|
+
const ESLINT_KEYS = ['config', 'suppressions']
|
|
49
|
+
|
|
50
|
+
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
51
|
+
|
|
52
|
+
/** 仓库里的相对路径:去掉开头的 ./;不能是绝对路径,不能有 ..、空段、通配。 */
|
|
53
|
+
function relativePath(value: unknown): string | undefined {
|
|
54
|
+
if (typeof value !== 'string') return undefined
|
|
55
|
+
const plain = value.replace(/^(?:\.\/)+/, '')
|
|
56
|
+
if (plain === '' || plain.startsWith('/') || plain.includes('\\') || /[*?[\]{}]/.test(plain)) return undefined
|
|
57
|
+
if (plain.split('/').some(segment => segment === '' || segment === '.' || segment === '..')) return undefined
|
|
58
|
+
return posix.normalize(plain) === plain ? plain : undefined
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 读配置的文字,只看写法(字段、类型、路径的写法),不看文件在不在:分叉点那一版也用它读。
|
|
63
|
+
* 工作区这一版另用 missingFiles 查写到的文件在不在。
|
|
64
|
+
*/
|
|
65
|
+
export function parseConfig(text: string): Parsed {
|
|
66
|
+
let raw: unknown
|
|
67
|
+
try {
|
|
68
|
+
raw = JSON.parse(text)
|
|
69
|
+
} catch (error) {
|
|
70
|
+
return { problems: [`${CONFIG_PATH} 不是合法的 JSON:${(error as Error).message}`] }
|
|
71
|
+
}
|
|
72
|
+
if (!isRecord(raw)) return { problems: [`${CONFIG_PATH} 应该是一个对象`] }
|
|
73
|
+
const problems: string[] = []
|
|
74
|
+
const extra = Object.keys(raw).filter(key => !KEYS.includes(key))
|
|
75
|
+
if (extra.length) problems.push(`${CONFIG_PATH} 有未知字段 ${extra.map(key => `"${key}"`).join('、')}(认得的有 ${KEYS.join('、')})`)
|
|
76
|
+
|
|
77
|
+
const checks: Check[] = []
|
|
78
|
+
if (!Array.isArray(raw.checks) || raw.checks.length === 0) {
|
|
79
|
+
problems.push(`${CONFIG_PATH} 的 "checks" 要写成非空数组,每条 { "name": 名字, "command": 命令 }`)
|
|
80
|
+
} else {
|
|
81
|
+
raw.checks.forEach((item: unknown, index) => {
|
|
82
|
+
const where = `checks 的第 ${index + 1} 条`
|
|
83
|
+
if (!isRecord(item)) return void problems.push(`${where} 应该是 { "name": 名字, "command": 命令 }`)
|
|
84
|
+
const unknown = Object.keys(item).filter(key => key !== 'name' && key !== 'command')
|
|
85
|
+
if (unknown.length) problems.push(`${where} 有未知字段 ${unknown.map(key => `"${key}"`).join('、')}`)
|
|
86
|
+
const { name, command } = item
|
|
87
|
+
if (typeof name !== 'string' || name.trim() === '' || /[\n\r]/.test(name)) return void problems.push(`${where} 要写名字(name,一行)`)
|
|
88
|
+
if (typeof command !== 'string' || command.trim() === '') return void problems.push(`${where}(${name})要写命令(command)`)
|
|
89
|
+
if (checks.some(check => check.name === name.trim())) return void problems.push(`${where} 的名字 "${name}" 和前面的重复了`)
|
|
90
|
+
checks.push({ name: name.trim(), command: command.trim() })
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let eslint: EslintConfig | undefined
|
|
95
|
+
if (raw.eslint !== undefined) {
|
|
96
|
+
const value = raw.eslint
|
|
97
|
+
if (!isRecord(value)) problems.push(`${CONFIG_PATH} 的 "eslint" 应该是 { "config": 配置文件, "suppressions": 豁免文件(可选) }`)
|
|
98
|
+
else {
|
|
99
|
+
const unknown = Object.keys(value).filter(key => !ESLINT_KEYS.includes(key))
|
|
100
|
+
if (unknown.length) problems.push(`${CONFIG_PATH} 的 "eslint" 有未知字段 ${unknown.map(key => `"${key}"`).join('、')}`)
|
|
101
|
+
const config = relativePath(value.config)
|
|
102
|
+
if (config === undefined) problems.push(`${CONFIG_PATH} 的 "eslint.config" 要写 eslint 配置文件(仓库里的相对路径,比如 "eslint.config.mjs")`)
|
|
103
|
+
const suppressions = value.suppressions === undefined ? DEFAULT_SUPPRESSIONS : relativePath(value.suppressions)
|
|
104
|
+
if (suppressions === undefined) problems.push(`${CONFIG_PATH} 的 "eslint.suppressions" 要写批量豁免文件(仓库里的相对路径)`)
|
|
105
|
+
if (config !== undefined && suppressions !== undefined) eslint = { config, suppressions }
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const tsconfig = raw.tsconfig === undefined ? [] : paths(raw.tsconfig, 'tsconfig', 'tsconfig 文件', problems)
|
|
110
|
+
for (const key of ['prettier', 'secrets']) {
|
|
111
|
+
if (raw[key] !== undefined && typeof raw[key] !== 'boolean') problems.push(`${CONFIG_PATH} 的 "${key}" 要写 true 或 false`)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (problems.length || tsconfig === undefined) return { problems }
|
|
115
|
+
return { config: { checks, ...(eslint ? { eslint } : {}), tsconfig, prettier: raw.prettier === true, secrets: raw.secrets === true }, problems }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 一组仓库里的文件:非空数组,不重复。 */
|
|
119
|
+
function paths(value: unknown, name: string, what: string, problems: string[]): string[] | undefined {
|
|
120
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
121
|
+
problems.push(`${CONFIG_PATH} 的 "${name}" 要写成非空数组,每项是${what}(仓库里的相对路径)`)
|
|
122
|
+
return undefined
|
|
123
|
+
}
|
|
124
|
+
const found = problems.length
|
|
125
|
+
const result: string[] = []
|
|
126
|
+
for (const item of value) {
|
|
127
|
+
const path = relativePath(item)
|
|
128
|
+
if (path === undefined) problems.push(`${name} 里的 ${JSON.stringify(item)} 要写${what}(仓库里的相对路径,不用通配)`)
|
|
129
|
+
else if (result.includes(path)) problems.push(`${name} 里的 "${path}" 写了两次`)
|
|
130
|
+
else result.push(path)
|
|
131
|
+
}
|
|
132
|
+
return problems.length > found ? undefined : result
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 工作区这一版配置里写到、却不在的文件(批量豁免文件可以还没有)。 */
|
|
136
|
+
export function missingFiles(root: string, config: Config): string[] {
|
|
137
|
+
const listed = [...(config.eslint ? [config.eslint.config] : []), ...config.tsconfig]
|
|
138
|
+
return listed.filter(path => fileNow(root, path) === undefined).map(path => `${CONFIG_PATH} 里写的 ${path} 不在(要写仓库里现有的文件)`)
|
|
139
|
+
}
|