gatelight 0.0.0-stage → 0.17.9
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 +9 -0
- package/.claude-plugin/plugin.json +11 -0
- package/.codex-plugin/plugin.json +18 -0
- package/.mcp.json +8 -0
- package/bin/modwall +8 -0
- package/bin/node.sh +14 -0
- package/bin/testcraft +8 -0
- package/bin/testpace +8 -0
- package/entry/command +44 -0
- package/hooks/antigravity.sh +24 -0
- package/hooks/codex.json +50 -0
- package/hooks/codex.sh +7 -0
- package/hooks/hooks.json +43 -0
- package/hooks/testpace.ts +25 -0
- package/hooks.json +16 -0
- package/mcp_config.json +8 -0
- package/package.json +21 -4
- package/setup.mjs +99 -0
- package/skills/merge/SKILL.md +42 -0
- package/skills/modular/SKILL.md +75 -0
- package/skills/setup/SKILL.md +101 -0
- package/skills/setup-modwall/SKILL.md +112 -0
- package/skills/setup-testcraft/SKILL.md +23 -0
- package/skills/setup-testcraft/checklist/dev-login.md +20 -0
- package/skills/write-tests/SKILL.md +153 -0
- package/src/base/agent.ts +100 -0
- package/src/base/baseline.ts +54 -0
- package/src/base/commandline.ts +40 -0
- package/src/base/commands.ts +94 -0
- package/src/base/confirm.ts +161 -0
- package/src/base/contract.ts +94 -0
- package/src/base/coverage.ts +46 -0
- package/src/base/entry.ts +90 -0
- package/src/base/excerpt.ts +46 -0
- package/src/base/execute.ts +70 -0
- package/src/base/exit.ts +6 -0
- package/src/base/format.ts +27 -0
- package/src/base/full.ts +160 -0
- package/src/base/gate.ts +251 -0
- package/src/base/git.ts +361 -0
- package/src/base/glob.ts +38 -0
- package/src/base/hook.ts +297 -0
- package/src/base/hookformat.ts +257 -0
- package/src/base/judging.ts +27 -0
- package/src/base/layermap.ts +69 -0
- package/src/base/module.ts +88 -0
- package/src/base/process-group.ts +101 -0
- package/src/base/records.ts +66 -0
- package/src/base/repo.ts +155 -0
- package/src/base/sessions.ts +55 -0
- package/src/base/shell.ts +176 -0
- package/src/base/stop.ts +100 -0
- package/src/base/store.ts +348 -0
- package/src/base/version.ts +24 -0
- package/src/cli.ts +186 -0
- package/src/craft/change.ts +24 -0
- package/src/craft/checklist.ts +157 -0
- package/src/craft/commands.ts +111 -0
- package/src/craft/config.ts +15 -0
- package/src/craft/full.ts +30 -0
- package/src/craft/index.ts +12 -0
- package/src/craft/session.ts +21 -0
- package/src/mcp.ts +79 -0
- package/src/modules.ts +10 -0
- package/src/modwall.ts +8 -0
- package/src/pace/commands.ts +281 -0
- package/src/pace/config.ts +103 -0
- package/src/pace/consent.ts +81 -0
- package/src/pace/focused.ts +208 -0
- package/src/pace/full.ts +38 -0
- package/src/pace/index.ts +16 -0
- package/src/pace/record.ts +84 -0
- package/src/pace/report.ts +335 -0
- package/src/pace/results.ts +243 -0
- package/src/pace/run/node-test-reporter.mjs +34 -0
- package/src/pace/run/node-test.ts +26 -0
- package/src/pace/run/playwright-reporter.mjs +48 -0
- package/src/pace/run/playwright.ts +34 -0
- package/src/pace/run/runner.ts +157 -0
- package/src/pace/run/targets.ts +130 -0
- package/src/pace/run/vitest-launcher.mjs +101 -0
- package/src/pace/run/vitest.ts +22 -0
- package/src/pace/session.ts +35 -0
- package/src/pace/testlist.ts +91 -0
- package/src/pace/tests.ts +22 -0
- package/src/pace/verify.ts +95 -0
- package/src/testcraft.ts +9 -0
- package/src/wall/check.ts +258 -0
- package/src/wall/commands.ts +279 -0
- package/src/wall/config.ts +394 -0
- package/src/wall/database.ts +136 -0
- package/src/wall/dbcheck.ts +389 -0
- package/src/wall/dbrules.ts +213 -0
- package/src/wall/edit.ts +64 -0
- package/src/wall/files.ts +35 -0
- package/src/wall/full.ts +30 -0
- package/src/wall/index.ts +15 -0
- package/src/wall/init.ts +149 -0
- package/src/wall/layermap.ts +124 -0
- package/src/wall/layout.ts +231 -0
- package/src/wall/loosen.ts +157 -0
- package/src/wall/miniprogram.ts +80 -0
- package/src/wall/project.ts +88 -0
- package/src/wall/rules.ts +400 -0
- package/src/wall/session.ts +198 -0
- package/src/wall/sql.ts +510 -0
- package/src/wall/store.ts +39 -0
- package/README.md +0 -3
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
// 规则:哪些关系算越界、怎么改。事实来自 layermap.ts(import、这次改动增删的关系)和 layout.ts(每个文件归谁)。
|
|
2
|
+
// R1 绕过入口 别的模块、装配层 import 了 B 入口以外的文件(只管 TS/JS 和 Python) 拦
|
|
3
|
+
// R2 循环 模块之间有回环 拦
|
|
4
|
+
// R3 方向反了 共享层引用模块或装配层;模块引用装配层;被管的代码引用 outside 或测试文件; 拦
|
|
5
|
+
// 一个应用引用另一个应用的代码;外面一层的共享层引用任何应用的代码
|
|
6
|
+
// R4 改已有行为 改了 B 已有导出函数的实现,A 在这次改动里新增了对它的调用 只提示
|
|
7
|
+
// R5 没有归属 代码文件不在模块、共享层、装配层、映射和 outside 里 拦
|
|
8
|
+
// R6 数据库越界 迁移文件那部分在 database.ts,库里的现状在 dbcheck.ts 拦
|
|
9
|
+
// R1、R2、R3、R5、R6 看项目当前的全部代码;R4 和"入口新增导出"看这次改动(和分叉点比)。
|
|
10
|
+
// 有几个应用时,R1、R2、R4 和"入口新增导出"只看同一个应用里的模块;应用之间的引用都是 R3。
|
|
11
|
+
// import 非代码文件(样式、图片、JSON):LayerMap 对不上,写相对路径、指向项目里存在的文件的,由 withAssets 算出目标,照 R1、R2、R3 判。
|
|
12
|
+
// 小程序的 .json(usingComponents)、.wxml、.wxss 里的引用由 miniprogram.ts 读出来,也照 R1、R2、R3 判:
|
|
13
|
+
// 组件的入口是模块目录下第一层的 components/ 里的组件;模板、样式不能经入口导出。
|
|
14
|
+
// R2 的环把数据库里模块之间的引用(database.ts 给的边)和代码的 import 一起算。
|
|
15
|
+
|
|
16
|
+
import { type App, type ExceptionRule, field, moduleId, type Scheme } from './config.ts'
|
|
17
|
+
import { posix } from 'node:path'
|
|
18
|
+
import { inEntryLanguage, isCode, isStylesheet, UNREAD_DIRECTORIES } from './files.ts'
|
|
19
|
+
import type { ChangeFacts, ProjectImports } from './layermap.ts'
|
|
20
|
+
import { label, type Layout, type Owner } from './layout.ts'
|
|
21
|
+
|
|
22
|
+
export type Rule = 'R1' | 'R2' | 'R3' | 'R5' | 'R6'
|
|
23
|
+
|
|
24
|
+
/** 拦截的规则,输出按这个顺序。 */
|
|
25
|
+
export const BLOCKING_RULES: readonly Rule[] = ['R1', 'R2', 'R3', 'R5', 'R6']
|
|
26
|
+
|
|
27
|
+
export const RULE_NAMES: Record<Rule | 'R4', string> = {
|
|
28
|
+
R1: '绕过入口',
|
|
29
|
+
R2: '模块之间成环',
|
|
30
|
+
R3: '依赖方向反了',
|
|
31
|
+
R4: '为新功能改了别人的已有行为',
|
|
32
|
+
R5: '文件没有归属',
|
|
33
|
+
R6: '数据库越界',
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface Violation {
|
|
37
|
+
rule: Rule
|
|
38
|
+
/** 认这一处的键(不含行号,行号会挪):日志里比上一次的越界这次改正了没有。 */
|
|
39
|
+
key: string
|
|
40
|
+
/** 哪里越界:第一行是概括,后面是细节。 */
|
|
41
|
+
where: string[]
|
|
42
|
+
fix: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Hint {
|
|
46
|
+
where: string
|
|
47
|
+
fix?: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const site = (fact: { from: string; line: number }): string => `${fact.from}:${fact.line}`
|
|
51
|
+
|
|
52
|
+
function allowed(scheme: Scheme, rule: ExceptionRule, from: string, to: string): boolean {
|
|
53
|
+
return scheme.allow.some(entry => entry.rule === rule && entry.from === from && entry.to === to)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const MOVE = '把那段代码搬到该在的地方:只有一个模块用的搬回那个模块,真正通用的搬进共享层'
|
|
57
|
+
|
|
58
|
+
/** 属于某个应用的代码(模块、共享层、装配层):给出应用名,单应用时是 undefined。 */
|
|
59
|
+
const appCode = (owner: Owner): { app: string | undefined } | undefined =>
|
|
60
|
+
owner.kind === 'module' || owner.kind === 'shared' || owner.kind === 'app' ? { app: owner.app } : undefined
|
|
61
|
+
|
|
62
|
+
/** R2 的一条边:同一个应用里模块到模块的一处引用(代码的 import,或数据库里经入口的引用)。 */
|
|
63
|
+
export interface Edge {
|
|
64
|
+
from: string
|
|
65
|
+
to: string
|
|
66
|
+
/** 文件:行 */
|
|
67
|
+
site: string
|
|
68
|
+
/** 引用了什么,比如 import 了 src/b/index.ts。 */
|
|
69
|
+
what: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* import 非代码文件:LayerMap 记成 SYMBOL_NOT_RESOLVED。写的是相对路径、又指向项目里存在的非代码文件的,算出目标,
|
|
74
|
+
* 从"没对上"挪进 imports,照 R1、R2、R3 判。路径后面的 ?query、#hash(打包工具的写法)去掉再找。
|
|
75
|
+
*/
|
|
76
|
+
export function withAssets(facts: ProjectImports, files: readonly string[]): ProjectImports {
|
|
77
|
+
const present = new Set(files)
|
|
78
|
+
const imports = [...facts.imports]
|
|
79
|
+
const unresolved = facts.unresolved.filter(fact => {
|
|
80
|
+
if (fact.reason !== 'SYMBOL_NOT_RESOLVED' || !/^\.\.?\//.test(fact.specifier)) return true
|
|
81
|
+
const to = posix.normalize(posix.join(posix.dirname(fact.from), fact.specifier.replace(/[?#].*$/, '')))
|
|
82
|
+
if (to.startsWith('../') || isCode(to) || !present.has(to)) return true
|
|
83
|
+
imports.push({ from: fact.from, line: fact.line, to, specifier: fact.specifier })
|
|
84
|
+
return false
|
|
85
|
+
})
|
|
86
|
+
return { imports, unresolved }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** R1、R2、R3:看项目现有的全部 import。dbEdges:数据库里模块之间的引用,和 import 一起算环。 */
|
|
90
|
+
export function importViolations(layout: Layout, facts: ProjectImports, dbEdges: readonly Edge[] = []): Violation[] {
|
|
91
|
+
const { scheme } = layout
|
|
92
|
+
const r1: Violation[] = []
|
|
93
|
+
const r3: Violation[] = []
|
|
94
|
+
const edges: Edge[] = []
|
|
95
|
+
for (const fact of facts.imports) {
|
|
96
|
+
const from = layout.owner(fact.from)
|
|
97
|
+
const to = layout.owner(fact.to)
|
|
98
|
+
// outside 和没有归属的文件不查(后者是 R5);测试文件不在事实里
|
|
99
|
+
if (from.kind === 'outside' || from.kind === 'none' || from.kind === 'test') continue
|
|
100
|
+
|
|
101
|
+
// R3:方向反了
|
|
102
|
+
let r3Text: string | undefined
|
|
103
|
+
let r3Fix = `${MOVE}。`
|
|
104
|
+
if (to.kind === 'outside') {
|
|
105
|
+
r3Text = 'outside 里的文件(不归 modwall 管的路径)'
|
|
106
|
+
r3Fix = '正式代码要用的东西不放在 outside 里:搬进共享层,或者搬进用它的那个模块。'
|
|
107
|
+
} else if (to.kind === 'test') {
|
|
108
|
+
r3Text = '测试文件'
|
|
109
|
+
r3Fix = '正式代码要用的东西不放在测试目录里:搬进共享层,或者搬进用它的那个模块。'
|
|
110
|
+
} else if (appCode(from) && appCode(to) && appCode(from)?.app !== appCode(to)?.app) {
|
|
111
|
+
r3Text = label(to)
|
|
112
|
+
r3Fix = '应用之间不能引用代码。要共用的代码放进外面一层的 shared(几个应用共用的共享层);应用之间通过接口(HTTP、生成的契约副本)打交道。'
|
|
113
|
+
} else if (from.kind === 'common' && appCode(to)) {
|
|
114
|
+
r3Text = label(to)
|
|
115
|
+
r3Fix = `外面一层的共享层是几个应用共用的,不能引用任何应用的代码。把那段代码搬到该在的地方:只有一个应用用的搬回那个应用,真正通用的留在外面一层的共享层;要用应用里的东西,通过参数传进来。`
|
|
116
|
+
} else if (from.kind === 'shared' && (to.kind === 'module' || to.kind === 'app')) {
|
|
117
|
+
r3Text = label(to)
|
|
118
|
+
r3Fix = `共享层只放真正通用的东西,不能引用模块和装配层。${MOVE};共享层要用的东西,通过参数传进来。`
|
|
119
|
+
} else if (from.kind === 'module' && to.kind === 'app') {
|
|
120
|
+
r3Text = label(to)
|
|
121
|
+
r3Fix = `模块不能引用装配层。${MOVE};要装配层组装好的东西,由装配层通过参数传进来。`
|
|
122
|
+
}
|
|
123
|
+
if (r3Text !== undefined && to.kind === 'app' && to.root && fact.to.endsWith('/__init__.py')) {
|
|
124
|
+
// 模块根目录的 __init__.py 归了装配层:从包名引模块的两种写法会被记成引用了它
|
|
125
|
+
const pkg = to.root.split('/').join('.')
|
|
126
|
+
r3Fix += `如果引的其实是别的模块:\`from ${pkg} import 模块名\` 和 \`from .. import 模块名\` 会被记成同时引用了 ${fact.to},写成 \`from ${pkg}.模块名 import …\` 或 \`from ..模块名 import …\`。`
|
|
127
|
+
}
|
|
128
|
+
if (r3Text !== undefined) {
|
|
129
|
+
if (!allowed(scheme, 'R3', fact.from, fact.to)) {
|
|
130
|
+
r3.push({
|
|
131
|
+
rule: 'R3',
|
|
132
|
+
key: `R3 ${fact.from} → ${fact.to}`,
|
|
133
|
+
where: [`${site(fact)}(${label(from)})import 了 ${r3Text} ${fact.to}`],
|
|
134
|
+
fix: r3Fix,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
continue
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 走到这里,引用的是同一个应用里的代码(或外面一层的共享层)
|
|
141
|
+
if (to.kind !== 'module') continue
|
|
142
|
+
const target = moduleId(to.app, to.name)
|
|
143
|
+
const fromModule = from.kind === 'module' ? moduleId(from.app, from.name) : undefined
|
|
144
|
+
if (fromModule === target) continue
|
|
145
|
+
|
|
146
|
+
// R1:别的模块、装配层只能走 B 的入口
|
|
147
|
+
const kind = entryKind(fact)
|
|
148
|
+
const module = layout.module(target)
|
|
149
|
+
const dir = module?.dir ?? target
|
|
150
|
+
const viaEntry = kind === 'component' ? isPublicComponent(dir, fact.to) : (kind === 'code' || kind === 'asset') && layout.isEntry(fact.to, target)
|
|
151
|
+
if ((from.kind === 'module' || from.kind === 'app') && kind !== undefined && !viaEntry && !allowed(scheme, 'R1', fact.from, fact.to)) {
|
|
152
|
+
const entries = module?.entries ?? []
|
|
153
|
+
const components = `${target} 对外的组件(${dir}/components/ 下第一层的组件)`
|
|
154
|
+
const fix =
|
|
155
|
+
kind === 'style'
|
|
156
|
+
? `样式不能经入口导出:几个模块都用的样式,搬进共享层;只该 ${target} 用的,改成用 ${target} 入口导出的组件。`
|
|
157
|
+
: kind === 'template'
|
|
158
|
+
? `小程序的模板、样式不能经入口导出:几个模块都用的,搬进共享层;只该 ${target} 用的,改成用 ${components}。`
|
|
159
|
+
: kind === 'component'
|
|
160
|
+
? `改成用 ${components};这个组件该给别的模块用,就挪进 ${dir}/components/(作为单独的一小步);几个模块都用的通用组件搬进共享层。`
|
|
161
|
+
: entries.length
|
|
162
|
+
? `改成从 ${target} 的入口 ${entries.join(' 或 ')} 引用;入口里没有,就在入口新增导出(作为单独的一小步)。`
|
|
163
|
+
: `${target} 还没有入口:在 ${dir}/ 下新建入口文件(${(layout.app(to.app)?.entry ?? []).join('、')} 之一),导出要用的东西,再从入口引用。`
|
|
164
|
+
r1.push({
|
|
165
|
+
rule: 'R1',
|
|
166
|
+
key: `R1 ${fact.from} → ${fact.to}`,
|
|
167
|
+
where: [`${site(fact)}(${label(from)})${kind === 'component' ? '用了' : 'import 了'} ${target} 模块的内部文件 ${fact.to}`],
|
|
168
|
+
fix,
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// R2 的边:同一个应用里模块到模块(应用之间的引用是 R3,不进来),所以环按应用各算
|
|
173
|
+
if (fromModule !== undefined && !allowed(scheme, 'R2', fact.from, fact.to)) {
|
|
174
|
+
edges.push({ from: fromModule, to: target, site: site(fact), what: `import 了 ${fact.to}` })
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return [...r1, ...cycles([...edges, ...dbEdges]), ...r3]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
type EntryKind = 'code' | 'asset' | 'style' | 'component' | 'template'
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* R1 怎么判这条引用:TS/JS、Python 引用代码(code)、非代码文件(asset,样式是 style)按入口文件判;
|
|
184
|
+
* 小程序 .json 引用组件(component)按 components/ 判;.wxml、.wxss 引用模板、样式(template)不能经入口导出。
|
|
185
|
+
* Go、Java 第一版不管(undefined)。
|
|
186
|
+
*/
|
|
187
|
+
function entryKind(fact: { from: string; to: string }): EntryKind | undefined {
|
|
188
|
+
if (fact.from.endsWith('.json')) return 'component'
|
|
189
|
+
if (fact.from.endsWith('.wxml') || fact.from.endsWith('.wxss')) return 'template'
|
|
190
|
+
if (!inEntryLanguage(fact.from)) return undefined
|
|
191
|
+
if (inEntryLanguage(fact.to)) return 'code'
|
|
192
|
+
if (isCode(fact.to)) return undefined
|
|
193
|
+
return isStylesheet(fact.to) ? 'style' : 'asset'
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** 小程序组件在模块目录下第一层的 components/ 里(components/<组件>/<文件>):模块对外的组件。 */
|
|
197
|
+
function isPublicComponent(dir: string, path: string): boolean {
|
|
198
|
+
const prefix = `${dir}/components/`
|
|
199
|
+
return path.startsWith(prefix) && path.slice(prefix.length).split('/').length === 2
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** R2:模块之间的回环。每个强连通的模块组报一次,给出一条环和环上每一步的引用。 */
|
|
203
|
+
function cycles(list: readonly Edge[]): Violation[] {
|
|
204
|
+
const edges = new Map<string, Map<string, Edge[]>>()
|
|
205
|
+
for (const edge of list) {
|
|
206
|
+
const out = edges.get(edge.from) ?? new Map<string, Edge[]>()
|
|
207
|
+
out.set(edge.to, [...(out.get(edge.to) ?? []), edge])
|
|
208
|
+
edges.set(edge.from, out)
|
|
209
|
+
}
|
|
210
|
+
const nodes = [...new Set([...edges.keys(), ...[...edges.values()].flatMap(out => [...out.keys()])])].sort()
|
|
211
|
+
// Tarjan 求强连通分量
|
|
212
|
+
let counter = 0
|
|
213
|
+
const index = new Map<string, number>()
|
|
214
|
+
const low = new Map<string, number>()
|
|
215
|
+
const stack: string[] = []
|
|
216
|
+
const onStack = new Set<string>()
|
|
217
|
+
const groups: string[][] = []
|
|
218
|
+
const visit = (node: string): void => {
|
|
219
|
+
index.set(node, counter)
|
|
220
|
+
low.set(node, counter)
|
|
221
|
+
counter++
|
|
222
|
+
stack.push(node)
|
|
223
|
+
onStack.add(node)
|
|
224
|
+
for (const next of [...(edges.get(node)?.keys() ?? [])].sort()) {
|
|
225
|
+
if (!index.has(next)) {
|
|
226
|
+
visit(next)
|
|
227
|
+
low.set(node, Math.min(low.get(node) as number, low.get(next) as number))
|
|
228
|
+
} else if (onStack.has(next)) {
|
|
229
|
+
low.set(node, Math.min(low.get(node) as number, index.get(next) as number))
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (low.get(node) === index.get(node)) {
|
|
233
|
+
const group: string[] = []
|
|
234
|
+
let member: string
|
|
235
|
+
do {
|
|
236
|
+
member = stack.pop() as string
|
|
237
|
+
onStack.delete(member)
|
|
238
|
+
group.push(member)
|
|
239
|
+
} while (member !== node)
|
|
240
|
+
if (group.length > 1) groups.push(group.sort())
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
for (const node of nodes) if (!index.has(node)) visit(node)
|
|
244
|
+
|
|
245
|
+
return groups
|
|
246
|
+
.sort((a, b) => (a[0] as string).localeCompare(b[0] as string))
|
|
247
|
+
.map(group => {
|
|
248
|
+
const members = new Set(group)
|
|
249
|
+
const start = group[0] as string
|
|
250
|
+
// 从组里第一个模块出发,找最短的一条回到它的路
|
|
251
|
+
const previous = new Map<string, string>()
|
|
252
|
+
const queue = [start]
|
|
253
|
+
let end: string | undefined
|
|
254
|
+
while (queue.length && end === undefined) {
|
|
255
|
+
const node = queue.shift() as string
|
|
256
|
+
for (const next of [...(edges.get(node)?.keys() ?? [])].sort()) {
|
|
257
|
+
if (!members.has(next)) continue
|
|
258
|
+
if (next === start) {
|
|
259
|
+
end = node
|
|
260
|
+
break
|
|
261
|
+
}
|
|
262
|
+
if (!previous.has(next)) {
|
|
263
|
+
previous.set(next, node)
|
|
264
|
+
queue.push(next)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const path = [start]
|
|
269
|
+
for (let node = end as string; node !== start; node = previous.get(node) as string) path.splice(1, 0, node)
|
|
270
|
+
path.push(start)
|
|
271
|
+
const where = [`${path.join(' → ')}${group.length > path.length - 1 ? `(成环的模块还有:${group.filter(name => !path.includes(name)).join('、')})` : ''}`]
|
|
272
|
+
for (let i = 0; i + 1 < path.length; i++) {
|
|
273
|
+
const facts = edges.get(path[i] as string)?.get(path[i + 1] as string) ?? []
|
|
274
|
+
for (const fact of facts.slice(0, 3)) where.push(`${path[i]} → ${path[i + 1]}:${fact.site} ${fact.what}`)
|
|
275
|
+
if (facts.length > 3) where.push(`${path[i]} → ${path[i + 1]}:还有 ${facts.length - 3} 处`)
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
rule: 'R2' as const,
|
|
279
|
+
key: `R2 ${group.join(' ')}`,
|
|
280
|
+
where,
|
|
281
|
+
fix: '把共用的东西下沉到共享层,或者通过参数传进来;两个模块确实分不开,就合成一个。',
|
|
282
|
+
}
|
|
283
|
+
})
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** 一个应用里代码文件能放的地方。 */
|
|
287
|
+
function places(app: App): string {
|
|
288
|
+
const items = [
|
|
289
|
+
...app.roots.map(root => `某个模块(${root}/<模块名>/)`),
|
|
290
|
+
...(app.shared.length ? [`共享层(${app.shared.join('、')})`] : []),
|
|
291
|
+
...(app.app.length ? [`装配层(${app.app.join('、')})`] : []),
|
|
292
|
+
]
|
|
293
|
+
return items.length ? items.join('、') : '某个模块、共享层或装配层'
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* R5 的改法:代码文件该放到哪里。rootFile:这个文件直接放在这个应用的模块根目录下。
|
|
298
|
+
* 有几个应用时按应用说:知道在哪个应用里的只说那个应用,不知道的列出每个应用能放的地方和外面一层的共享层。
|
|
299
|
+
*/
|
|
300
|
+
function homeFix(scheme: Scheme, rootFile?: { path: string; app: App }): string {
|
|
301
|
+
const keep = rootFile
|
|
302
|
+
? `要保留在模块根目录下的(比如 Python 包的 __init__.py),在 ${field(rootFile.app, 'app')} 里写到这个文件("${rootFile.path}",不用通配),它就归装配层。`
|
|
303
|
+
: ''
|
|
304
|
+
if (!scheme.grouped) return `放进${places(scheme.apps[0] as App)}。${keep}`
|
|
305
|
+
if (rootFile) return `放进 ${rootFile.app.name} 应用的${places(rootFile.app)}。${keep}`
|
|
306
|
+
const each = scheme.apps.map(app => `${app.name} 应用的${places(app)}`).join(';')
|
|
307
|
+
const common = scheme.shared.length ? `外面一层的共享层(${scheme.shared.join('、')})` : '外面一层的共享层(shared,现在没写)'
|
|
308
|
+
return `放进它所在应用的模块、共享层或装配层:${each}。几个应用共用的放进${common}。`
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** R5 怎么报、怎么改:modwall check 和改文件之前的钩子共用。 */
|
|
312
|
+
export function unownedText(scheme: Scheme, path: string, owner: Extract<Owner, { kind: 'none' }>): { where: string; fix: string } {
|
|
313
|
+
if (owner.unread) {
|
|
314
|
+
const where = `${path} 在 LayerMap 不读的目录 ${owner.unread} 里,modwall 查不到它引用了谁${owner.within ? `(按路径它在${owner.within}里)` : ''},没有归属`
|
|
315
|
+
const fix = owner.within
|
|
316
|
+
? `换个目录名,把它搬出 ${owner.unread}/(LayerMap 不读的目录名有 ${UNREAD_DIRECTORIES.join('、')},不分大小写);业务上叫这类名字的模块(比如供应商 vendor)也要换个目录名。`
|
|
317
|
+
: `第三方或工具的代码,在 outside 里写上 "${owner.unread}/**"(放宽规则,要用户同意);项目自己的代码${homeFix(scheme)}`
|
|
318
|
+
return { where, fix }
|
|
319
|
+
}
|
|
320
|
+
const app = owner.root === undefined ? undefined : scheme.apps.find(item => item.name === owner.app)
|
|
321
|
+
if (owner.root !== undefined && app) {
|
|
322
|
+
const at = app.name === undefined ? '' : ` ${app.name} 应用的`
|
|
323
|
+
return { where: `${path} 直接放在${at}模块根目录 ${owner.root} 下,没有归属(模块是根目录下的子目录)`, fix: homeFix(scheme, { path, app }) }
|
|
324
|
+
}
|
|
325
|
+
return { where: `${path} 没有归属`, fix: homeFix(scheme) }
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** R5:代码文件没有归属。只看路径。 */
|
|
329
|
+
export function unownedFiles(layout: Layout, files: readonly string[]): Violation[] {
|
|
330
|
+
return files
|
|
331
|
+
.filter(path => isCode(path))
|
|
332
|
+
.map(path => ({ path, owner: layout.owner(path) }))
|
|
333
|
+
.filter((item): item is { path: string; owner: Extract<Owner, { kind: 'none' }> } => item.owner.kind === 'none')
|
|
334
|
+
.sort((a, b) => a.path.localeCompare(b.path))
|
|
335
|
+
.map(({ path, owner }) => {
|
|
336
|
+
const { where, fix } = unownedText(layout.scheme, path, owner)
|
|
337
|
+
return { rule: 'R5' as const, key: `R5 ${path}`, where: [where], fix }
|
|
338
|
+
})
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** R4:这次改了 B 模块已有的导出函数的实现,同时 A 模块在这次改动里新增了对它的调用。只提示。 */
|
|
342
|
+
export function behaviorHints(layout: Layout, changes: ChangeFacts): Hint[] {
|
|
343
|
+
const hints: Hint[] = []
|
|
344
|
+
for (const declaration of changes.declarations) {
|
|
345
|
+
if (!declaration.exported) continue
|
|
346
|
+
const owner = layout.owner(declaration.path)
|
|
347
|
+
if (owner.kind !== 'module') continue
|
|
348
|
+
const callee = moduleId(owner.app, owner.name)
|
|
349
|
+
for (const call of changes.calls) {
|
|
350
|
+
if (call.to !== declaration.path || call.declaration !== declaration.declaration) continue
|
|
351
|
+
// 只看同一个应用里别的模块:应用之间的调用另由 R3 拦
|
|
352
|
+
const caller = layout.owner(call.from)
|
|
353
|
+
if (caller.kind !== 'module' || caller.app !== owner.app || caller.name === owner.name) continue
|
|
354
|
+
hints.push({
|
|
355
|
+
where: `${callee} 模块的 ${declaration.declaration}(${declaration.path}:${declaration.line})这次改了实现,${moduleId(caller.app, caller.name)} 模块在 ${site(call)} 新增了对它的调用`,
|
|
356
|
+
fix: `为新功能就在 ${callee} 新增一个函数,不要给旧函数加分支;是正常的重构(比如改了签名、同时更新调用处)就不用管。`,
|
|
357
|
+
})
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return hints
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** 这次改动给哪些模块的入口新增了引用(转导出也算 import):不算违规,留作试点数据。 */
|
|
364
|
+
export function entryExports(layout: Layout, changes: ChangeFacts): Hint[] {
|
|
365
|
+
return changes.imports
|
|
366
|
+
.filter(edge => edge.change === 'ADDED')
|
|
367
|
+
.flatMap(edge => {
|
|
368
|
+
const from = layout.owner(edge.from)
|
|
369
|
+
const to = layout.owner(edge.to)
|
|
370
|
+
if (from.kind !== 'module' || to.kind !== 'module' || from.app !== to.app || from.name !== to.name) return []
|
|
371
|
+
const id = moduleId(from.app, from.name)
|
|
372
|
+
if (!layout.isEntry(edge.from, id)) return []
|
|
373
|
+
return [{ where: `${id} 的入口 ${edge.from}:${edge.line} 新增引用 ${edge.to}` }]
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const REASONS: Record<string, string> = {
|
|
378
|
+
SYMBOL_NOT_RESOLVED: '路径或别名没对上文件,也可能是样式文件或没装的包',
|
|
379
|
+
SOURCE_DECLARATION_UNMAPPED: '是项目里的文件,但地图里没有它的声明',
|
|
380
|
+
SYNTAX_ONLY: '这个文件只做了语法解析,它的 import 一条都没解析',
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** LayerMap 标明没看全的地方:照列,不拦。可能是项目自己的 import 里,写相对路径的排在前面。 */
|
|
384
|
+
export function uncheckedItems(layout: Layout, facts: ProjectImports | undefined, changes: ChangeFacts | undefined): string[] {
|
|
385
|
+
const items: string[] = []
|
|
386
|
+
if (facts) {
|
|
387
|
+
const own = facts.unresolved
|
|
388
|
+
.filter(fact => fact.possiblyOwn && layout.owner(fact.from).kind !== 'outside')
|
|
389
|
+
.sort((a, b) => Number(!a.specifier.startsWith('.')) - Number(!b.specifier.startsWith('.')))
|
|
390
|
+
for (const fact of own) items.push(`${site(fact)} 的 import "${fact.specifier}":${REASONS[fact.reason] ?? fact.reason}`)
|
|
391
|
+
}
|
|
392
|
+
if (changes) {
|
|
393
|
+
for (const text of changes.incomplete) items.push(`检查没做完:${text}`)
|
|
394
|
+
// 只列代码文件:配置、文档这些 modwall 本来就不查,不存在"没检查到"
|
|
395
|
+
for (const file of changes.unmapped) if (isCode(file.path)) items.push(`LayerMap 不跟的改动文件:${file.path}(${file.kind})`)
|
|
396
|
+
if (changes.truncated) items.push('这次改动的检查结果被截断了,有的关系没列出来')
|
|
397
|
+
if (changes.edgesTruncated) items.push('这次改动的声明超过 LayerMap 跟踪的上限(200 个),R4 的提示可能不全')
|
|
398
|
+
}
|
|
399
|
+
return items
|
|
400
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// "模块边界"说给 agent 的话,经底座的钩子送出去(base/module.ts 的 brief、onSession、unfinished、beforeEdit):
|
|
2
|
+
// 会话说明 模块列表、各模块的职责和写法约定(用库的应用加上数据库的约定);记下这时内容的样子(会话起点,底座存,见 base/baseline.ts)
|
|
3
|
+
// 改文件之前 只做看路径、看要写的文字就能判断的事:目标是代码文件又没有归属(R5),新建和修改都拒绝,附上改法;
|
|
4
|
+
// 要写进去的新文字里用相对路径直接引用了别的模块的内部文件(R1 的一部分,见 edit.ts),也拒绝
|
|
5
|
+
// 停止门 不跑检查(钩子进程里 layermap 首次建图可能超过时限):这个会话改过代码或配置(和起点比,没有起点的不算)、
|
|
6
|
+
// 当前内容又没有 modwall check 的通过记录时拦一次,让 agent 用 Bash 跑 modwall check。
|
|
7
|
+
// 同一份内容只拦一次(记号里带着内容的样子):改了以后还没有通过记录,再拦。
|
|
8
|
+
// 这道门是有意留在开发中的:"开发轻量、合并兜底"说的是测试;架构越界发现得晚返工大,检查又只要几秒,在分支上就查。
|
|
9
|
+
|
|
10
|
+
import { readdirSync, readFileSync } from 'node:fs'
|
|
11
|
+
import { join } from 'node:path'
|
|
12
|
+
import { SessionBaselines } from '../base/baseline.ts'
|
|
13
|
+
import { CONFIRM_TOOL } from '../base/confirm.ts'
|
|
14
|
+
import { fileNow, isIgnored } from '../base/git.ts'
|
|
15
|
+
import type { Repo } from '../base/repo.ts'
|
|
16
|
+
import { type App, CONFIG_PATH, joined, readConfig, type Scheme, schemeOf } from './config.ts'
|
|
17
|
+
import { entryBypass } from './edit.ts'
|
|
18
|
+
import { isCode, unreadDirectory } from './files.ts'
|
|
19
|
+
import { type Layout, type Module, ownerIn } from './layout.ts'
|
|
20
|
+
import { CONTENT_FORMAT, contentKey, layoutOf, openProject } from './project.ts'
|
|
21
|
+
import { unownedText } from './rules.ts'
|
|
22
|
+
import { MODULE, openStore } from './store.ts'
|
|
23
|
+
|
|
24
|
+
/** 模块超过这个数,只列模块名和 MODULE.md 的头两句。 */
|
|
25
|
+
const MANY_MODULES = 12
|
|
26
|
+
|
|
27
|
+
/** 会话说明:接入了"模块边界"的仓库才说。 */
|
|
28
|
+
export function brief(label: string, repo?: Repo): string[] {
|
|
29
|
+
if (!repo || !joined(repo.root)) return []
|
|
30
|
+
const opened = openProject(repo.root)
|
|
31
|
+
if ('notJoined' in opened) return []
|
|
32
|
+
const config = opened.project.now.config
|
|
33
|
+
return guide(repo.root, config ? layoutOf(opened.project, config) : undefined, label).split('\n')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 这个仓库里各会话的起点:那时内容的样子(底座存,按会话和工作区各一份)。 */
|
|
37
|
+
function baselines(repo: Repo): SessionBaselines<string> {
|
|
38
|
+
return new SessionBaselines(repo, MODULE, CONTENT_FORMAT)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 会话碰到这个工作区时:记下这时内容的样子(只记第一次)。没接入时不记。 */
|
|
42
|
+
export function onSession(repo: Repo, sessionId: string): void {
|
|
43
|
+
baselines(repo).note(sessionId, () => {
|
|
44
|
+
const opened = openProject(repo.root)
|
|
45
|
+
return 'notJoined' in opened ? undefined : contentKey(opened.project)
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 停止门:这个会话在这个仓库里改了代码或配置(和起点比;没有起点的不算这个会话改的),当前内容还没有 modwall check 的通过记录。 */
|
|
50
|
+
export function unfinished(repo: Repo, sessionId: string, seen: readonly string[], named: boolean): { keys: string[]; text: string } | undefined {
|
|
51
|
+
const opened = openProject(repo.root)
|
|
52
|
+
if ('notJoined' in opened) return undefined
|
|
53
|
+
const now = contentKey(opened.project)
|
|
54
|
+
if (baselines(repo).since(sessionId, now) === now || openStore(repo).passes.get(now)) return undefined
|
|
55
|
+
const key = `wall:check:${now}`
|
|
56
|
+
if (seen.includes(key)) return undefined
|
|
57
|
+
return {
|
|
58
|
+
keys: [key],
|
|
59
|
+
text:
|
|
60
|
+
`这个会话${named ? `在 ${repo.root} ` : ''}改了代码或 modwall 的配置,当前内容还没有 modwall check 的通过记录。` +
|
|
61
|
+
`用 Bash 跑 modwall check,按输出的改法改到通过再收尾;要放宽规则的,照输出调用 ${CONFIRM_TOOL} 工具让用户确认;改不了的,用白话告诉用户卡在哪。`,
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function guide(root: string, layout: Layout | undefined, label: string): string {
|
|
66
|
+
const lines = [`${label}用 modwall 按模块开发(.modwall/config.json)。模块边界以 modwall 为准:LayerMap 报的模块之间的依赖和循环按它自己推断的划分,不作数。`]
|
|
67
|
+
if (!layout || layout.problems.length) {
|
|
68
|
+
lines.push(`${CONFIG_PATH} 现在有问题或不在:用 Bash 跑 modwall check 看问题在哪,先改好配置。`)
|
|
69
|
+
return lines.join('\n')
|
|
70
|
+
}
|
|
71
|
+
const { scheme } = layout
|
|
72
|
+
lines.push(...(scheme.grouped ? groupedRules(scheme) : singleRules(scheme)))
|
|
73
|
+
const modules = layout.modules
|
|
74
|
+
if (modules.length === 0) {
|
|
75
|
+
lines.push('现在还没有模块。')
|
|
76
|
+
return lines.join('\n')
|
|
77
|
+
}
|
|
78
|
+
const many = modules.length > MANY_MODULES
|
|
79
|
+
const line = (module: Module): string => {
|
|
80
|
+
const about = moduleDoc(join(root, module.dir))
|
|
81
|
+
const text = about === undefined ? '' : many ? firstTwo(about) : about
|
|
82
|
+
return `- ${module.id}(${module.dir})${text ? `:${text}` : ''}`
|
|
83
|
+
}
|
|
84
|
+
if (!scheme.grouped) {
|
|
85
|
+
lines.push(`模块(${modules.length} 个):`, ...modules.map(line))
|
|
86
|
+
return lines.join('\n')
|
|
87
|
+
}
|
|
88
|
+
lines.push(`模块(${modules.length} 个,按应用分):`)
|
|
89
|
+
for (const app of scheme.apps) {
|
|
90
|
+
const own = modules.filter(module => module.app === app.name)
|
|
91
|
+
lines.push(`${app.name} 应用(${own.length} 个):`, ...(own.length ? own.map(line) : ['- 还没有模块']))
|
|
92
|
+
}
|
|
93
|
+
return lines.join('\n')
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const or = (items: string[]): string => (items.length ? items.join('、') : '没有')
|
|
97
|
+
|
|
98
|
+
const CHECK_LINES = [
|
|
99
|
+
'- 代码文件要放在模块、共享层或装配层里:没有归属的,新建和修改都会被拦。LayerMap 不读的目录(vendor、node_modules、.claude 这类,不分大小写)里不放项目代码:modwall 查不到它引用了谁,没写进 outside 的都算没有归属。',
|
|
100
|
+
`- 改完代码用 Bash 跑 modwall check,按输出的改法改到通过。要放宽规则(加例外、扩大共享层等)必须用户在 ${CONFIRM_TOOL} 工具的弹窗里同意(编号和文字照 modwall check 输出的填),你说"用户同意了"不算。写法见 gatelight:modular 技能。`,
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
/** 写了 database 的应用:数据库按模块分 schema 的约定。 */
|
|
104
|
+
function databaseLines(scheme: Scheme): string[] {
|
|
105
|
+
return scheme.apps.flatMap(app => {
|
|
106
|
+
const db = app.database
|
|
107
|
+
if (!db) return []
|
|
108
|
+
const at = app.name === undefined ? '' : `${app.name} 应用的`
|
|
109
|
+
const roles =
|
|
110
|
+
db.runtime === undefined
|
|
111
|
+
? '每个模块一个同名的数据库角色,程序每个事务开头切成模块的角色'
|
|
112
|
+
: `每个模块一个同名的数据库角色(拥有对象,迁移切成它跑)和一个运行角色(${db.runtime},只有本模块的读写授权,不拥有对象、不建对象),程序每个事务开头切成模块的运行角色`
|
|
113
|
+
return [
|
|
114
|
+
`- ${at}数据库(Postgres)也按模块分:模块的表、视图、函数建在它的内部 schema(模块目录名转成小写加下划线,adminAccess → admin_access),给别的模块用的视图和函数放进入口 schema(<内部 schema>_api,不放表);别的模块只引用对方入口 schema 里的对象,不在对方入口上建视图(跨模块读取写在函数正文或代码里),写入走对方入口里的函数,入口只加不改(入口里的类型别的模块可以用),模块之间不建外键(只存 id);只在自己的两个 schema 里建、改、删对象;共享层用 shared,public 里不放对象。迁移放在模块目录下的 ${db.migrations}/(共享层的在 ${db.shared}/),文件名以 14 位时间戳开头,只写正向(一个时间戳一个文件);迁移里不建 schema(跑迁移的工具建);函数一律带 SET search_path = <本模块的内部 schema>, pg_temp、一律 SECURITY DEFINER,迁移和函数里不改 search_path,引用别的模块、shared 写带 schema 的全名;别名、变量不和模块的 schema 同名,引用的表名和别的模块的 schema 同名时起别名。${roles}:迁移和模块的代码里不自己切身份(SET ROLE、set_config('role', …)),切身份只在装配层或共享层的那个小函数里。`,
|
|
115
|
+
]
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function singleRules(scheme: Scheme): string[] {
|
|
120
|
+
const app = scheme.apps[0] as App
|
|
121
|
+
return [
|
|
122
|
+
`- 模块根目录:${or(app.roots)},下面每个子目录是一个模块。新功能默认在模块根目录下新建一个模块目录,不用登记、不用问人。`,
|
|
123
|
+
`- 要别的模块的能力,只从它的入口(模块目录下第一层的 ${app.entry.join('、')})引用;入口里没有,就在对方入口新增导出,作为单独的一小步。`,
|
|
124
|
+
'- 不给别的模块的已有函数加分支:为新功能在对方新增一个函数。模块之间不能成环。',
|
|
125
|
+
`- 共享层(${or(app.shared)})只放真正通用的东西,不能引用模块和装配层;装配层(${or(app.app)})把各模块接起来,别人不能引用它。正式代码不引用 outside(${or(scheme.outside)})和测试文件。`,
|
|
126
|
+
...databaseLines(scheme),
|
|
127
|
+
...CHECK_LINES,
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 有几个应用:每个应用各有一套模块,按应用列出模块根目录、入口、共享层和装配层。 */
|
|
132
|
+
function groupedRules(scheme: Scheme): string[] {
|
|
133
|
+
const each = (part: (app: App) => string): string => scheme.apps.map(app => `${app.name}:${part(app)}`).join(';')
|
|
134
|
+
return [
|
|
135
|
+
`- 这个仓库有 ${scheme.apps.length} 个应用(${scheme.apps.map(app => app.name).join('、')}),每个应用各有一套模块,模块名只在应用内唯一,写成 应用名/模块名。应用之间不能引用代码(引用对方的模块、共享层、装配层都不行):几个应用共用的代码放进外面一层的共享层(${or(scheme.shared)}),它不能引用任何应用的代码;应用之间通过接口打交道。`,
|
|
136
|
+
`- 模块根目录(${each(app => or(app.roots))}),下面每个子目录是一个模块。新功能默认在所在应用的模块根目录下新建一个模块目录,不用登记、不用问人。`,
|
|
137
|
+
`- 要同一个应用里别的模块的能力,只从它的入口(模块目录下第一层的文件,${each(app => or(app.entry))})引用;入口里没有,就在对方入口新增导出,作为单独的一小步。`,
|
|
138
|
+
'- 不给别的模块的已有函数加分支:为新功能在对方新增一个函数。同一个应用里的模块之间不能成环。',
|
|
139
|
+
`- 每个应用的共享层(${each(app => or(app.shared))})只放这个应用里真正通用的东西,不能引用模块和装配层;装配层(${each(app => or(app.app))})把这个应用的各模块接起来,别人不能引用它。正式代码不引用 outside(${or(scheme.outside)})和测试文件。`,
|
|
140
|
+
...databaseLines(scheme),
|
|
141
|
+
...CHECK_LINES,
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 模块的 MODULE.md(文件名不分大小写),去掉 frontmatter 和标题,压成一段。写法和 module-gates 兼容。 */
|
|
146
|
+
function moduleDoc(dir: string): string | undefined {
|
|
147
|
+
let name: string | undefined
|
|
148
|
+
try {
|
|
149
|
+
name = readdirSync(dir).find(file => file.toLowerCase() === 'module.md')
|
|
150
|
+
} catch {
|
|
151
|
+
return undefined
|
|
152
|
+
}
|
|
153
|
+
if (!name) return undefined
|
|
154
|
+
const text = readFileSync(join(dir, name), 'utf8').replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/, '')
|
|
155
|
+
const prose = text
|
|
156
|
+
.split('\n')
|
|
157
|
+
.filter(line => !/^\s*#/.test(line))
|
|
158
|
+
.map(line => line.replace(/^\s*[-*]\s+/, '').trim())
|
|
159
|
+
.filter(Boolean)
|
|
160
|
+
.join(' ')
|
|
161
|
+
return prose || undefined
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** 分句:中文的句末标点后面直接断;英文的 . ! ? ; 后面要跟空白才断(1.5、e.g 这类不断)。 */
|
|
165
|
+
function sentences(text: string): string[] {
|
|
166
|
+
return text
|
|
167
|
+
.split(/(?<=[。!?;])|(?<=[.!?;])\s+/)
|
|
168
|
+
.map(part => part.trim())
|
|
169
|
+
.filter(Boolean)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 头两句:英文的句子之间留一个空格,中文的直接接上。 */
|
|
173
|
+
function firstTwo(text: string): string {
|
|
174
|
+
const [first, second] = sentences(text)
|
|
175
|
+
if (first === undefined) return ''
|
|
176
|
+
if (second === undefined) return first
|
|
177
|
+
return /[.!?;]$/.test(first) ? `${first} ${second}` : `${first}${second}`
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 改文件之前(path 相对仓库根目录):目标是代码文件又没有归属,就拒绝,返回原因。配置文件不在、或读出来校验不过(JSON 坏了、字段不对)时不拦,
|
|
182
|
+
* 交给 modwall check;路径重叠、map 指向不存在的模块这类要看全部文件才知道的配置错误,这里不查,照路径判断归属。
|
|
183
|
+
* 看的是工作区里现在的配置:这只是提前提醒,放宽配置躲不过 modwall check(放宽项要用户同意)。
|
|
184
|
+
*/
|
|
185
|
+
export function beforeEdit(repo: Repo, path: string, text?: string): string | undefined {
|
|
186
|
+
if (!isCode(path)) return undefined
|
|
187
|
+
// LayerMap 不读的目录里被 git 忽略的文件(装依赖生成的 node_modules 这类)不在检查范围里,和文件清单一致
|
|
188
|
+
if (unreadDirectory(path) !== undefined && isIgnored(repo.root, path)) return undefined
|
|
189
|
+
const loaded = readConfig(fileNow(repo.root, CONFIG_PATH))
|
|
190
|
+
if (!loaded.config) return undefined
|
|
191
|
+
const scheme = schemeOf(loaded.config)
|
|
192
|
+
const owner = ownerIn(scheme, path)
|
|
193
|
+
// 有归属的:再看要写进去的新文字里有没有绕过入口引用别的模块的内部文件(edit.ts)
|
|
194
|
+
if (owner.kind !== 'none') return text === undefined ? undefined : entryBypass(scheme, path, owner, text)
|
|
195
|
+
const { where, fix } = unownedText(scheme, path, owner)
|
|
196
|
+
const reason = owner.unread ? fix : `代码文件要${fix}新功能默认在${scheme.grouped ? '所在应用的' : ''}模块根目录下新建一个模块目录,不用登记。`
|
|
197
|
+
return `modwall(R5 文件没有归属):${where}。${reason}`
|
|
198
|
+
}
|