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.
Files changed (108) hide show
  1. package/.antigravity-plugin/plugin.json +9 -0
  2. package/.claude-plugin/plugin.json +11 -0
  3. package/.codex-plugin/plugin.json +18 -0
  4. package/.mcp.json +8 -0
  5. package/bin/modwall +8 -0
  6. package/bin/node.sh +14 -0
  7. package/bin/testcraft +8 -0
  8. package/bin/testpace +8 -0
  9. package/entry/command +44 -0
  10. package/hooks/antigravity.sh +24 -0
  11. package/hooks/codex.json +50 -0
  12. package/hooks/codex.sh +7 -0
  13. package/hooks/hooks.json +43 -0
  14. package/hooks/testpace.ts +25 -0
  15. package/hooks.json +16 -0
  16. package/mcp_config.json +8 -0
  17. package/package.json +21 -4
  18. package/setup.mjs +99 -0
  19. package/skills/merge/SKILL.md +42 -0
  20. package/skills/modular/SKILL.md +75 -0
  21. package/skills/setup/SKILL.md +101 -0
  22. package/skills/setup-modwall/SKILL.md +112 -0
  23. package/skills/setup-testcraft/SKILL.md +23 -0
  24. package/skills/setup-testcraft/checklist/dev-login.md +20 -0
  25. package/skills/write-tests/SKILL.md +153 -0
  26. package/src/base/agent.ts +100 -0
  27. package/src/base/baseline.ts +54 -0
  28. package/src/base/commandline.ts +40 -0
  29. package/src/base/commands.ts +94 -0
  30. package/src/base/confirm.ts +161 -0
  31. package/src/base/contract.ts +94 -0
  32. package/src/base/coverage.ts +46 -0
  33. package/src/base/entry.ts +90 -0
  34. package/src/base/excerpt.ts +46 -0
  35. package/src/base/execute.ts +70 -0
  36. package/src/base/exit.ts +6 -0
  37. package/src/base/format.ts +27 -0
  38. package/src/base/full.ts +160 -0
  39. package/src/base/gate.ts +251 -0
  40. package/src/base/git.ts +361 -0
  41. package/src/base/glob.ts +38 -0
  42. package/src/base/hook.ts +297 -0
  43. package/src/base/hookformat.ts +257 -0
  44. package/src/base/judging.ts +27 -0
  45. package/src/base/layermap.ts +69 -0
  46. package/src/base/module.ts +88 -0
  47. package/src/base/process-group.ts +101 -0
  48. package/src/base/records.ts +66 -0
  49. package/src/base/repo.ts +155 -0
  50. package/src/base/sessions.ts +55 -0
  51. package/src/base/shell.ts +176 -0
  52. package/src/base/stop.ts +100 -0
  53. package/src/base/store.ts +348 -0
  54. package/src/base/version.ts +24 -0
  55. package/src/cli.ts +186 -0
  56. package/src/craft/change.ts +24 -0
  57. package/src/craft/checklist.ts +157 -0
  58. package/src/craft/commands.ts +111 -0
  59. package/src/craft/config.ts +15 -0
  60. package/src/craft/full.ts +30 -0
  61. package/src/craft/index.ts +12 -0
  62. package/src/craft/session.ts +21 -0
  63. package/src/mcp.ts +79 -0
  64. package/src/modules.ts +10 -0
  65. package/src/modwall.ts +8 -0
  66. package/src/pace/commands.ts +281 -0
  67. package/src/pace/config.ts +103 -0
  68. package/src/pace/consent.ts +81 -0
  69. package/src/pace/focused.ts +208 -0
  70. package/src/pace/full.ts +38 -0
  71. package/src/pace/index.ts +16 -0
  72. package/src/pace/record.ts +84 -0
  73. package/src/pace/report.ts +335 -0
  74. package/src/pace/results.ts +243 -0
  75. package/src/pace/run/node-test-reporter.mjs +34 -0
  76. package/src/pace/run/node-test.ts +26 -0
  77. package/src/pace/run/playwright-reporter.mjs +48 -0
  78. package/src/pace/run/playwright.ts +34 -0
  79. package/src/pace/run/runner.ts +157 -0
  80. package/src/pace/run/targets.ts +130 -0
  81. package/src/pace/run/vitest-launcher.mjs +101 -0
  82. package/src/pace/run/vitest.ts +22 -0
  83. package/src/pace/session.ts +35 -0
  84. package/src/pace/testlist.ts +91 -0
  85. package/src/pace/tests.ts +22 -0
  86. package/src/pace/verify.ts +95 -0
  87. package/src/testcraft.ts +9 -0
  88. package/src/wall/check.ts +258 -0
  89. package/src/wall/commands.ts +279 -0
  90. package/src/wall/config.ts +394 -0
  91. package/src/wall/database.ts +136 -0
  92. package/src/wall/dbcheck.ts +389 -0
  93. package/src/wall/dbrules.ts +213 -0
  94. package/src/wall/edit.ts +64 -0
  95. package/src/wall/files.ts +35 -0
  96. package/src/wall/full.ts +30 -0
  97. package/src/wall/index.ts +15 -0
  98. package/src/wall/init.ts +149 -0
  99. package/src/wall/layermap.ts +124 -0
  100. package/src/wall/layout.ts +231 -0
  101. package/src/wall/loosen.ts +157 -0
  102. package/src/wall/miniprogram.ts +80 -0
  103. package/src/wall/project.ts +88 -0
  104. package/src/wall/rules.ts +400 -0
  105. package/src/wall/session.ts +198 -0
  106. package/src/wall/sql.ts +510 -0
  107. package/src/wall/store.ts +39 -0
  108. package/README.md +0 -3
