|
1
|
import { execSync } from "child_process" |
|
2
|
import { ChangedPackage, PackageKind } from "../types/index.js" |
|
3
|
import getMonorepoRoot from "../utils/get-monorepo-root.js" |
|
4
|
|
|
5
|
const MAX_DIFF_LINES_PER_FILE = 500 |
|
6
|
const MAX_FILES_PER_PACKAGE = 10 |
|
7
|
|
|
8
|
// File patterns that are not doc-relevant regardless of content |
|
9
|
const SKIP_FILE_PATTERNS = [ |
|
10
|
/\.spec\.(ts|tsx|js|jsx)$/, |
|
11
|
/\.test\.(ts|tsx|js|jsx)$/, |
|
12
|
/\/__tests__\//, |
|
13
|
/\/fixtures\//, |
|
14
|
/\/migrations\//, |
|
15
|
/\/__mocks__\//, |
|
16
|
] |
|
17
|
|
|
18
|
// File patterns that are always doc-relevant |
|
19
|
const DOC_RELEVANT_PATTERNS = [ |
|
20
|
/\/src\/services\//, |
|
21
|
/\/src\/models\//, |
|
22
|
/\/src\/.*\/workflows\//, |
|
23
|
/\/src\/.*\/steps\//, |
|
24
|
/\.ts$/, // catch-all: TypeScript files may export public APIs |
|
25
|
] |
|
26
|
|
|
27
|
export class DiffAnalyzer { |
|
28
|
private monorepoRoot: string |
|
29
|
|
|
30
|
constructor() { |
|
31
|
this.monorepoRoot = getMonorepoRoot() |
|
32
|
} |
|
33
|
|
|
34
|
async analyze(commitSha: string): Promise<ChangedPackage[]> { |
|
35
|
const allFiles = this.getCommitFiles(commitSha) |
|
36
|
const packageFiles = allFiles.filter((f) => f.startsWith("packages/")) |
|
37
|
|
|
38
|
if (packageFiles.length === 0) { |
|
39
|
return [] |
|
40
|
} |
|
41
|
|
|
42
|
const packageMap = this.groupByPackage(packageFiles) |
|
43
|
const result: ChangedPackage[] = [] |
|
44
|
|
|
45
|
for (const [packagePath, files] of packageMap) { |
|
46
|
const kind = this.classifyKind(packagePath) |
|
47
|
|
|
48
|
const docRelevantFiles = files.filter((f) => this.isDocRelevant(f)) |
|
49
|
// For known kinds, skip if no doc-relevant files. |
|
50
|
// For "other", include even without a relevance filter so Claude |
|
51
|
// can decide whether the change warrants documentation updates. |
|
52
|
if (docRelevantFiles.length === 0 && kind !== "other") { |
|
53
|
continue |
|
54
|
} |
|
55
|
|
|
56
|
const filesToDiff = kind === "other" ? files : docRelevantFiles |
|
57
|
const diff = this.getDiff(commitSha, filesToDiff) |
|
58
|
|
|
59
|
result.push({ |
|
60
|
path: packagePath, |
|
61
|
name: packagePath.split("/").pop() ?? packagePath, |
|
62
|
kind, |
|
63
|
files: filesToDiff, |
|
64
|
diff, |
|
65
|
}) |
|
66
|
} |
|
67
|
|
|
68
|
return result |
|
69
|
} |
|
70
|
|
|
71
|
private getCommitFiles(commitSha: string): string[] { |
|
72
|
try { |
|
73
|
const output = execSync( |
|
74
|
`git diff-tree --no-commit-id -r --name-only ${commitSha}`, |
|
75
|
{ cwd: this.monorepoRoot } |
|
76
|
) |
|
77
|
.toString() |
|
78
|
.trim() |
|
79
|
|
|
80
|
return output.split("\n").filter(Boolean) |
|
81
|
} catch { |
|
82
|
return [] |
|
83
|
} |
|
84
|
} |
|
85
|
|
|
86
|
private groupByPackage(files: string[]): Map<string, string[]> { |
|
87
|
const packageMap = new Map<string, string[]>() |
|
88
|
|
|
89
|
for (const file of files) { |
|
90
|
const parts = file.split("/") |
|
91
|
// Group by first 3 path segments: packages/<category>/<name> |
|
92
|
// e.g. "packages/modules/product" or "packages/core/framework" |
|
93
|
const packagePath = parts.slice(0, 3).join("/") |
|
94
|
if (!packageMap.has(packagePath)) { |
|
95
|
packageMap.set(packagePath, []) |
|
96
|
} |
|
97
|
packageMap.get(packagePath)!.push(file) |
|
98
|
} |
|
99
|
|
|
100
|
return packageMap |
|
101
|
} |
|
102
|
|
|
103
|
private classifyKind(packagePath: string): PackageKind { |
|
104
|
if (packagePath.startsWith("packages/modules/")) { |
|
105
|
return "module" |
|
106
|
} |
|
107
|
if (packagePath.startsWith("packages/core/core-flows")) { |
|
108
|
return "core-flow" |
|
109
|
} |
|
110
|
if ( |
|
111
|
packagePath.startsWith("packages/core/framework") || |
|
112
|
packagePath.startsWith("packages/core/utils") || |
|
113
|
packagePath.startsWith("packages/core/") |
|
114
|
) { |
|
115
|
return "framework" |
|
116
|
} |
|
117
|
if (packagePath.startsWith("packages/admin/")) { |
|
118
|
return "admin" |
|
119
|
} |
|
120
|
if (packagePath.startsWith("packages/design-system/")) { |
|
121
|
return "design-system" |
|
122
|
} |
|
123
|
if (packagePath.startsWith("packages/cli/")) { |
|
124
|
return "cli" |
|
125
|
} |
|
126
|
return "other" |
|
127
|
} |
|
128
|
|
|
129
|
private isDocRelevant(filePath: string): boolean { |
|
130
|
for (const pattern of SKIP_FILE_PATTERNS) { |
|
131
|
if (pattern.test(filePath)) { |
|
132
|
return false |
|
133
|
} |
|
134
|
} |
|
135
|
for (const pattern of DOC_RELEVANT_PATTERNS) { |
|
136
|
if (pattern.test(filePath)) { |
|
137
|
return true |
|
138
|
} |
|
139
|
} |
|
140
|
return false |
|
141
|
} |
|
142
|
|
|
143
|
private getDiff(commitSha: string, files: string[]): string { |
|
144
|
const filesToDiff = files.slice(0, MAX_FILES_PER_PACKAGE) |
|
145
|
let combined = "" |
|
146
|
|
|
147
|
for (const file of filesToDiff) { |
|
148
|
try { |
|
149
|
const raw = execSync( |
|
150
|
`git diff ${commitSha}~1 ${commitSha} -- "${file}"`, |
|
151
|
{ cwd: this.monorepoRoot } |
|
152
|
).toString() |
|
153
|
|
|
154
|
const lines = raw.split("\n") |
|
155
|
const truncated = |
|
156
|
lines.length > MAX_DIFF_LINES_PER_FILE |
|
157
|
? [ |
|
158
|
...lines.slice(0, MAX_DIFF_LINES_PER_FILE), |
|
159
|
`... (${lines.length - MAX_DIFF_LINES_PER_FILE} more lines truncated)`, |
|
160
|
] |
|
161
|
: lines |
|
162
|
|
|
163
|
combined += `### ${file}\n\`\`\`diff\n${truncated.join("\n")}\n\`\`\`\n\n` |
|
164
|
} catch { |
|
165
|
combined += `### ${file}\n(diff unavailable)\n\n` |
|
166
|
} |
|
167
|
} |
|
168
|
|
|
169
|
if (files.length > MAX_FILES_PER_PACKAGE) { |
|
170
|
combined += `... and ${files.length - MAX_FILES_PER_PACKAGE} more files not shown.\n` |
|
171
|
} |
|
172
|
|
|
173
|
return combined |
|
174
|
} |
|
175
|
} |