forked from systemfsoftware/systemfsoftware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitlint.config.ts
More file actions
299 lines (262 loc) · 8.82 KB
/
Copy pathcommitlint.config.ts
File metadata and controls
299 lines (262 loc) · 8.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { execFileSync } from 'node:child_process'
import pnpmScopes from '@commitlint/config-pnpm-scopes'
import type { UserConfig } from '@commitlint/types'
import { readdirSync } from 'node:fs'
import { dirname, join, relative } from 'node:path'
import { fileURLToPath } from 'node:url'
const repoRoot = dirname(fileURLToPath(import.meta.url))
const packagesDir = join(repoRoot, 'packages')
/**
* Derive intermediate group scopes (e.g. `stryker-js`) for directories that
* contain workspace packages but are not packages themselves. Preserves the
* scopes used by historical commits in this repo.
*/
const discoverGroupScopes = (dir: string): readonly string[] => {
const groups: string[] = []
for (const entry of readdirSync(dir, { withFileTypes: true })) {
if (!entry.isDirectory()) continue
const groupPath = join(dir, entry.name)
const hasPackageJson = (() => {
try {
return readdirSync(groupPath).includes('package.json')
} catch {
return false
}
})()
if (hasPackageJson) continue // it's a leaf package, not a group
const hasNestedPackage = readdirSync(groupPath, { withFileTypes: true })
.filter((e) => e.isDirectory())
.some((e) => {
try {
return readdirSync(join(groupPath, e.name)).includes('package.json')
} catch {
return false
}
})
if (hasNestedPackage) {
groups.push(relative(packagesDir, groupPath))
}
}
return groups
}
const EXTRA_SCOPES = ['repo', 'deps', 'release', 'ci', ...discoverGroupScopes(packagesDir)] as const
const matchesAny = (...patterns: readonly RegExp[]) => (path: string) => patterns.some((p) => p.test(path))
const isDoc = matchesAny(
/\.mdx?$/,
/^docs\//,
/(^|\/)README\.md$/i,
/(^|\/)AGENTS\.md$/i,
/(^|\/)CLAUDE\.md$/i,
/(^|\/)CHANGELOG\.md$/i,
)
const isTest = matchesAny(
/\.(test|spec|tst)\.(ts|tsx|js|jsx|mjs|cjs)$/,
/(^|\/)__tests__\//,
/(^|\/)__mocks__\//,
/(^|\/)tests\//,
/(^|\/)test-helpers\//,
/(^|\/)e2e\//,
/(^|\/)fixtures\//,
)
const isCI = matchesAny(
/^\.github\/workflows\//,
/^\.github\/actions\//,
/^\.github\/dependabot\.ya?ml$/,
)
const isLockfile = matchesAny(
/(^|\/)pnpm-lock\.yaml$/,
/(^|\/)package-lock\.json$/,
/(^|\/)bun\.lockb?$/,
/(^|\/)yarn\.lock$/,
)
const isTooling = matchesAny(
/^\.claude\//,
/^\.husky\//,
/^\.opencode\//,
/(^|\/)commitlint\.config\.[mc]?[jt]s$/,
/(^|\/)\.releaserc(\..+)?$/,
/(^|\/)\.lintstagedrc(\..+)?$/,
/(^|\/)tsconfig.*\.json$/,
/(^|\/)vitest\.config\.[mc]?[jt]s$/,
/(^|\/)stryker\.conf(ig)?\.[mc]?[jt]s$/,
/(^|\/)stryker(\..+)?\.json$/,
/(^|\/)\.editorconfig$/,
/(^|\/)\.gitignore$/,
/(^|\/)\.prettierrc(\..+)?$/,
/(^|\/)biome\.json$/,
/(^|\/)oxlint\.config\.[mc]?[jt]s$/,
/(^|\/)\.dprint\.jsonc?$/,
/(^|\/)package\.json$/,
/(^|\/)pnpm-workspace\.yaml$/,
/(^|\/)turbo\.json$/,
/(^|\/)\.npmrc$/,
)
const ALLOWED_BY_SHAPE: ReadonlyArray<{
readonly name: string
readonly match: (path: string) => boolean
readonly allowed: Readonly<Record<string, true>>
}> = [
{ name: 'docs', match: isDoc, allowed: { docs: true, chore: true, ai: true } },
{ name: 'test', match: isTest, allowed: { test: true, chore: true } },
{ name: 'CI', match: isCI, allowed: { ci: true, chore: true } },
{ name: 'lockfile', match: isLockfile, allowed: { deps: true, chore: true } },
{
name: 'tooling',
match: isTooling,
allowed: { chore: true, build: true, ci: true, deps: true, ai: true, security: true },
},
]
const stagedFiles = (): readonly string[] => {
try {
return execFileSync('git', ['diff', '--cached', '--name-only'], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
})
.split('\n')
.map((l) => l.trim())
.filter(Boolean)
} catch {
return []
}
}
/**
* SOTA monorepo scope discovery:
* Use @commitlint/config-pnpm-scopes to derive scopes from pnpm-workspace.yaml,
* then append repo-wide meta scopes. This automatically handles nested workspace
* members like packages/stryker-js/* without hand-rolled recursion.
*/
const scopeEnum = async (context: Record<string, unknown>) => {
const base = (await pnpmScopes.rules['scope-enum'](context)) as [number, 'always' | 'never', readonly string[]]
return [
base[0],
base[1],
[...base[2], ...EXTRA_SCOPES],
] as [number, 'always' | 'never', readonly string[]]
}
const configuration: UserConfig = {
extends: ['@commitlint/config-conventional', '@commitlint/config-pnpm-scopes'],
plugins: [
{
rules: {
'no-ai-coauthors': ({ raw }) => {
if (!raw) {
return [true, 'OK']
}
// AI co-author email patterns
const aiEmailPatterns = [
/noreply@anthropic\.com/i,
/cursoragent@cursor\.com/i,
/noreply@aider\.dev/i,
/cascade@windsurf\.com/i,
/noreply@codeium\.com/i,
/clio-agent@sisyphuslabs\.ai/i,
/factory-droid\[bot\]@users\.noreply\.github\.com/i,
] as const
// Only scan Co-authored-by lines for AI model mentions to avoid false positives
// (e.g., "Opus" audio codec, "Haiku" build tool)
const coauthorLines = raw.match(/^Co-?-?[Aa]uthored-by:.*$/gmi) || []
const aiModelPatterns = [
/\b(Claude\s+)?(Opus|Sonnet|Haiku)\b/i,
/\bgpt-4o\b/i,
/\bClaude\b.*\b3\.\d+\b/i,
] as const
const hasAIModelInCoauthor = coauthorLines.some((line: string) =>
aiModelPatterns.some((pattern) => pattern.test(line))
)
const hasAIEmail = aiEmailPatterns.some((pattern) => pattern.test(raw))
const hasAICoauthor = hasAIEmail || hasAIModelInCoauthor
return [
!hasAICoauthor,
hasAICoauthor
? 'AI co-authors and AI model references are not allowed in commit messages'
: 'OK',
]
},
'type-matches-diff-shape': ({ type }) => {
const files = stagedFiles()
if (files.length === 0 || !type) return [true, 'OK']
const allMatch = (m: (p: string) => boolean) => files.every(m)
for (const shape of ALLOWED_BY_SHAPE) {
if (allMatch(shape.match) && !shape.allowed[type]) {
const allowed = Object.keys(shape.allowed).sort().join(' / ')
return [false, `'${type}' with 100% ${shape.name} paths — REQUIRED type: ${allowed}`]
}
}
if (type === 'feat' || type === 'fix') {
const hasProductionSource = files.some(
(p) => !isDoc(p) && !isTest(p) && !isCI(p) && !isLockfile(p) && !isTooling(p),
)
if (!hasProductionSource) {
return [
false,
`'${type}' MUST touch >=1 production source file (none of: docs, test, CI, lockfile, tooling)`,
]
}
}
return [true, 'OK']
},
},
},
],
rules: {
// AI co-author prevention (enforced)
'no-ai-coauthors': [2, 'always'],
'type-matches-diff-shape': [2, 'always'],
// Commit types — aligned with semantic-release changelog filtering
// feat, fix, perf, api, revert appear in changelog; everything else is noise-filtered
'type-enum': [
2,
'always',
[
'ai',
'api',
'build',
'chore',
'ci',
'deps',
'docs',
'feat',
'fix',
'improvement',
'perf',
'refactor',
'revert',
'security',
'style',
'test',
],
],
// Scopes — auto-discovered from pnpm-workspace.yaml plus repo-wide meta scopes
'scope-enum': scopeEnum as unknown as [number, 'always' | 'never', readonly string[]],
'scope-case': [2, 'always', 'kebab-case'],
// Type constraints
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
// Subject constraints
'subject-case': [2, 'always', 'lower-case'],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
// Length constraints
'header-max-length': [2, 'always', 72],
'body-max-line-length': [2, 'always', 120],
'footer-max-line-length': [2, 'always', 100],
// Structural constraints
'body-leading-blank': [2, 'always'],
'footer-leading-blank': [2, 'always'],
'header-full-stop': [2, 'never', '.'],
'body-full-stop': [2, 'never', '.'],
// References encouraged but not required
'references-empty': [1, 'never'],
},
defaultIgnores: true,
formatter: '@commitlint/format',
}
// LLM ONE-SHOT TEMPLATE:
// feat: add user session management
// api: change authentication endpoint response format
//
// body with full details here
//
// BREAKING CHANGE: description if applicable
// Use api!:` for API contract breaking changes that aren't features or fixes
export default configuration