gatelight 0.19.3 → 0.40.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 +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/README.md +2 -2
- package/package.json +1 -1
- package/skills/merge/SKILL.md +1 -1
- package/skills/modular/SKILL.md +6 -6
- package/skills/setup/SKILL.md +2 -2
- package/skills/setup-codekeep/SKILL.md +61 -17
- package/skills/setup-codekeep/frontend.eslint.mjs +287 -0
- package/skills/setup-modwall/SKILL.md +10 -11
- package/skills/setup-testcraft/SKILL.md +9 -3
- package/skills/setup-testcraft/checklist/dev-login.md +3 -3
- package/skills/setup-testcraft/checklist/same-number.md +26 -0
- package/skills/write-tests/SKILL.md +12 -12
- package/src/base/baseline.ts +2 -2
- package/src/base/commands.ts +2 -40
- package/src/base/execute.ts +1 -11
- package/src/base/exit.ts +1 -1
- package/src/base/full.ts +7 -10
- package/src/base/git.ts +34 -3
- package/src/base/hook.ts +71 -17
- package/src/base/hookformat.ts +16 -6
- package/src/base/layermap.ts +6 -3
- package/src/base/module.ts +8 -8
- package/src/base/repo.ts +8 -34
- package/src/base/sessions.ts +45 -13
- package/src/base/shell.ts +18 -10
- package/src/base/stop.ts +20 -6
- package/src/base/store.ts +15 -30
- package/src/cli.ts +3 -16
- package/src/craft/commands.ts +7 -1
- package/src/craft/defaults.ts +59 -0
- package/src/craft/session.ts +8 -4
- package/src/keep/check.ts +107 -40
- package/src/keep/config.ts +56 -26
- package/src/keep/eslint.ts +144 -6
- package/src/keep/loosen.ts +11 -61
- package/src/keep/session.ts +19 -5
- package/src/keep/styles.ts +72 -0
- package/src/pace/commands.ts +9 -3
- package/src/pace/full.ts +14 -25
- package/src/pace/index.ts +3 -0
- package/src/pace/record.ts +12 -6
- package/src/pace/results.ts +43 -2
- package/src/pace/run/playwright-reporter.mjs +5 -4
- package/src/pace/run/runner.ts +13 -7
- package/src/pace/run/targets.ts +3 -2
- package/src/pace/session.ts +3 -3
- package/src/pace/unfinished.ts +87 -0
- package/src/pace/unmerged.ts +87 -0
- package/src/pace/verify.ts +2 -2
- package/src/wall/commands.ts +92 -26
- package/src/wall/database.ts +12 -1
- package/src/wall/dbcheck.ts +2 -2
- package/src/wall/dbrules.ts +60 -19
- package/src/wall/literals.ts +162 -0
- package/src/wall/migrate.ts +248 -0
- package/src/wall/project.ts +29 -9
- package/src/wall/receipt.ts +2 -1
- package/src/wall/session.ts +5 -5
- package/src/wall/store.ts +3 -1
package/src/craft/commands.ts
CHANGED
|
@@ -5,9 +5,11 @@ import { readdirSync, readFileSync } from 'node:fs'
|
|
|
5
5
|
import { join } from 'node:path'
|
|
6
6
|
import { EXIT } from '../base/exit.ts'
|
|
7
7
|
import { openRepo } from '../base/repo.ts'
|
|
8
|
+
import { VERSION } from '../base/version.ts'
|
|
8
9
|
import { open, type Snapshot } from './change.ts'
|
|
9
10
|
import { baseChecklist, changedChecklist, hitsOf } from './checklist.ts'
|
|
10
11
|
import { CHECKLIST_DIR, RULES_DIR } from './config.ts'
|
|
12
|
+
import { missingText } from './defaults.ts'
|
|
11
13
|
|
|
12
14
|
const USAGE = `用法:
|
|
13
15
|
testcraft checklist 清单(.testcraft/checklist/)里这次改动命中的条目和各条怎么查:按条目查一遍。
|
|
@@ -58,17 +60,21 @@ function checklist(s: Snapshot): number {
|
|
|
58
60
|
}
|
|
59
61
|
if (base.problems.length) lines.push('', '分叉点上这几条写得不对,这次没按它们提醒(不拦这次改动,另找时间修好):', ...base.problems.map(problem => ` · ${problem}`))
|
|
60
62
|
if (changed.problems.length) lines.push('', '这次加、改的条目写得不对(合并后要用得上):', ...changed.problems.map(problem => ` ✗ ${problem}`))
|
|
63
|
+
const missing = missingText(s.root)
|
|
64
|
+
if (missing) lines.push('', missing)
|
|
61
65
|
console.log(lines.join('\n'))
|
|
62
66
|
return changed.problems.length ? EXIT.fail : EXIT.ok
|
|
63
67
|
}
|
|
64
68
|
|
|
65
69
|
function status(s: Snapshot): number {
|
|
66
|
-
const lines: string[] = [
|
|
70
|
+
const lines: string[] = [`gatelight ${VERSION}。和 ${s.branch} 的分叉点 ${s.base.slice(0, 8)} 比,这次改了 ${s.changes.length} 个文件。`]
|
|
67
71
|
const base = baseChecklist(s)
|
|
68
72
|
if (base.items.length) {
|
|
69
73
|
const hits = hitsOf(s, base.items)
|
|
70
74
|
lines.push(`清单(${CHECKLIST_DIR}/):${base.items.length} 条,这次命中 ${hits.length} 条${hits.length ? `:${hits.map(hit => hit.item.id).join('、')}(testcraft checklist 列出怎么查)` : ''}`)
|
|
71
75
|
} else lines.push('清单:没有条目')
|
|
76
|
+
const missing = missingText(s.root)
|
|
77
|
+
if (missing) lines.push(missing)
|
|
72
78
|
const rules = listRules(s.root)
|
|
73
79
|
lines.push(rules.length ? `暂定的规则和记过的 bug(${RULES_DIR}/):\n${rules.map(rule => ` - ${rule}`).join('\n')}` : '暂定的规则和记过的 bug:没有')
|
|
74
80
|
console.log(lines.join('\n'))
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// gatelight 自带的默认清单条目(skills/setup-testcraft/checklist/):已接入的项目还没有的新条目,提醒补上。
|
|
2
|
+
// 项目在 .testcraft/defaults 里记一行版本号:补到了 gatelight 哪一版的默认条目(setup-testcraft 接入、补条目时写)。
|
|
3
|
+
// 只提醒比它新、项目清单里又没有同名文件的条目:项目不要的条目,不复制、照样把版本号改到这一版,就不再提醒。
|
|
4
|
+
// 没有这个文件的(这个机制之前接入的)当作一条都没补过:没有的默认条目都提醒。
|
|
5
|
+
|
|
6
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
import { fileURLToPath } from 'node:url'
|
|
9
|
+
import { compareVersions, VERSION } from '../base/version.ts'
|
|
10
|
+
import { CHECKLIST_DIR, TESTCRAFT_DIR } from './config.ts'
|
|
11
|
+
|
|
12
|
+
/** 默认条目放在哪(插件里)。 */
|
|
13
|
+
export const DEFAULTS_DIR = fileURLToPath(new URL('../../skills/setup-testcraft/checklist', import.meta.url))
|
|
14
|
+
|
|
15
|
+
/** 项目补到了哪一版的默认条目。 */
|
|
16
|
+
export const SYNCED_FILE = `${TESTCRAFT_DIR}/defaults`
|
|
17
|
+
|
|
18
|
+
/** 每条默认条目是 gatelight 哪一版加的。加新的默认条目时在这里写上(测试核对和目录里的文件一一对应)。 */
|
|
19
|
+
export const DEFAULT_SINCE: Readonly<Record<string, string>> = {
|
|
20
|
+
'dev-login.md': '0.16.1',
|
|
21
|
+
'same-number.md': '0.22.2',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface MissingDefault {
|
|
25
|
+
file: string
|
|
26
|
+
title: string
|
|
27
|
+
since: string
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 项目记的版本号;没有文件、写的不是版本号的当作 0.0.0(一条都没补过)。 */
|
|
31
|
+
function syncedVersion(root: string): string {
|
|
32
|
+
try {
|
|
33
|
+
const text = readFileSync(join(root, SYNCED_FILE), 'utf8').trim()
|
|
34
|
+
return /^\d+\.\d+\.\d+$/.test(text) ? text : '0.0.0'
|
|
35
|
+
} catch {
|
|
36
|
+
return '0.0.0'
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 比项目记的版本新、项目清单(工作区里的)又没有同名文件的默认条目。 */
|
|
41
|
+
export function missingDefaults(root: string): MissingDefault[] {
|
|
42
|
+
const synced = syncedVersion(root)
|
|
43
|
+
const have = new Set(existsSync(join(root, CHECKLIST_DIR)) ? readdirSync(join(root, CHECKLIST_DIR)) : [])
|
|
44
|
+
return Object.entries(DEFAULT_SINCE)
|
|
45
|
+
.filter(([file, since]) => compareVersions(since, synced) > 0 && !have.has(file))
|
|
46
|
+
.map(([file, since]) => ({ file, since, title: /^#\s+(.+?)\s*$/m.exec(readFileSync(join(DEFAULTS_DIR, file), 'utf8'))?.[1] ?? file }))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 说给 agent 的一段:哪几条没补、怎么补、不要的怎么记。没有要补的返回 undefined。 */
|
|
50
|
+
export function missingText(root: string): string | undefined {
|
|
51
|
+
const missing = missingDefaults(root)
|
|
52
|
+
if (missing.length === 0) return undefined
|
|
53
|
+
const named = missing.map(entry => `${entry.file.replace(/\.md$/, '')}(${entry.title})`).join('、')
|
|
54
|
+
return (
|
|
55
|
+
`gatelight 有 ${missing.length} 条默认清单条目这个仓库还没有:${named}。` +
|
|
56
|
+
`下次在这个仓库的分支上做事时顺手补上,照 gatelight:setup-testcraft 技能"已接入的项目补默认条目"一节,和那个分支一起合;` +
|
|
57
|
+
`补完(或用户说哪条不要)把 ${SYNCED_FILE} 写成 ${VERSION},就不再提醒。`
|
|
58
|
+
)
|
|
59
|
+
}
|
package/src/craft/session.ts
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
// "测试写法"说给 agent 的话,经底座的钩子送出去(base/module.ts 的 brief):会话开始时说几句写测试的约定。
|
|
2
|
-
//
|
|
2
|
+
// 停止门里没有这个模块的项:加测试、删测试、改到清单条目,收尾时都不拦。清单在落地前的整组里提醒(见 full.ts),平时用 testcraft checklist 看。
|
|
3
|
+
// 也不查仓库里留没留记录类的 .md:程序只看得出有没有 .md,分不出过时的方案和文档站、博客、CHANGELOG 这类内容,拦了会误伤,提示里叫删还会让 agent 删掉真内容。
|
|
3
4
|
// 这里没有"证明测试管用""复查测试"这类检查:测试写得好不好靠写测试的约定,不靠再起一轮核对。
|
|
4
5
|
|
|
5
6
|
import type { Repo } from '../base/repo.ts'
|
|
6
7
|
import { joined } from './config.ts'
|
|
8
|
+
import { missingText } from './defaults.ts'
|
|
7
9
|
|
|
8
10
|
export function guide(label = '这个仓库'): string[] {
|
|
9
11
|
return [
|
|
10
12
|
`${label}用 testcraft 管测试的写法(.testcraft/):`,
|
|
11
13
|
'1. 什么时候写:分支上开发时不写新测试、不改已有的测试(需求还在试),做完告诉用户做完了、测试还没写。用户说要合并这个分支时才写:把这次定下来的规则用白话一条条列出来,交给一个子代理照 gatelight:write-tests 技能写新测试、改掉因为需求变了而不过的旧测试(没有起子代理的工具就自己写),跑通、提交,再合。例外,当场做:修已合进默认分支的规则的 bug,先写能重现它的测试再修。',
|
|
12
|
-
'2.
|
|
13
|
-
'3.
|
|
14
|
+
'2. 写哪些:小改动(文字、样式、注释、显示用的配置)不写。必跑层:钱、权限、状态与数据(什么状态下能做、数据会不会错)写在边界层(接口、命令这类从外面调用的地方,连真实的依赖)、测透,每个功能留一条主流程的端到端测试(有页面的走页面)。细节层:界面细节、样式、提示文字这类人容易漏看的,也值得写。修过的 bug 留一条能重现它的测试。在端到端测试里重复验证边界层守着的规则不写。',
|
|
15
|
+
'3. 已定的业务规则(合进默认分支的)以测试名为准。要改已定的业务规则(包括为此删测试、让测试不跑、改期望值),先用白话问用户,用户同意了再改;用户这次自己提出要改的不用再问。测试上别的调整(跟着代码改接法、换写法、挪位置、删重复的)自己定,不问用户,也不用写说明。.md 和注释都只给 AI 看,过时的说法 AI 会照着做,所以不留记录(方案、设计、说明):规则在测试里;为什么这样定、否掉的做法、用户拍板的、还没定的写进做这件事的提交说明;AI 很可能好心改掉、测试又守不住的地方,在那段代码旁边留一句为什么,别的注释不复述规则、不写代码在做什么(改到哪个文件,就把那个文件的注释收成这样)。方案只在分支上讨论时写,合并时写进提交说明、删掉文件。要改一段看不出为什么这样写的代码,先看那几行的提交说明(git log -L、git blame)。',
|
|
14
16
|
'4. 改动命中 .testcraft/checklist/ 里条目的触发条件时(testcraft checklist 列出,落地前的整组里也会说一遍),按条目查一遍,查出问题就改好。修 bug 时,写不成测试、又能从改动看出来的坑,加一条清单。',
|
|
15
17
|
]
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
/** 会话说明:接入了"测试写法"的仓库才说。 */
|
|
19
21
|
export function brief(label: string, repo?: Repo): string[] {
|
|
20
|
-
|
|
22
|
+
if (!repo || !joined(repo.root)) return []
|
|
23
|
+
const missing = missingText(repo.root)
|
|
24
|
+
return missing ? [...guide(label), missing] : guide(label)
|
|
21
25
|
}
|
package/src/keep/check.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
// codekeep check:整组里"代码规范"的那一段(见 full.ts
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// codekeep check:整组里"代码规范"的那一段(见 full.ts),也可以单独跑。依次做下面几件事:1、2 没过就停(配置不对、放松没同意,后面查了也不算);
|
|
2
|
+
// 3–5 互不依赖,一项没过也接着查,最后一起列出没过的。
|
|
3
|
+
// 1. 配置写得对不对(.codekeep/config.json),写到的文件在不在;现在这一版 eslint、prettier、tsconfig 生效的配置算不算得出来;
|
|
4
|
+
// 2. 和分叉点比,规范改松了哪些(loosen.ts、eslint.ts、prettier.ts、tsconfig.ts;biome 只看配置文件改没改),主题、组件层的样式改了哪些(styles.ts)。放松的条目交给底座的用户确认(askUser),
|
|
4
5
|
// 还没同意的这一段不过,照底座给的话让 agent 调确认工具。分叉点上还没有配置(首次接入)的不比、不问。
|
|
5
6
|
// 分叉点那一版算不出来的(工具在那一版上跑不起来),不当成没放松,列成"程序看不出"的一项,问。
|
|
6
|
-
// 3.
|
|
7
|
-
//
|
|
7
|
+
// 3. 配了 eslint 的,用项目自己的 eslint 程序接口跑一遍 lint,有错误就不过;警告不拦,落在这次改到的行上的列成提示;eslint 起不来时停(环境问题)。不跑项目写的 lint 命令:
|
|
8
|
+
// 命令行会读批量豁免文件(放在哪、叫什么、写成目录都行),从外面猜不全;程序接口不读,查出来的就是代码按规则过没过。
|
|
9
|
+
// 4. 依次跑 checks 里的每条命令(从仓库根目录用 sh 跑)。
|
|
10
|
+
// 5. secrets 开着时用 gitleaks 扫这次改动(secrets.ts)。
|
|
8
11
|
// 退出码用底座的:0 通过;1 没通过;2 配置、环境问题(配置写错、工具没装、算不出分叉点)。
|
|
9
12
|
|
|
10
13
|
import { spawnSync } from 'node:child_process'
|
|
@@ -13,16 +16,17 @@ import { join } from 'node:path'
|
|
|
13
16
|
import { askUser, type ConfirmItem } from '../base/confirm.ts'
|
|
14
17
|
import { EXIT } from '../base/exit.ts'
|
|
15
18
|
import { PROJECT_ENV } from '../base/execute.ts'
|
|
16
|
-
import { filesNow, forkPoint } from '../base/git.ts'
|
|
19
|
+
import { changesSince, diffLines, filesNow, forkPoint } from '../base/git.ts'
|
|
17
20
|
import { judgingFile } from '../base/judging.ts'
|
|
18
21
|
import { hash } from '../base/records.ts'
|
|
19
22
|
import type { Repo } from '../base/repo.ts'
|
|
20
23
|
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'
|
|
24
|
+
import { eslintItems, type FileRules, fileRules, isCode, lint, type Linted, loadEslint, stable, type Warning } from './eslint.ts'
|
|
22
25
|
import { withForkTree } from './forktree.ts'
|
|
23
|
-
import { BIOME_CONFIGS, biomeItem, configItems, GITLEAKS_CONFIG, GITLEAKS_IGNORE, gitleaksItems, removedItem
|
|
26
|
+
import { BIOME_CONFIGS, biomeItem, configItems, GITLEAKS_CONFIG, GITLEAKS_IGNORE, gitleaksItems, removedItem } from './loosen.ts'
|
|
24
27
|
import { checkedFiles, loadPrettier, prettierItem } from './prettier.ts'
|
|
25
28
|
import { scanSecrets } from './secrets.ts'
|
|
29
|
+
import { stylesItems } from './styles.ts'
|
|
26
30
|
import { findTsc, type TsSettings, tsconfigItem, tsSettings } from './tsconfig.ts'
|
|
27
31
|
|
|
28
32
|
/** 弹窗开头说这是什么的半句。 */
|
|
@@ -146,24 +150,32 @@ async function toolItems(root: string, fork: string, before: Config, now: Config
|
|
|
146
150
|
/** 和分叉点那一版比,放松了哪些。beforeText 是分叉点上的配置文件(没有就是首次接入,不比)。 */
|
|
147
151
|
async function loosened(root: string, fork: string, beforeText: string | undefined, now: Config, side: NowSide): Promise<Loosened> {
|
|
148
152
|
if (beforeText === undefined) return { items: [], reasons: [] }
|
|
149
|
-
|
|
153
|
+
// 分叉点那一版跳过现在不认的字段读:升级后去掉的字段不让整份比较落空,跳过的单列一项问
|
|
154
|
+
const parsed = parseConfig(beforeText, true)
|
|
155
|
+
const before = parsed.config
|
|
150
156
|
if (!before) {
|
|
151
157
|
return {
|
|
152
158
|
items: [{ before: stable(['config-unknown', beforeText]), after: stable(['config-unknown', now]), text: `分叉点上的 ${CONFIG_PATH} 写得不对、读不出来,程序看不出这次规范是改松还是改紧` }],
|
|
153
|
-
reasons: [`分叉点上的 ${CONFIG_PATH} 读不出来:${
|
|
159
|
+
reasons: [`分叉点上的 ${CONFIG_PATH} 读不出来:${parsed.problems.join(';')}`],
|
|
154
160
|
}
|
|
155
161
|
}
|
|
162
|
+
const skipped: ConfirmItem[] = parsed.skipped.length
|
|
163
|
+
? [
|
|
164
|
+
{
|
|
165
|
+
before: stable(['config-skipped', beforeText]),
|
|
166
|
+
after: stable(['config-skipped', now]),
|
|
167
|
+
text: [`分叉点上的 ${CONFIG_PATH} 有现在不认的字段,这几处程序看不出是改松还是改紧(别的照常比了):`, ...parsed.skipped].join('\n'),
|
|
168
|
+
},
|
|
169
|
+
]
|
|
170
|
+
: []
|
|
156
171
|
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
|
-
}
|
|
172
|
+
const items = [...skipped, ...configItems(before, now), ...tools.items]
|
|
162
173
|
if (before.secrets) items.push(...gitleaksItems(judgingFile(root, fork, GITLEAKS_IGNORE), judgingFile(root, fork, GITLEAKS_CONFIG)))
|
|
163
174
|
for (const path of BIOME_CONFIGS) {
|
|
164
175
|
const item = biomeItem(path, judgingFile(root, fork, path))
|
|
165
176
|
if (item) items.push(item)
|
|
166
177
|
}
|
|
178
|
+
if (before.styles && now.styles) items.push(...stylesItems(root, fork, before.styles, now.styles))
|
|
167
179
|
return { items, reasons: tools.reasons }
|
|
168
180
|
}
|
|
169
181
|
|
|
@@ -184,16 +196,42 @@ function askLines(items: ConfirmItem[], ask: string[], reasons: string[]): strin
|
|
|
184
196
|
]
|
|
185
197
|
}
|
|
186
198
|
|
|
187
|
-
/**
|
|
188
|
-
|
|
199
|
+
/** 提示最多列几条:接入分支上排版改了整个仓库,每一行都算改到的。 */
|
|
200
|
+
const HINTS_SHOWN = 40
|
|
201
|
+
|
|
202
|
+
/** 落在这次改到的行上的警告(和分叉点比,含没提交的)。旧代码上的不列:警告不拦,列出来只为写新代码时看一眼。 */
|
|
203
|
+
function touched(root: string, base: string, warnings: Warning[]): Warning[] {
|
|
204
|
+
if (!warnings.length) return []
|
|
205
|
+
const files = new Set(warnings.map(warning => warning.file))
|
|
206
|
+
const lines = new Map<string, Set<number>>()
|
|
207
|
+
for (const change of changesSince(root, base)) {
|
|
208
|
+
if (change.status === 'D' || !files.has(change.path)) continue
|
|
209
|
+
lines.set(change.path, new Set(diffLines(root, base, change).added.map(added => added.line)))
|
|
210
|
+
}
|
|
211
|
+
return warnings.filter(warning => lines.get(warning.file)?.has(warning.line))
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// 警告多是前端模板的文字规则:字数、标点、放说明的位置要看用途才判断得了,程序只能猜,所以不拦,交给 agent 看页面判断。
|
|
215
|
+
// 不叫 agent "改到没有警告":猜错的那些改了就是改业务文字(丢掉原因、办法、数据)。
|
|
216
|
+
function hintLines(hints: Warning[]): string[] {
|
|
217
|
+
return [
|
|
218
|
+
'',
|
|
219
|
+
`提示(不拦):这次改到的行上有 ${hints.length} 条警告${hints.length > HINTS_SHOWN ? `,列前 ${HINTS_SHOWN} 条` : ''}:`,
|
|
220
|
+
...hints.slice(0, HINTS_SHOWN).map(hint => ` ${hint.file}:${hint.line} ${hint.message}`),
|
|
221
|
+
'对着页面看一眼:是说明文字、或者确实写长了的,改;放的是数据、单位、问句、要照录的,不用改。拿不准的不改,也不要为了消掉提示删掉原因、办法和数据。',
|
|
222
|
+
]
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** 依次跑 checks 里的每条命令,一条没过也接着跑。返回没过的那几条和原因。 */
|
|
226
|
+
function runChecks(root: string, config: Config): Array<{ name: string; cause: string }> {
|
|
227
|
+
const failed: Array<{ name: string; cause: string }> = []
|
|
189
228
|
for (const check of config.checks) {
|
|
190
229
|
console.log(`== 规范检查:${check.name}(${check.command})`)
|
|
191
230
|
const r = spawnSync('/bin/sh', ['-c', check.command], { cwd: root, env: PROJECT_ENV, stdio: ['ignore', 'inherit', 'inherit'] })
|
|
192
231
|
if (r.status === 0) continue
|
|
193
|
-
|
|
194
|
-
return { name: check.name, cause }
|
|
232
|
+
failed.push({ name: check.name, cause: r.status !== null ? `退出码 ${r.status}` : r.signal ? `被信号 ${r.signal} 终止` : `没能启动:${r.error?.message ?? ''}` })
|
|
195
233
|
}
|
|
196
|
-
return
|
|
234
|
+
return failed
|
|
197
235
|
}
|
|
198
236
|
|
|
199
237
|
export async function check(repo: Repo): Promise<number> {
|
|
@@ -211,7 +249,7 @@ export async function check(repo: Repo): Promise<number> {
|
|
|
211
249
|
|
|
212
250
|
// 配置删掉了:分叉点上有能用的配置的,删掉算放松,用户同意过就退出 codekeep
|
|
213
251
|
if (judging.now === undefined) {
|
|
214
|
-
const before = judging.before === undefined ? undefined : parseConfig(judging.before).config
|
|
252
|
+
const before = judging.before === undefined ? undefined : parseConfig(judging.before, true).config
|
|
215
253
|
if (!before) {
|
|
216
254
|
const hasDir = existsSync(join(root, KEEP_DIR))
|
|
217
255
|
console.error(
|
|
@@ -254,43 +292,72 @@ export async function check(repo: Repo): Promise<number> {
|
|
|
254
292
|
}
|
|
255
293
|
const loosenedLine = found.items.length ? `规范改松 ${found.items.length} 项,用户都同意过` : '规范配置没有改松(和分叉点上的比)'
|
|
256
294
|
|
|
257
|
-
// 3
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
295
|
+
// 3–5 互不依赖:一项没过也接着查后面的,最后一起说。只说第一项的话,接入时 lint 要分很多批改,这期间格式、类型、密钥都没人查
|
|
296
|
+
const failures: string[] = []
|
|
297
|
+
let blocked = false
|
|
298
|
+
|
|
299
|
+
// 3. lint
|
|
300
|
+
let linted: Linted | undefined
|
|
301
|
+
if (config.eslint) {
|
|
302
|
+
console.log(`== lint:codekeep 用项目的 eslint 查(${config.eslint.config})`)
|
|
303
|
+
try {
|
|
304
|
+
const files = [...side.eslint!].filter(([, rules]) => rules).map(([file]) => file)
|
|
305
|
+
linted = await lint(await loadEslint(root, config.eslint.config), root, config.eslint.config, files)
|
|
306
|
+
} catch (error) {
|
|
307
|
+
console.error(`\ncodekeep check:lint 没跑成(${config.eslint.config}):${firstLine(error)}`)
|
|
308
|
+
return EXIT.usage
|
|
309
|
+
}
|
|
310
|
+
if (linted.errors > 0) {
|
|
311
|
+
console.log(linted.report)
|
|
312
|
+
failures.push(`lint 有 ${linted.errors} 个错误(上面列出;批量豁免文件在这里不起作用)`)
|
|
313
|
+
}
|
|
314
|
+
const hints = touched(root, fork.commit, linted.warnings)
|
|
315
|
+
if (hints.length) console.log(hintLines(hints).join('\n'))
|
|
264
316
|
}
|
|
265
317
|
|
|
266
|
-
// 4.
|
|
318
|
+
// 4. 规范检查
|
|
319
|
+
for (const failed of runChecks(root, config)) failures.push(`规范检查"${failed.name}"没过(${failed.cause})`)
|
|
320
|
+
|
|
321
|
+
// 5. 密钥
|
|
267
322
|
if (config.secrets) {
|
|
268
323
|
const secrets = scanSecrets(root, fork.commit)
|
|
269
324
|
if ('missing' in secrets) {
|
|
270
325
|
console.error(
|
|
271
|
-
`\
|
|
326
|
+
`\n找不到 gitleaks(${CONFIG_PATH} 开着 secrets,要用它扫密钥)。这是装在这台机器上的工具,先问用户;同意后装:brew install gitleaks(没有 Homebrew 的见 https://github.com/gitleaks/gitleaks#installing)。`,
|
|
272
327
|
)
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
if ('problem' in secrets) {
|
|
276
|
-
console.error(`\
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if ('leaks' in secrets) {
|
|
328
|
+
failures.push('密钥没扫:找不到 gitleaks')
|
|
329
|
+
blocked = true
|
|
330
|
+
} else if ('problem' in secrets) {
|
|
331
|
+
console.error(`\n密钥没扫成:${secrets.problem}`)
|
|
332
|
+
failures.push('密钥没扫成(上面说了原因)')
|
|
333
|
+
blocked = true
|
|
334
|
+
} else if ('leaks' in secrets) {
|
|
280
335
|
console.log(
|
|
281
336
|
[
|
|
282
337
|
'',
|
|
283
|
-
'
|
|
338
|
+
'gitleaks 在这次改动里扫出了疑似密钥(上面列出,已打码)。改法:',
|
|
284
339
|
' · 真的密钥、密码:从代码里拿掉,改成从环境变量或不进版本库的本地配置读,并告诉用户这个密钥可能已经泄露、要换掉。已经提交了的,要改写这个分支上的那几个提交才去得掉,先问用户。',
|
|
285
340
|
' · 测试用的假值:改成一眼看得出是假的写法(比如 test-password-not-real)。',
|
|
286
341
|
` · 确实不是密钥的:把那一条的 Fingerprint 加进 ${GITLEAKS_IGNORE}(算放松,整组会要用户确认)。`,
|
|
287
342
|
].join('\n'),
|
|
288
343
|
)
|
|
289
|
-
|
|
344
|
+
failures.push('gitleaks 扫出了疑似密钥(上面列出)')
|
|
290
345
|
}
|
|
291
346
|
}
|
|
292
347
|
|
|
293
|
-
|
|
348
|
+
if (failures.length > 0) {
|
|
349
|
+
console.log(
|
|
350
|
+
[
|
|
351
|
+
'',
|
|
352
|
+
`codekeep check:没通过,${failures.length} 项:`,
|
|
353
|
+
...failures.map(text => ` ✗ ${text}`),
|
|
354
|
+
'按上面的输出改;不要为了过关改松规范(关规则、降级、调高上限、加忽略),确实要改的先用白话跟用户讲清。',
|
|
355
|
+
].join('\n'),
|
|
356
|
+
)
|
|
357
|
+
return blocked ? EXIT.usage : EXIT.fail
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const names = [...(linted ? [`lint${linted.warnings.length ? `,${linted.warnings.length} 条警告不拦` : ''}`] : []), ...config.checks.map(item => item.name)].join('、')
|
|
294
361
|
console.log(`\ncodekeep check:通过。${loosenedLine};规范检查都过了(${names})${config.secrets ? ';这次改动里没扫出密钥' : ''}。`)
|
|
295
362
|
return EXIT.ok
|
|
296
363
|
}
|
package/src/keep/config.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
// "代码规范"在项目里的配置:.codekeep/config.json。没有 .codekeep/ 目录的仓库没接入,它不出声、不加检查。
|
|
2
|
-
// checks
|
|
3
|
-
// eslint
|
|
4
|
-
// suppressions 是批量豁免文件(eslint --suppress-all 生成的,不写就是仓库根目录的 eslint-suppressions.json)
|
|
2
|
+
// checks 规范检查的命令(类型检查、格式),直接写工具命令,不经过项目的脚本;整组里依次跑。lint 不写在这里
|
|
3
|
+
// eslint 项目的 eslint:config 是配置文件。codekeep 按它对每个代码文件算生效的规则来比,也用它直接跑 lint(check.ts)
|
|
5
4
|
// prettier 开着时比格式检查查哪些文件(prettier 在仓库根目录跑,查 git 收的文件)
|
|
6
5
|
// tsconfig 拿来比严格选项的 tsconfig 文件
|
|
7
6
|
// secrets 开着时整组里用 gitleaks 扫这次的提交和没提交的改动
|
|
7
|
+
// styles 前端只准写在这里的具体外观:theme 是主题文件,ui 是组件层目录(只看里面的样式文件);改了要用户同意(styles.ts)
|
|
8
8
|
// 这份配置是评判用的配置(base/judging.ts),走"用户同意后生效":改松的地方见 loosen.ts。
|
|
9
9
|
|
|
10
|
-
import { existsSync } from 'node:fs'
|
|
10
|
+
import { existsSync, statSync } from 'node:fs'
|
|
11
11
|
import { join, posix } from 'node:path'
|
|
12
12
|
import { fileNow, hasFile } from '../base/git.ts'
|
|
13
13
|
|
|
14
14
|
export const KEEP_DIR = '.codekeep'
|
|
15
15
|
export const CONFIG_PATH = `${KEEP_DIR}/config.json`
|
|
16
|
-
/** eslint 批量豁免文件的默认位置(eslint 自己的默认名字,相对仓库根目录)。 */
|
|
17
|
-
export const DEFAULT_SUPPRESSIONS = 'eslint-suppressions.json'
|
|
18
16
|
|
|
19
17
|
export interface Check {
|
|
20
18
|
name: string
|
|
@@ -23,7 +21,12 @@ export interface Check {
|
|
|
23
21
|
|
|
24
22
|
export interface EslintConfig {
|
|
25
23
|
config: string
|
|
26
|
-
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 主题文件和组件层目录:前端代码里具体的颜色、尺寸只准写在这两处。 */
|
|
27
|
+
export interface StylesConfig {
|
|
28
|
+
theme: string[]
|
|
29
|
+
ui: string[]
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
export interface Config {
|
|
@@ -32,11 +35,14 @@ export interface Config {
|
|
|
32
35
|
tsconfig: string[]
|
|
33
36
|
prettier: boolean
|
|
34
37
|
secrets: boolean
|
|
38
|
+
styles?: StylesConfig
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
export interface Parsed {
|
|
38
42
|
config?: Config
|
|
39
43
|
problems: string[]
|
|
44
|
+
/** 读的时候跳过的未知字段(只有 lenient 时有)。 */
|
|
45
|
+
skipped: string[]
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
/** 接入了:分叉点或工作区里有 .codekeep/。 */
|
|
@@ -44,8 +50,9 @@ export function joined(root: string, fork?: string): boolean {
|
|
|
44
50
|
return existsSync(join(root, KEEP_DIR)) || (fork !== undefined && hasFile(root, fork, KEEP_DIR))
|
|
45
51
|
}
|
|
46
52
|
|
|
47
|
-
const KEYS = ['checks', 'eslint', 'tsconfig', 'prettier', 'secrets']
|
|
48
|
-
const ESLINT_KEYS = ['config'
|
|
53
|
+
const KEYS = ['checks', 'eslint', 'tsconfig', 'prettier', 'secrets', 'styles']
|
|
54
|
+
const ESLINT_KEYS = ['config']
|
|
55
|
+
const STYLES_KEYS = ['theme', 'ui']
|
|
49
56
|
|
|
50
57
|
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
51
58
|
|
|
@@ -61,28 +68,31 @@ function relativePath(value: unknown): string | undefined {
|
|
|
61
68
|
/**
|
|
62
69
|
* 读配置的文字,只看写法(字段、类型、路径的写法),不看文件在不在:分叉点那一版也用它读。
|
|
63
70
|
* 工作区这一版另用 missingFiles 查写到的文件在不在。
|
|
71
|
+
* lenient:未知字段跳过、记在 skipped 里,不算写错。分叉点那一版用它:配置去掉过的字段(比如升级后不认的)不让整份读不出来。
|
|
64
72
|
*/
|
|
65
|
-
export function parseConfig(text: string): Parsed {
|
|
73
|
+
export function parseConfig(text: string, lenient = false): Parsed {
|
|
66
74
|
let raw: unknown
|
|
67
75
|
try {
|
|
68
76
|
raw = JSON.parse(text)
|
|
69
77
|
} catch (error) {
|
|
70
|
-
return { problems: [`${CONFIG_PATH} 不是合法的 JSON:${(error as Error).message}`] }
|
|
78
|
+
return { problems: [`${CONFIG_PATH} 不是合法的 JSON:${(error as Error).message}`], skipped: [] }
|
|
71
79
|
}
|
|
72
|
-
if (!isRecord(raw)) return { problems: [`${CONFIG_PATH} 应该是一个对象`] }
|
|
80
|
+
if (!isRecord(raw)) return { problems: [`${CONFIG_PATH} 应该是一个对象`], skipped: [] }
|
|
73
81
|
const problems: string[] = []
|
|
82
|
+
const skipped: string[] = []
|
|
83
|
+
const unknownField = (text: string): void => void (lenient ? skipped : problems).push(text)
|
|
74
84
|
const extra = Object.keys(raw).filter(key => !KEYS.includes(key))
|
|
75
|
-
if (extra.length)
|
|
85
|
+
if (extra.length) unknownField(`${CONFIG_PATH} 有未知字段 ${extra.map(key => `"${key}"`).join('、')}(认得的有 ${KEYS.join('、')})`)
|
|
76
86
|
|
|
77
87
|
const checks: Check[] = []
|
|
78
|
-
if (!Array.isArray(raw.checks) || raw.checks.length === 0) {
|
|
79
|
-
problems.push(`${CONFIG_PATH} 的 "checks"
|
|
88
|
+
if (!Array.isArray(raw.checks) || (raw.checks.length === 0 && raw.eslint === undefined)) {
|
|
89
|
+
problems.push(`${CONFIG_PATH} 的 "checks" 要写成数组,每条 { "name": 名字, "command": 命令 };没配 eslint 的不能是空的`)
|
|
80
90
|
} else {
|
|
81
91
|
raw.checks.forEach((item: unknown, index) => {
|
|
82
92
|
const where = `checks 的第 ${index + 1} 条`
|
|
83
93
|
if (!isRecord(item)) return void problems.push(`${where} 应该是 { "name": 名字, "command": 命令 }`)
|
|
84
94
|
const unknown = Object.keys(item).filter(key => key !== 'name' && key !== 'command')
|
|
85
|
-
if (unknown.length)
|
|
95
|
+
if (unknown.length) unknownField(`${where} 有未知字段 ${unknown.map(key => `"${key}"`).join('、')}`)
|
|
86
96
|
const { name, command } = item
|
|
87
97
|
if (typeof name !== 'string' || name.trim() === '' || /[\n\r]/.test(name)) return void problems.push(`${where} 要写名字(name,一行)`)
|
|
88
98
|
if (typeof command !== 'string' || command.trim() === '') return void problems.push(`${where}(${name})要写命令(command)`)
|
|
@@ -94,15 +104,13 @@ export function parseConfig(text: string): Parsed {
|
|
|
94
104
|
let eslint: EslintConfig | undefined
|
|
95
105
|
if (raw.eslint !== undefined) {
|
|
96
106
|
const value = raw.eslint
|
|
97
|
-
if (!isRecord(value)) problems.push(`${CONFIG_PATH} 的 "eslint" 应该是 { "config":
|
|
107
|
+
if (!isRecord(value)) problems.push(`${CONFIG_PATH} 的 "eslint" 应该是 { "config": 配置文件 }`)
|
|
98
108
|
else {
|
|
99
109
|
const unknown = Object.keys(value).filter(key => !ESLINT_KEYS.includes(key))
|
|
100
|
-
if (unknown.length)
|
|
110
|
+
if (unknown.length) unknownField(`${CONFIG_PATH} 的 "eslint" 有未知字段 ${unknown.map(key => `"${key}"`).join('、')}`)
|
|
101
111
|
const config = relativePath(value.config)
|
|
102
112
|
if (config === undefined) problems.push(`${CONFIG_PATH} 的 "eslint.config" 要写 eslint 配置文件(仓库里的相对路径,比如 "eslint.config.mjs")`)
|
|
103
|
-
|
|
104
|
-
if (suppressions === undefined) problems.push(`${CONFIG_PATH} 的 "eslint.suppressions" 要写批量豁免文件(仓库里的相对路径)`)
|
|
105
|
-
if (config !== undefined && suppressions !== undefined) eslint = { config, suppressions }
|
|
113
|
+
if (config !== undefined) eslint = { config }
|
|
106
114
|
}
|
|
107
115
|
}
|
|
108
116
|
|
|
@@ -111,8 +119,26 @@ export function parseConfig(text: string): Parsed {
|
|
|
111
119
|
if (raw[key] !== undefined && typeof raw[key] !== 'boolean') problems.push(`${CONFIG_PATH} 的 "${key}" 要写 true 或 false`)
|
|
112
120
|
}
|
|
113
121
|
|
|
114
|
-
|
|
115
|
-
|
|
122
|
+
const styles = raw.styles === undefined ? undefined : stylesOf(raw.styles, problems, unknownField)
|
|
123
|
+
|
|
124
|
+
if (problems.length || tsconfig === undefined) return { problems, skipped }
|
|
125
|
+
return {
|
|
126
|
+
config: { checks, ...(eslint ? { eslint } : {}), tsconfig, prettier: raw.prettier === true, secrets: raw.secrets === true, ...(styles ? { styles } : {}) },
|
|
127
|
+
problems,
|
|
128
|
+
skipped,
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** "styles":theme、ui 各是一组路径,至少写一组。 */
|
|
133
|
+
function stylesOf(value: unknown, problems: string[], unknownField: (text: string) => void): StylesConfig | undefined {
|
|
134
|
+
const shape = `{ "theme": [主题文件], "ui": [组件层目录] }`
|
|
135
|
+
if (!isRecord(value)) return void problems.push(`${CONFIG_PATH} 的 "styles" 应该是 ${shape}`)
|
|
136
|
+
const unknown = Object.keys(value).filter(key => !STYLES_KEYS.includes(key))
|
|
137
|
+
if (unknown.length) unknownField(`${CONFIG_PATH} 的 "styles" 有未知字段 ${unknown.map(key => `"${key}"`).join('、')}`)
|
|
138
|
+
if (value.theme === undefined && value.ui === undefined) return void problems.push(`${CONFIG_PATH} 的 "styles" 要写 theme、ui 里的至少一项:${shape}`)
|
|
139
|
+
const theme = value.theme === undefined ? [] : paths(value.theme, 'styles.theme', '主题文件', problems)
|
|
140
|
+
const ui = value.ui === undefined ? [] : paths(value.ui, 'styles.ui', '组件层目录', problems)
|
|
141
|
+
return theme && ui ? { theme, ui } : undefined
|
|
116
142
|
}
|
|
117
143
|
|
|
118
144
|
/** 一组仓库里的文件:非空数组,不重复。 */
|
|
@@ -132,8 +158,12 @@ function paths(value: unknown, name: string, what: string, problems: string[]):
|
|
|
132
158
|
return problems.length > found ? undefined : result
|
|
133
159
|
}
|
|
134
160
|
|
|
135
|
-
/**
|
|
161
|
+
/** 工作区这一版配置里写到、却不在的文件和目录。 */
|
|
136
162
|
export function missingFiles(root: string, config: Config): string[] {
|
|
137
|
-
const listed = [...(config.eslint ? [config.eslint.config] : []), ...config.tsconfig]
|
|
138
|
-
|
|
163
|
+
const listed = [...(config.eslint ? [config.eslint.config] : []), ...config.tsconfig, ...(config.styles?.theme ?? [])]
|
|
164
|
+
const dirs = config.styles?.ui ?? []
|
|
165
|
+
return [
|
|
166
|
+
...listed.filter(path => fileNow(root, path) === undefined).map(path => `${CONFIG_PATH} 里写的 ${path} 不在(要写仓库里现有的文件)`),
|
|
167
|
+
...dirs.filter(dir => !statSync(join(root, dir), { throwIfNoEntry: false })?.isDirectory()).map(dir => `${CONFIG_PATH} 里写的 ${dir} 不在(要写仓库里现有的目录)`),
|
|
168
|
+
]
|
|
139
169
|
}
|