@@ -0,0 +1,361 @@
1
+ // 和 git 打交道的部分:仓库位置、工作区内容哈希、默认分支、落地历史。
2
+
3
+ import { execFileSync, spawnSync } from 'node:child_process'
4
+ import { copyFileSync, existsSync, mkdtempSync, readFileSync, rmSync, statSync } from 'node:fs'
5
+ import { tmpdir } from 'node:os'
6
+ import { join, resolve } from 'node:path'
7
+
8
+ export function git(cwd: string, args: string[], env?: Record<string, string>): string {
9
+ return execFileSync('git', args, {
10
+ cwd,
11
+ env: env ? { ...process.env, ...env } : process.env,
12
+ encoding: 'utf8',
13
+ maxBuffer: 512 * 1024 * 1024,
14
+ stdio: ['ignore', 'pipe', 'pipe'],
15
+ }).replace(/\n$/, '')
16
+ }
17
+
18
+ /** 跑一条 git 命令;它失败时返回 undefined。 */
19
+ export function gitOr(cwd: string, args: string[]): string | undefined {
20
+ try {
21
+ return git(cwd, args)
22
+ } catch {
23
+ return undefined
24
+ }
25
+ }
26
+
27
+ /**
28
+ * 配置里给这个钩子事件声明的钩子(git hook list),每个一行,被关掉的写成 "disabled\t<名字>";
29
+ * 没有任何钩子时是空的。git 太旧、没有 hook list 时返回 undefined。
30
+ */
31
+ export function configuredHooks(root: string, event: string): string[] | undefined {
32
+ const r = spawnSync('git', ['hook', 'list', event], { cwd: root, encoding: 'utf8' })
33
+ // 没有任何钩子时退出码 1;git 太旧时是用法错误
34
+ if (r.status !== 0 && r.status !== 1) return undefined
35
+ return r.stdout.split('\n').filter(Boolean)
36
+ }
37
+
38
+ /** git 配置里这一项的值(各层配置合起来);没有返回 undefined。 */
39
+ export function configValue(root: string, key: string): string | undefined {
40
+ return gitOr(root, ['config', '--get', key])
41
+ }
42
+
43
+ /** 当前目录所在仓库(或 worktree)的根目录;不在仓库里返回 undefined。 */
44
+ export function repoRoot(cwd: string): string | undefined {
45
+ return gitOr(cwd, ['rev-parse', '--show-toplevel'])
46
+ }
47
+
48
+ /** 所有 worktree 共用的 .git 目录。记录放这里:同一份内容在哪个 worktree 测过都算数。 */
49
+ export function commonDir(root: string): string {
50
+ return git(root, ['rev-parse', '--path-format=absolute', '--git-common-dir'])
51
+ }
52
+
53
+ /**
54
+ * 在一份真实索引的副本上做事,不动真实索引。副本带着暂存区的内容和文件状态缓存。
55
+ */
56
+ function withTempIndex<T>(root: string, fn: (env: Record<string, string>) => T): T {
57
+ const index = git(root, ['rev-parse', '--path-format=absolute', '--git-path', 'index'])
58
+ const dir = mkdtempSync(join(tmpdir(), 'testpace-index-'))
59
+ const tmpIndex = join(dir, 'index')
60
+ try {
61
+ if (existsSync(index)) copyFileSync(index, tmpIndex)
62
+ return fn({ GIT_INDEX_FILE: tmpIndex })
63
+ } finally {
64
+ rmSync(dir, { recursive: true, force: true })
65
+ }
66
+ }
67
+
68
+ /**
69
+ * 工作区完整内容的哈希:已跟踪文件的当前内容加上未跟踪、未被 .gitignore 忽略的文件。
70
+ * 结果就是 git 的树对象 id,和"把这些内容提交后的提交树"是同一个值。
71
+ */
72
+ export function workTreeId(root: string): string {
73
+ return withTempIndex(root, env => {
74
+ // 标了 assume-unchanged 的文件,git add 会当它没改;先在临时索引里去掉这个标记。
75
+ // skip-worktree(稀疏检出)不动:那些文件不在磁盘上,去掉标记会被当成删除。
76
+ const assumed = git(root, ['ls-files', '-v', '-z'], env)
77
+ .split('\0')
78
+ .filter(entry => /^[a-z] /.test(entry))
79
+ .map(entry => entry.slice(2))
80
+ if (assumed.length > 0) {
81
+ execFileSync('git', ['update-index', '--no-assume-unchanged', '-z', '--stdin'], {
82
+ cwd: root,
83
+ env: { ...process.env, ...env },
84
+ input: `${assumed.join('\0')}\0`,
85
+ stdio: ['pipe', 'ignore', 'pipe'],
86
+ })
87
+ }
88
+ git(root, ['add', '--all', '--', '.'], env)
89
+ return git(root, ['write-tree'], env)
90
+ })
91
+ }
92
+
93
+ /** 两棵树之间改动过的文件路径(相对仓库根目录)。 */
94
+ export function changedPaths(root: string, from: string, to: string): string[] {
95
+ const out = git(root, ['diff-tree', '-r', '-z', '--name-only', '--no-renames', from, to])
96
+ return out.split('\0').filter(Boolean)
97
+ }
98
+
99
+ export function currentBranch(root: string): string | undefined {
100
+ return gitOr(root, ['symbolic-ref', '--quiet', '--short', 'HEAD'])
101
+ }
102
+
103
+ export function headCommit(root: string): string | undefined {
104
+ return gitOr(root, ['rev-parse', '--verify', '--quiet', 'HEAD'])
105
+ }
106
+
107
+ function refExists(root: string, ref: string): boolean {
108
+ return gitOr(root, ['rev-parse', '--verify', '--quiet', ref]) !== undefined
109
+ }
110
+
111
+ const ORIGIN = 'refs/remotes/origin/'
112
+
113
+ export type DefaultBranch =
114
+ | {
115
+ branch: string
116
+ /** origin/HEAD 指着一个不存在的分支(远程改了默认分支的名字,本地还是旧的):它指的是谁。这时默认分支是按 main、master 认的。 */
117
+ dangling?: string
118
+ }
119
+ | { branch?: undefined; problem: string }
120
+
121
+ /** 没有可信的 origin/HEAD 时,按这几个名字认默认分支。 */
122
+ export const FALLBACK_BRANCHES = ['main', 'master'] as const
123
+
124
+ /**
125
+ * 默认分支,认不出时说明原因。只在这里认,合并门、停止门、选测试的对比、报告都用这一个结果。
126
+ * origin/HEAD 指的分支在 origin 上确实有才信它:指着不存在的分支时照没有 origin/HEAD 认,
127
+ * 不然合并门守的是一个不存在的分支,真正的默认分支没人管。
128
+ * 没有可信的 origin/HEAD 时看 main、master,本地有或 origin 上有都算。
129
+ */
130
+ export function findDefaultBranch(root: string): DefaultBranch {
131
+ const head = gitOr(root, ['symbolic-ref', '--quiet', `${ORIGIN}HEAD`])
132
+ const target = head?.startsWith(ORIGIN) ? head.slice(ORIGIN.length) : head
133
+ if (target && head?.startsWith(ORIGIN) && refExists(root, head)) return { branch: target }
134
+ for (const name of FALLBACK_BRANCHES) {
135
+ if (refExists(root, `refs/heads/${name}`) || refExists(root, `${ORIGIN}${name}`)) return { branch: name, ...(target ? { dangling: target } : {}) }
136
+ }
137
+ return {
138
+ problem: target ? `origin/HEAD 指着不存在的分支 ${target},本地和 origin 上也没有 main、master` : '没有 origin/HEAD,本地和 origin 上也没有 main、master',
139
+ }
140
+ }
141
+
142
+ export function defaultBranch(root: string): string | undefined {
143
+ return findDefaultBranch(root).branch
144
+ }
145
+
146
+ export type ForkPoint =
147
+ | {
148
+ /** 分叉点的提交。 */
149
+ commit: string
150
+ /** 和哪个默认分支比的。 */
151
+ branch: string
152
+ }
153
+ | { commit?: undefined; problem: string }
154
+
155
+ /**
156
+ * 这次改动从哪里算起:当前提交和默认分支的分叉点(本地的默认分支,没有就用 origin 上的)。直接在默认分支上就是当前提交。
157
+ * 只在这里算,选测试的对比、评判用的配置读哪一版(judging.ts)都用这一个结果。算不出时说明原因。
158
+ */
159
+ export function forkPoint(root: string): ForkPoint {
160
+ const found = findDefaultBranch(root)
161
+ if ('problem' in found) return { problem: `认不出默认分支:${found.problem}` }
162
+ const { branch } = found
163
+ if (!headCommit(root)) return { problem: '仓库还没有提交' }
164
+ const commit = mergeBase(root, 'HEAD', `refs/heads/${branch}`) ?? mergeBase(root, 'HEAD', `${ORIGIN}${branch}`)
165
+ return commit ? { commit, branch } : { problem: `当前提交和 ${branch} 没有共同的祖先` }
166
+ }
167
+
168
+ export interface LandingFile {
169
+ path: string
170
+ added: number
171
+ }
172
+
173
+ export interface Landing {
174
+ commit: string
175
+ time: number
176
+ files: LandingFile[]
177
+ }
178
+
179
+ /** 默认分支上每次落地(第一父链上的提交,合并按相对第一父的改动算)新增的行数。 */
180
+ export function landings(root: string, branch: string, sinceMs: number): Landing[] {
181
+ // -z:路径原样输出(不加引号、不转义中文)。每个提交是一个 "<id> <时间>" 字段,后面跟着它的 "新增\t删除\t路径" 字段
182
+ const out = gitOr(root, [
183
+ 'log',
184
+ '--first-parent',
185
+ '--diff-merges=first-parent',
186
+ '--no-renames',
187
+ '--numstat',
188
+ '-z',
189
+ '--format=%H %ct',
190
+ // --since 把很小的数(0、86400 这类)当成"现在";--max-age 直接按秒算
191
+ `--max-age=${Math.floor(sinceMs / 1000)}`,
192
+ `refs/heads/${branch}`,
193
+ '--',
194
+ ])
195
+ if (!out) return []
196
+ const result: Landing[] = []
197
+ for (const field of out.split('\0')) {
198
+ const header = /^\n?([0-9a-f]{40,64}) (\d+)$/.exec(field)
199
+ if (header) {
200
+ result.push({ commit: header[1]!, time: Number(header[2]) * 1000, files: [] })
201
+ continue
202
+ }
203
+ const stat = /^\n?(\d+|-)\t(?:\d+|-)\t(.+)$/s.exec(field)
204
+ if (!stat || stat[1] === '-') continue
205
+ result.at(-1)?.files.push({ path: stat[2]!, added: Number(stat[1]) })
206
+ }
207
+ return result
208
+ }
209
+
210
+ /** 一个提交(或分支、标签)的树;找不到返回 undefined。 */
211
+ export function treeOf(root: string, rev: string): string | undefined {
212
+ return gitOr(root, ['rev-parse', '--verify', '--quiet', `${rev}^{tree}`])
213
+ }
214
+
215
+ export function commitOf(root: string, rev: string): string | undefined {
216
+ return gitOr(root, ['rev-parse', '--verify', '--quiet', `${rev}^{commit}`])
217
+ }
218
+
219
+ /** 提交(或分支)里有没有这个文件(path 相对仓库根目录)。 */
220
+ export function hasFile(root: string, rev: string, path: string): boolean {
221
+ return gitOr(root, ['cat-file', '-e', `${rev}:${path}`]) !== undefined
222
+ }
223
+
224
+ /** 提交(或分支)里这个文件原样的内容(path 相对仓库根目录,末尾的换行不去掉,能和工作区里的直接比);没有这个文件返回 undefined。 */
225
+ export function fileAt(root: string, rev: string, path: string): string | undefined {
226
+ return blobAt(root, rev, path)?.toString('utf8')
227
+ }
228
+
229
+ /** 提交(或分支)里这个目录下的全部文件(路径相对仓库根目录);没有这个目录时为空。 */
230
+ export function filesAt(root: string, rev: string, dir: string): string[] {
231
+ // -z:路径原样输出,不转义中文
232
+ const out = gitOr(root, ['ls-tree', '-r', '-z', '--name-only', rev, '--', `${dir.replace(/\/+$/, '')}/`])
233
+ return (out ?? '').split('\0').filter(Boolean)
234
+ }
235
+
236
+ /** 工作区里这个文件现在的内容。只读普通文件(跟着软链走);目录、不存在的都是 undefined。 */
237
+ export function fileNow(root: string, path: string): string | undefined {
238
+ const full = join(root, path)
239
+ return statSync(full, { throwIfNoEntry: false })?.isFile() ? readFileSync(full, 'utf8') : undefined
240
+ }
241
+
242
+ /**
243
+ * 工作区里这个目录下(不给 dir 就是整个仓库)现有的文件:跟踪的,加上没跟踪、没被 .gitignore 忽略的;
244
+ * 删掉了的不算,嵌套的仓库(别的 worktree)不进去。
245
+ */
246
+ export function filesNow(root: string, dir?: string): string[] {
247
+ const out = git(root, ['ls-files', '-z', '--cached', '--others', '--exclude-standard', ...(dir === undefined ? [] : ['--', `${dir.replace(/\/+$/, '')}/`])])
248
+ return [...new Set(out.split('\0').filter(Boolean))].filter(path => statSync(join(root, path), { throwIfNoEntry: false })?.isFile() === true)
249
+ }
250
+
251
+ /** 这个路径(相对仓库根目录)被 .gitignore 忽略了。 */
252
+ export function isIgnored(root: string, path: string): boolean {
253
+ return gitOr(root, ['check-ignore', '-q', '--', path]) !== undefined
254
+ }
255
+
256
+ /** 提交(或分支)里这个文件原样的字节(二进制文件也行);没有这个文件返回 undefined。 */
257
+ export function blobAt(root: string, rev: string, path: string): Buffer | undefined {
258
+ try {
259
+ return execFileSync('git', ['cat-file', 'blob', `${rev}:${path}`], { cwd: root, maxBuffer: 512 * 1024 * 1024, stdio: ['ignore', 'pipe', 'ignore'] })
260
+ } catch {
261
+ return undefined
262
+ }
263
+ }
264
+
265
+ /** 这次改动里的一个文件。 */
266
+ export interface Change {
267
+ status: 'A' | 'M' | 'D' | 'R'
268
+ path: string
269
+ /** 改名前的路径(status 为 R)。 */
270
+ oldPath?: string
271
+ }
272
+
273
+ /** 工作区和 base(一般是分叉点)比,改了哪些文件,含没提交的。改名照内容认;没跟踪的新文件算新增。只在这里算,各模块都用这一份。 */
274
+ export function changesSince(root: string, base: string): Change[] {
275
+ const out = git(root, ['diff', '--name-status', '-z', '-M', '--no-ext-diff', base])
276
+ const parts = out.split('\0').filter(part => part !== '')
277
+ const changes: Change[] = []
278
+ for (let i = 0; i < parts.length; ) {
279
+ const status = parts[i++] as string
280
+ const letter = status[0]
281
+ if (letter === 'R') {
282
+ const oldPath = parts[i++] as string
283
+ const path = parts[i++] as string
284
+ changes.push({ status: 'R', path, oldPath })
285
+ } else if (letter === 'C') {
286
+ i++
287
+ changes.push({ status: 'A', path: parts[i++] as string })
288
+ } else if (letter === 'A' || letter === 'M' || letter === 'D' || letter === 'T') {
289
+ changes.push({ status: letter === 'T' ? 'M' : letter, path: parts[i++] as string })
290
+ } else {
291
+ i++
292
+ }
293
+ }
294
+ const untracked = git(root, ['ls-files', '--others', '--exclude-standard', '-z']).split('\0').filter(Boolean)
295
+ for (const path of untracked) changes.push({ status: 'A', path })
296
+ return changes.sort((a, b) => a.path.localeCompare(b.path))
297
+ }
298
+
299
+ export interface DiffLines {
300
+ /** 新版里加上或改过的行:行号从 1 起。 */
301
+ added: Array<{ line: number; text: string }>
302
+ removed: string[]
303
+ }
304
+
305
+ /** 一个文件这次改了哪些行(和 base 比)。新文件整份算加上的。按文本比,不认 .gitattributes 的二进制标记和 textconv:标成 -diff 就看不到改了的行。 */
306
+ export function diffLines(root: string, base: string, change: Change): DiffLines {
307
+ if (change.status === 'A') {
308
+ const text = fileNow(root, change.path) ?? ''
309
+ return { added: text.split('\n').map((line, index) => ({ line: index + 1, text: line })), removed: [] }
310
+ }
311
+ if (change.status === 'D') {
312
+ return { added: [], removed: (fileAt(root, base, change.path) ?? '').split('\n') }
313
+ }
314
+ const paths = change.oldPath ? [change.oldPath, change.path] : [change.path]
315
+ const out = gitOr(root, ['diff', '-U0', '-M', '--no-color', '--no-ext-diff', '--text', '--no-textconv', base, '--', ...paths]) ?? ''
316
+ const added: DiffLines['added'] = []
317
+ const removed: string[] = []
318
+ let next = 0
319
+ for (const line of out.split('\n')) {
320
+ const hunk = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line)
321
+ if (hunk) {
322
+ next = Number(hunk[1])
323
+ continue
324
+ }
325
+ if (line.startsWith('+++') || line.startsWith('---')) continue
326
+ if (line.startsWith('+')) added.push({ line: next++, text: line.slice(1) })
327
+ else if (line.startsWith('-')) removed.push(line.slice(1))
328
+ }
329
+ return { added, removed }
330
+ }
331
+
332
+ export function hasRemote(root: string): boolean {
333
+ return (gitOr(root, ['remote']) ?? '') !== ''
334
+ }
335
+
336
+ export function isAncestor(root: string, ancestor: string, descendant: string): boolean {
337
+ try {
338
+ git(root, ['merge-base', '--is-ancestor', ancestor, descendant])
339
+ return true
340
+ } catch {
341
+ return false
342
+ }
343
+ }
344
+
345
+ export function mergeBase(root: string, a: string, b: string): string | undefined {
346
+ return gitOr(root, ['merge-base', a, b])
347
+ }
348
+
349
+ /** 一个引用(比如 refs/heads/main、refs/remotes/origin/main)指向的提交和它的树;引用不存在返回 undefined。 */
350
+ export function refTip(root: string, ref: string): { commit: string; tree: string } | undefined {
351
+ const out = gitOr(root, ['log', '-1', '--format=%H %T', ref, '--'])
352
+ if (!out) return undefined
353
+ const [commit = '', tree = ''] = out.split(' ')
354
+ return { commit, tree }
355
+ }
356
+
357
+ /** 远程上的同名分支(refs/remotes/<远程>/<分支>)里有没有一个已经包含这个提交。 */
358
+ export function onRemoteBranch(root: string, branch: string, commit: string): boolean {
359
+ const refs = gitOr(root, ['for-each-ref', '--format=%(refname)', `refs/remotes/*/${branch}`])
360
+ return (refs ?? '').split('\n').filter(Boolean).some(ref => isAncestor(root, commit, ref))
361
+ }
@@ -0,0 +1,38 @@
1
+ // 路径规则(约定里的 ignore、各模块配置里的规则)。相对仓库根目录匹配完整路径:
2
+ // `**` 跨目录,`*` 和 `?` 不跨目录,以 `/` 结尾表示整个目录。
3
+
4
+ export function globToRegExp(pattern: string): RegExp {
5
+ let p = pattern.replace(/^\.\//, '')
6
+ if (p.endsWith('/')) p += '**'
7
+ let out = ''
8
+ for (let i = 0; i < p.length; i++) {
9
+ const c = p[i]!
10
+ if (c === '*') {
11
+ if (p[i + 1] === '*') {
12
+ if (p[i + 2] === '/') {
13
+ out += '(?:.*/)?'
14
+ i += 2
15
+ } else {
16
+ out += '.*'
17
+ i += 1
18
+ }
19
+ } else {
20
+ out += '[^/]*'
21
+ }
22
+ } else if (c === '?') {
23
+ out += '[^/]'
24
+ } else {
25
+ out += c.replace(/[.+^${}()|[\]\\]/g, '\\$&')
26
+ }
27
+ }
28
+ return new RegExp(`^${out}$`)
29
+ }
30
+
31
+ export function matcher(patterns: string[]): (path: string) => boolean {
32
+ const res = patterns.map(globToRegExp)
33
+ return path => res.some(re => re.test(path))
34
+ }
35
+
36
+ export function matchesAny(path: string, patterns: readonly string[]): boolean {
37
+ return patterns.some(pattern => globToRegExp(pattern).test(path))
38
+ }