archtracker-mcp 0.1.1 → 0.2.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/dist/cli/index.js +1017 -98
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +9 -9
- package/dist/index.js +1017 -98
- package/dist/index.js.map +1 -1
- package/dist/mcp/index.js +1048 -123
- package/dist/mcp/index.js.map +1 -1
- package/package.json +12 -2
package/dist/index.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
var SCHEMA_VERSION = "1.0";
|
|
3
3
|
|
|
4
4
|
// src/analyzer/analyze.ts
|
|
5
|
+
import { resolve as resolve4 } from "path";
|
|
6
|
+
|
|
7
|
+
// src/analyzer/engines/dependency-cruiser.ts
|
|
5
8
|
import { resolve } from "path";
|
|
6
9
|
import { cruise } from "dependency-cruiser";
|
|
7
10
|
var DEFAULT_EXCLUDE = [
|
|
@@ -12,110 +15,1026 @@ var DEFAULT_EXCLUDE = [
|
|
|
12
15
|
"coverage",
|
|
13
16
|
"\\.archtracker"
|
|
14
17
|
];
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
18
|
+
var DependencyCruiserEngine = class {
|
|
19
|
+
async analyze(rootDir, options = {}) {
|
|
20
|
+
const {
|
|
21
|
+
exclude = [],
|
|
22
|
+
maxDepth = 0,
|
|
23
|
+
tsConfigPath,
|
|
24
|
+
includeTypeOnly = true
|
|
25
|
+
} = options;
|
|
26
|
+
const absRootDir = resolve(rootDir);
|
|
27
|
+
const allExclude = [...DEFAULT_EXCLUDE, ...exclude];
|
|
28
|
+
const excludePattern = allExclude.join("|");
|
|
29
|
+
const cruiseOptions = {
|
|
30
|
+
baseDir: absRootDir,
|
|
31
|
+
exclude: { path: excludePattern },
|
|
32
|
+
doNotFollow: { path: "node_modules" },
|
|
33
|
+
maxDepth,
|
|
34
|
+
tsPreCompilationDeps: includeTypeOnly ? true : false,
|
|
35
|
+
combinedDependencies: false
|
|
36
|
+
};
|
|
37
|
+
if (tsConfigPath) {
|
|
38
|
+
cruiseOptions.tsConfig = { fileName: tsConfigPath };
|
|
39
|
+
}
|
|
40
|
+
let result;
|
|
41
|
+
try {
|
|
42
|
+
result = await cruise(["."], cruiseOptions);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
45
|
+
throw new Error(`dependency-cruiser failed: ${message}`, {
|
|
46
|
+
cause: error
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (result.exitCode !== 0 && !result.output) {
|
|
50
|
+
throw new Error(`Analysis exited with code ${result.exitCode}`);
|
|
51
|
+
}
|
|
52
|
+
const cruiseResult = result.output;
|
|
53
|
+
return this.buildGraph(absRootDir, cruiseResult);
|
|
35
54
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
buildGraph(rootDir, cruiseResult) {
|
|
56
|
+
const files = {};
|
|
57
|
+
const edges = [];
|
|
58
|
+
const circularSet = /* @__PURE__ */ new Set();
|
|
59
|
+
const circularDependencies = [];
|
|
60
|
+
for (const mod of cruiseResult.modules) {
|
|
61
|
+
if (this.isExternalModule(mod)) continue;
|
|
62
|
+
files[mod.source] = {
|
|
63
|
+
path: mod.source,
|
|
64
|
+
exists: !mod.couldNotResolve,
|
|
65
|
+
dependencies: [],
|
|
66
|
+
dependents: []
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
for (const mod of cruiseResult.modules) {
|
|
70
|
+
for (const dep of mod.dependencies) {
|
|
71
|
+
if (dep.couldNotResolve || dep.coreModule) continue;
|
|
72
|
+
if (!files[mod.source] || this.isExternalDep(dep)) continue;
|
|
73
|
+
const edgeType = dep.typeOnly ? "type-only" : dep.dynamic ? "dynamic" : "static";
|
|
74
|
+
edges.push({ source: mod.source, target: dep.resolved, type: edgeType });
|
|
75
|
+
if (files[mod.source]) {
|
|
76
|
+
files[mod.source].dependencies.push(dep.resolved);
|
|
77
|
+
}
|
|
78
|
+
if (files[dep.resolved]) {
|
|
79
|
+
files[dep.resolved].dependents.push(mod.source);
|
|
80
|
+
}
|
|
81
|
+
if (dep.circular && dep.cycle) {
|
|
82
|
+
const cyclePath = dep.cycle.map((c) => c.name);
|
|
83
|
+
const cycleKey = [...cyclePath].sort().join("\u2192");
|
|
84
|
+
if (!circularSet.has(cycleKey)) {
|
|
85
|
+
circularSet.add(cycleKey);
|
|
86
|
+
circularDependencies.push({ cycle: cyclePath });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
rootDir,
|
|
93
|
+
files,
|
|
94
|
+
edges,
|
|
95
|
+
circularDependencies,
|
|
96
|
+
totalFiles: Object.keys(files).length,
|
|
97
|
+
totalEdges: edges.length
|
|
98
|
+
};
|
|
45
99
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
);
|
|
100
|
+
isExternalModule(mod) {
|
|
101
|
+
if (mod.coreModule) return true;
|
|
102
|
+
const depTypes = mod.dependencyTypes ?? [];
|
|
103
|
+
if (depTypes.some((t2) => t2.startsWith("npm") || t2 === "core")) return true;
|
|
104
|
+
return isExternalPath(mod.source);
|
|
105
|
+
}
|
|
106
|
+
isExternalDep(dep) {
|
|
107
|
+
if (dep.coreModule) return true;
|
|
108
|
+
if (dep.dependencyTypes.some((t2) => t2.startsWith("npm") || t2 === "core"))
|
|
109
|
+
return true;
|
|
110
|
+
return isExternalPath(dep.resolved);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
function isExternalPath(source) {
|
|
114
|
+
if (source.startsWith("@")) return true;
|
|
115
|
+
if (!source.includes("/") && !source.includes("\\") && !source.includes("."))
|
|
116
|
+
return true;
|
|
117
|
+
if (source.startsWith("node:")) return true;
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/analyzer/engines/regex-engine.ts
|
|
122
|
+
import { readdir, readFile } from "fs/promises";
|
|
123
|
+
import { join, relative, resolve as resolve2 } from "path";
|
|
124
|
+
|
|
125
|
+
// src/analyzer/engines/cycle.ts
|
|
126
|
+
function detectCycles(edges) {
|
|
127
|
+
const adj = /* @__PURE__ */ new Map();
|
|
128
|
+
for (const edge of edges) {
|
|
129
|
+
if (!adj.has(edge.source)) adj.set(edge.source, []);
|
|
130
|
+
adj.get(edge.source).push(edge.target);
|
|
131
|
+
}
|
|
132
|
+
const visited = /* @__PURE__ */ new Set();
|
|
133
|
+
const inStack = /* @__PURE__ */ new Set();
|
|
134
|
+
const cycles = [];
|
|
135
|
+
const cycleKeys = /* @__PURE__ */ new Set();
|
|
136
|
+
function dfs(node, path) {
|
|
137
|
+
if (inStack.has(node)) {
|
|
138
|
+
const cycleStart = path.indexOf(node);
|
|
139
|
+
if (cycleStart !== -1) {
|
|
140
|
+
const cycle = path.slice(cycleStart);
|
|
141
|
+
const key = [...cycle].sort().join("\u2192");
|
|
142
|
+
if (!cycleKeys.has(key)) {
|
|
143
|
+
cycleKeys.add(key);
|
|
144
|
+
cycles.push({ cycle });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (visited.has(node)) return;
|
|
150
|
+
visited.add(node);
|
|
151
|
+
inStack.add(node);
|
|
152
|
+
path.push(node);
|
|
153
|
+
for (const neighbor of adj.get(node) ?? []) {
|
|
154
|
+
dfs(neighbor, path);
|
|
155
|
+
}
|
|
156
|
+
path.pop();
|
|
157
|
+
inStack.delete(node);
|
|
158
|
+
}
|
|
159
|
+
for (const node of adj.keys()) {
|
|
160
|
+
if (!visited.has(node)) {
|
|
161
|
+
dfs(node, []);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return cycles;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// src/analyzer/engines/strip-comments.ts
|
|
168
|
+
function stripComments(content, style) {
|
|
169
|
+
switch (style) {
|
|
170
|
+
case "c-style":
|
|
171
|
+
return stripCStyle(content);
|
|
172
|
+
case "hash":
|
|
173
|
+
return stripHash(content);
|
|
174
|
+
case "python":
|
|
175
|
+
return stripPython(content);
|
|
176
|
+
case "ruby":
|
|
177
|
+
return stripRuby(content);
|
|
178
|
+
case "php":
|
|
179
|
+
return stripPhp(content);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function stripCStyle(content) {
|
|
183
|
+
let result = "";
|
|
184
|
+
let i = 0;
|
|
185
|
+
while (i < content.length) {
|
|
186
|
+
if (content[i] === "/" && content[i + 1] === "/") {
|
|
187
|
+
while (i < content.length && content[i] !== "\n") {
|
|
188
|
+
result += " ";
|
|
189
|
+
i++;
|
|
190
|
+
}
|
|
191
|
+
} else if (content[i] === "/" && content[i + 1] === "*") {
|
|
192
|
+
result += " ";
|
|
193
|
+
i++;
|
|
194
|
+
result += " ";
|
|
195
|
+
i++;
|
|
196
|
+
while (i < content.length) {
|
|
197
|
+
if (content[i] === "*" && content[i + 1] === "/") {
|
|
198
|
+
result += " ";
|
|
199
|
+
i++;
|
|
200
|
+
result += " ";
|
|
201
|
+
i++;
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
result += content[i] === "\n" ? "\n" : " ";
|
|
205
|
+
i++;
|
|
206
|
+
}
|
|
207
|
+
} else if (content[i] === '"') {
|
|
208
|
+
result += content[i];
|
|
209
|
+
i++;
|
|
210
|
+
while (i < content.length && content[i] !== '"') {
|
|
211
|
+
if (content[i] === "\\" && i + 1 < content.length) {
|
|
212
|
+
result += content[i];
|
|
213
|
+
i++;
|
|
214
|
+
result += content[i];
|
|
215
|
+
i++;
|
|
216
|
+
} else if (content[i] === "\n") {
|
|
217
|
+
result += "\n";
|
|
218
|
+
i++;
|
|
219
|
+
} else {
|
|
220
|
+
result += content[i];
|
|
221
|
+
i++;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (i < content.length) {
|
|
225
|
+
result += content[i];
|
|
226
|
+
i++;
|
|
227
|
+
}
|
|
228
|
+
} else if (content[i] === "`") {
|
|
229
|
+
result += " ";
|
|
230
|
+
i++;
|
|
231
|
+
while (i < content.length && content[i] !== "`") {
|
|
232
|
+
result += content[i] === "\n" ? "\n" : " ";
|
|
233
|
+
i++;
|
|
234
|
+
}
|
|
235
|
+
if (i < content.length) {
|
|
236
|
+
result += " ";
|
|
237
|
+
i++;
|
|
238
|
+
}
|
|
239
|
+
} else {
|
|
240
|
+
result += content[i];
|
|
241
|
+
i++;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return result;
|
|
245
|
+
}
|
|
246
|
+
function stripHash(content) {
|
|
247
|
+
let result = "";
|
|
248
|
+
let i = 0;
|
|
249
|
+
while (i < content.length) {
|
|
250
|
+
if (content[i] === "#") {
|
|
251
|
+
while (i < content.length && content[i] !== "\n") {
|
|
252
|
+
result += " ";
|
|
253
|
+
i++;
|
|
254
|
+
}
|
|
255
|
+
} else if (content[i] === '"') {
|
|
256
|
+
result += content[i];
|
|
257
|
+
i++;
|
|
258
|
+
while (i < content.length && content[i] !== '"') {
|
|
259
|
+
if (content[i] === "\\" && i + 1 < content.length) {
|
|
260
|
+
result += content[i++];
|
|
261
|
+
result += content[i++];
|
|
262
|
+
} else {
|
|
263
|
+
result += content[i++];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (i < content.length) {
|
|
267
|
+
result += content[i];
|
|
268
|
+
i++;
|
|
269
|
+
}
|
|
270
|
+
} else if (content[i] === "'") {
|
|
271
|
+
result += content[i];
|
|
272
|
+
i++;
|
|
273
|
+
while (i < content.length && content[i] !== "'") {
|
|
274
|
+
if (content[i] === "\\" && i + 1 < content.length) {
|
|
275
|
+
result += content[i++];
|
|
276
|
+
result += content[i++];
|
|
277
|
+
} else {
|
|
278
|
+
result += content[i++];
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (i < content.length) {
|
|
282
|
+
result += content[i];
|
|
283
|
+
i++;
|
|
284
|
+
}
|
|
285
|
+
} else {
|
|
286
|
+
result += content[i];
|
|
287
|
+
i++;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return result;
|
|
291
|
+
}
|
|
292
|
+
function stripPython(content) {
|
|
293
|
+
let result = "";
|
|
294
|
+
let i = 0;
|
|
295
|
+
while (i < content.length) {
|
|
296
|
+
if (content[i] === '"' && content[i + 1] === '"' && content[i + 2] === '"' || content[i] === "'" && content[i + 1] === "'" && content[i + 2] === "'") {
|
|
297
|
+
const quote = content[i];
|
|
298
|
+
const tripleQuote = quote + quote + quote;
|
|
299
|
+
result += " ";
|
|
300
|
+
i += 3;
|
|
301
|
+
while (i < content.length) {
|
|
302
|
+
if (content[i] === quote && content[i + 1] === quote && content[i + 2] === quote) {
|
|
303
|
+
result += " ";
|
|
304
|
+
i += 3;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
result += content[i] === "\n" ? "\n" : " ";
|
|
308
|
+
i++;
|
|
309
|
+
}
|
|
310
|
+
} else if (content[i] === "#") {
|
|
311
|
+
while (i < content.length && content[i] !== "\n") {
|
|
312
|
+
result += " ";
|
|
313
|
+
i++;
|
|
314
|
+
}
|
|
315
|
+
} else if (content[i] === '"' || content[i] === "'") {
|
|
316
|
+
const quote = content[i];
|
|
317
|
+
result += content[i];
|
|
318
|
+
i++;
|
|
319
|
+
while (i < content.length && content[i] !== quote) {
|
|
320
|
+
if (content[i] === "\\" && i + 1 < content.length) {
|
|
321
|
+
result += content[i++];
|
|
322
|
+
result += content[i++];
|
|
323
|
+
} else if (content[i] === "\n") {
|
|
324
|
+
result += "\n";
|
|
325
|
+
i++;
|
|
326
|
+
break;
|
|
327
|
+
} else {
|
|
328
|
+
result += content[i++];
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (i < content.length && content[i] === quote) {
|
|
332
|
+
result += content[i];
|
|
333
|
+
i++;
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
result += content[i];
|
|
337
|
+
i++;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return result;
|
|
341
|
+
}
|
|
342
|
+
function stripRuby(content) {
|
|
343
|
+
const lines = content.split("\n");
|
|
344
|
+
const result = [];
|
|
345
|
+
let inBlock = false;
|
|
346
|
+
for (const line of lines) {
|
|
347
|
+
if (!inBlock && line.startsWith("=begin")) {
|
|
348
|
+
inBlock = true;
|
|
349
|
+
result.push(" ".repeat(line.length));
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
if (inBlock) {
|
|
353
|
+
if (line.startsWith("=end")) {
|
|
354
|
+
inBlock = false;
|
|
355
|
+
}
|
|
356
|
+
result.push(" ".repeat(line.length));
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
let processed = "";
|
|
360
|
+
let i = 0;
|
|
361
|
+
while (i < line.length) {
|
|
362
|
+
if (line[i] === "#") {
|
|
363
|
+
processed += " ".repeat(line.length - i);
|
|
364
|
+
break;
|
|
365
|
+
} else if (line[i] === '"') {
|
|
366
|
+
processed += line[i];
|
|
367
|
+
i++;
|
|
368
|
+
while (i < line.length && line[i] !== '"') {
|
|
369
|
+
if (line[i] === "\\" && i + 1 < line.length) {
|
|
370
|
+
processed += line[i++];
|
|
371
|
+
processed += line[i++];
|
|
372
|
+
} else {
|
|
373
|
+
processed += line[i++];
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (i < line.length) {
|
|
377
|
+
processed += line[i];
|
|
378
|
+
i++;
|
|
379
|
+
}
|
|
380
|
+
} else if (line[i] === "'") {
|
|
381
|
+
processed += line[i];
|
|
382
|
+
i++;
|
|
383
|
+
while (i < line.length && line[i] !== "'") {
|
|
384
|
+
if (line[i] === "\\" && i + 1 < line.length) {
|
|
385
|
+
processed += line[i++];
|
|
386
|
+
processed += line[i++];
|
|
387
|
+
} else {
|
|
388
|
+
processed += line[i++];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (i < line.length) {
|
|
392
|
+
processed += line[i];
|
|
393
|
+
i++;
|
|
394
|
+
}
|
|
395
|
+
} else {
|
|
396
|
+
processed += line[i];
|
|
397
|
+
i++;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
result.push(processed);
|
|
401
|
+
}
|
|
402
|
+
return result.join("\n");
|
|
403
|
+
}
|
|
404
|
+
function stripPhp(content) {
|
|
405
|
+
let result = "";
|
|
406
|
+
let i = 0;
|
|
407
|
+
while (i < content.length) {
|
|
408
|
+
if (content[i] === "/" && content[i + 1] === "/" || content[i] === "#") {
|
|
409
|
+
while (i < content.length && content[i] !== "\n") {
|
|
410
|
+
result += " ";
|
|
411
|
+
i++;
|
|
412
|
+
}
|
|
413
|
+
} else if (content[i] === "/" && content[i + 1] === "*") {
|
|
414
|
+
result += " ";
|
|
415
|
+
i++;
|
|
416
|
+
result += " ";
|
|
417
|
+
i++;
|
|
418
|
+
while (i < content.length) {
|
|
419
|
+
if (content[i] === "*" && content[i + 1] === "/") {
|
|
420
|
+
result += " ";
|
|
421
|
+
i++;
|
|
422
|
+
result += " ";
|
|
423
|
+
i++;
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
result += content[i] === "\n" ? "\n" : " ";
|
|
427
|
+
i++;
|
|
428
|
+
}
|
|
429
|
+
} else if (content[i] === '"') {
|
|
430
|
+
result += content[i];
|
|
431
|
+
i++;
|
|
432
|
+
while (i < content.length && content[i] !== '"') {
|
|
433
|
+
if (content[i] === "\\" && i + 1 < content.length) {
|
|
434
|
+
result += content[i++];
|
|
435
|
+
result += content[i++];
|
|
436
|
+
} else {
|
|
437
|
+
result += content[i++];
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (i < content.length) {
|
|
441
|
+
result += content[i];
|
|
442
|
+
i++;
|
|
443
|
+
}
|
|
444
|
+
} else if (content[i] === "'") {
|
|
445
|
+
result += content[i];
|
|
446
|
+
i++;
|
|
447
|
+
while (i < content.length && content[i] !== "'") {
|
|
448
|
+
if (content[i] === "\\" && i + 1 < content.length) {
|
|
449
|
+
result += content[i++];
|
|
450
|
+
result += content[i++];
|
|
451
|
+
} else {
|
|
452
|
+
result += content[i++];
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
if (i < content.length) {
|
|
456
|
+
result += content[i];
|
|
457
|
+
i++;
|
|
458
|
+
}
|
|
459
|
+
} else {
|
|
460
|
+
result += content[i];
|
|
461
|
+
i++;
|
|
462
|
+
}
|
|
50
463
|
}
|
|
51
|
-
|
|
52
|
-
return buildGraph(absRootDir, cruiseResult);
|
|
464
|
+
return result;
|
|
53
465
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
466
|
+
|
|
467
|
+
// src/analyzer/engines/regex-engine.ts
|
|
468
|
+
var RegexEngine = class {
|
|
469
|
+
constructor(config) {
|
|
470
|
+
this.config = config;
|
|
471
|
+
}
|
|
472
|
+
async analyze(rootDir, options = {}) {
|
|
473
|
+
const absRootDir = resolve2(rootDir);
|
|
474
|
+
const excludePatterns = [
|
|
475
|
+
...this.config.defaultExclude ?? [],
|
|
476
|
+
...options.exclude ?? [],
|
|
477
|
+
"\\.archtracker"
|
|
478
|
+
].map((p) => new RegExp(p));
|
|
479
|
+
const projectFiles = await this.collectFiles(
|
|
480
|
+
absRootDir,
|
|
481
|
+
excludePatterns,
|
|
482
|
+
options.maxDepth ?? 0
|
|
483
|
+
);
|
|
484
|
+
const projectFileSet = new Set(projectFiles);
|
|
485
|
+
const files = {};
|
|
486
|
+
const edges = [];
|
|
487
|
+
const edgeSet = /* @__PURE__ */ new Set();
|
|
488
|
+
for (const filePath of projectFiles) {
|
|
489
|
+
const relPath = relative(absRootDir, filePath);
|
|
490
|
+
files[relPath] = {
|
|
491
|
+
path: relPath,
|
|
492
|
+
exists: true,
|
|
493
|
+
dependencies: [],
|
|
494
|
+
dependents: []
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
for (const filePath of projectFiles) {
|
|
498
|
+
const relSource = relative(absRootDir, filePath);
|
|
499
|
+
let content;
|
|
500
|
+
try {
|
|
501
|
+
content = await readFile(filePath, "utf-8");
|
|
502
|
+
} catch {
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
const stripped = stripComments(content, this.config.commentStyle);
|
|
506
|
+
const imports = this.extractImports(stripped);
|
|
507
|
+
for (const importPath of imports) {
|
|
508
|
+
const resolved = this.config.resolveImport(
|
|
509
|
+
importPath,
|
|
510
|
+
filePath,
|
|
511
|
+
absRootDir,
|
|
512
|
+
projectFileSet
|
|
513
|
+
);
|
|
514
|
+
if (!resolved) continue;
|
|
515
|
+
const relTarget = relative(absRootDir, resolved);
|
|
516
|
+
if (!files[relTarget]) continue;
|
|
517
|
+
if (relSource === relTarget) continue;
|
|
518
|
+
const edgeKey = `${relSource}\0${relTarget}`;
|
|
519
|
+
if (edgeSet.has(edgeKey)) continue;
|
|
520
|
+
edgeSet.add(edgeKey);
|
|
521
|
+
edges.push({
|
|
522
|
+
source: relSource,
|
|
523
|
+
target: relTarget,
|
|
524
|
+
type: "static"
|
|
525
|
+
});
|
|
526
|
+
files[relSource].dependencies.push(relTarget);
|
|
527
|
+
files[relTarget].dependents.push(relSource);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
const circularDependencies = detectCycles(edges);
|
|
531
|
+
return {
|
|
532
|
+
rootDir: absRootDir,
|
|
533
|
+
files,
|
|
534
|
+
edges,
|
|
535
|
+
circularDependencies,
|
|
536
|
+
totalFiles: Object.keys(files).length,
|
|
537
|
+
totalEdges: edges.length
|
|
66
538
|
};
|
|
67
539
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
files[mod.source].dependencies.push(dep.resolved);
|
|
80
|
-
}
|
|
81
|
-
if (files[dep.resolved]) {
|
|
82
|
-
files[dep.resolved].dependents.push(mod.source);
|
|
83
|
-
}
|
|
84
|
-
if (dep.circular && dep.cycle) {
|
|
85
|
-
const cyclePath = dep.cycle.map((c) => c.name);
|
|
86
|
-
const cycleKey = [...cyclePath].sort().join("\u2192");
|
|
87
|
-
if (!circularSet.has(cycleKey)) {
|
|
88
|
-
circularSet.add(cycleKey);
|
|
89
|
-
circularDependencies.push({ cycle: cyclePath });
|
|
540
|
+
extractImports(content) {
|
|
541
|
+
if (this.config.extractImports) {
|
|
542
|
+
return this.config.extractImports(content);
|
|
543
|
+
}
|
|
544
|
+
const imports = [];
|
|
545
|
+
for (const pattern of this.config.importPatterns) {
|
|
546
|
+
const regex = new RegExp(pattern.regex.source, pattern.regex.flags);
|
|
547
|
+
let match;
|
|
548
|
+
while ((match = regex.exec(content)) !== null) {
|
|
549
|
+
if (match[1]) {
|
|
550
|
+
imports.push(match[1]);
|
|
90
551
|
}
|
|
91
552
|
}
|
|
92
553
|
}
|
|
554
|
+
return imports;
|
|
93
555
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
556
|
+
async collectFiles(dir, excludePatterns, maxDepth, currentDepth = 0) {
|
|
557
|
+
if (maxDepth > 0 && currentDepth >= maxDepth) return [];
|
|
558
|
+
const results = [];
|
|
559
|
+
let entries;
|
|
560
|
+
try {
|
|
561
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
562
|
+
} catch {
|
|
563
|
+
return results;
|
|
564
|
+
}
|
|
565
|
+
for (const entry of entries) {
|
|
566
|
+
const fullPath = join(dir, entry.name);
|
|
567
|
+
const relPath = relative(dir, fullPath);
|
|
568
|
+
if (excludePatterns.some(
|
|
569
|
+
(p) => p.test(entry.name) || p.test(relPath) || p.test(fullPath)
|
|
570
|
+
)) {
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
if (entry.isDirectory()) {
|
|
574
|
+
if (entry.name.startsWith(".")) continue;
|
|
575
|
+
const sub = await this.collectFiles(
|
|
576
|
+
fullPath,
|
|
577
|
+
excludePatterns,
|
|
578
|
+
maxDepth,
|
|
579
|
+
currentDepth + 1
|
|
580
|
+
);
|
|
581
|
+
results.push(...sub);
|
|
582
|
+
} else if (entry.isFile()) {
|
|
583
|
+
const dotIdx = entry.name.lastIndexOf(".");
|
|
584
|
+
if (dotIdx > 0) {
|
|
585
|
+
const ext = entry.name.slice(dotIdx);
|
|
586
|
+
if (this.config.extensions.includes(ext)) {
|
|
587
|
+
results.push(fullPath);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
return results;
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
// src/analyzer/engines/detect.ts
|
|
597
|
+
import { readdir as readdir2, stat as stat2 } from "fs/promises";
|
|
598
|
+
import { join as join2 } from "path";
|
|
599
|
+
var MARKERS = [
|
|
600
|
+
{ file: "Cargo.toml", language: "rust" },
|
|
601
|
+
{ file: "go.mod", language: "go" },
|
|
602
|
+
{ file: "pyproject.toml", language: "python" },
|
|
603
|
+
{ file: "setup.py", language: "python" },
|
|
604
|
+
{ file: "requirements.txt", language: "python" },
|
|
605
|
+
{ file: "Pipfile", language: "python" },
|
|
606
|
+
{ file: "pom.xml", language: "java" },
|
|
607
|
+
{ file: "build.gradle", language: "java" },
|
|
608
|
+
{ file: "build.gradle.kts", language: "kotlin" },
|
|
609
|
+
{ file: "Package.swift", language: "swift" },
|
|
610
|
+
{ file: "Gemfile", language: "ruby" },
|
|
611
|
+
{ file: "composer.json", language: "php" },
|
|
612
|
+
{ file: "CMakeLists.txt", language: "c-cpp" },
|
|
613
|
+
{ file: "Makefile", language: "c-cpp" },
|
|
614
|
+
{ file: "package.json", language: "javascript" },
|
|
615
|
+
{ file: "tsconfig.json", language: "javascript" }
|
|
616
|
+
];
|
|
617
|
+
var EXT_MAP = {
|
|
618
|
+
".ts": "javascript",
|
|
619
|
+
".tsx": "javascript",
|
|
620
|
+
".js": "javascript",
|
|
621
|
+
".jsx": "javascript",
|
|
622
|
+
".mjs": "javascript",
|
|
623
|
+
".cjs": "javascript",
|
|
624
|
+
".py": "python",
|
|
625
|
+
".rs": "rust",
|
|
626
|
+
".go": "go",
|
|
627
|
+
".java": "java",
|
|
628
|
+
".c": "c-cpp",
|
|
629
|
+
".cpp": "c-cpp",
|
|
630
|
+
".cc": "c-cpp",
|
|
631
|
+
".cxx": "c-cpp",
|
|
632
|
+
".h": "c-cpp",
|
|
633
|
+
".hpp": "c-cpp",
|
|
634
|
+
".rb": "ruby",
|
|
635
|
+
".php": "php",
|
|
636
|
+
".swift": "swift",
|
|
637
|
+
".kt": "kotlin",
|
|
638
|
+
".kts": "kotlin"
|
|
639
|
+
};
|
|
640
|
+
async function detectLanguage(rootDir) {
|
|
641
|
+
for (const marker of MARKERS) {
|
|
642
|
+
try {
|
|
643
|
+
const s = await stat2(join2(rootDir, marker.file));
|
|
644
|
+
if (s.isFile() || s.isDirectory()) {
|
|
645
|
+
return marker.language;
|
|
646
|
+
}
|
|
647
|
+
} catch {
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
const counts = /* @__PURE__ */ new Map();
|
|
651
|
+
try {
|
|
652
|
+
await scanExtensions(rootDir, counts, 2, 0);
|
|
653
|
+
} catch {
|
|
654
|
+
}
|
|
655
|
+
if (counts.size > 0) {
|
|
656
|
+
let maxLang = "javascript";
|
|
657
|
+
let maxCount = 0;
|
|
658
|
+
for (const [lang, count] of counts) {
|
|
659
|
+
if (count > maxCount) {
|
|
660
|
+
maxCount = count;
|
|
661
|
+
maxLang = lang;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return maxLang;
|
|
665
|
+
}
|
|
666
|
+
return "javascript";
|
|
102
667
|
}
|
|
103
|
-
function
|
|
104
|
-
if (
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
668
|
+
async function scanExtensions(dir, counts, maxDepth, currentDepth) {
|
|
669
|
+
if (currentDepth >= maxDepth) return;
|
|
670
|
+
const entries = await readdir2(dir, { withFileTypes: true });
|
|
671
|
+
for (const entry of entries) {
|
|
672
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
|
673
|
+
if (entry.isDirectory() && currentDepth < maxDepth - 1) {
|
|
674
|
+
await scanExtensions(
|
|
675
|
+
join2(dir, entry.name),
|
|
676
|
+
counts,
|
|
677
|
+
maxDepth,
|
|
678
|
+
currentDepth + 1
|
|
679
|
+
);
|
|
680
|
+
} else if (entry.isFile()) {
|
|
681
|
+
const dotIdx = entry.name.lastIndexOf(".");
|
|
682
|
+
if (dotIdx > 0) {
|
|
683
|
+
const ext = entry.name.slice(dotIdx);
|
|
684
|
+
const lang = EXT_MAP[ext];
|
|
685
|
+
if (lang) {
|
|
686
|
+
counts.set(lang, (counts.get(lang) ?? 0) + 1);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
108
691
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
692
|
+
|
|
693
|
+
// src/analyzer/engines/languages.ts
|
|
694
|
+
import { readFileSync } from "fs";
|
|
695
|
+
import { join as join3, dirname, resolve as resolve3 } from "path";
|
|
696
|
+
var python = {
|
|
697
|
+
id: "python",
|
|
698
|
+
extensions: [".py"],
|
|
699
|
+
commentStyle: "python",
|
|
700
|
+
importPatterns: [
|
|
701
|
+
// from package.module import something
|
|
702
|
+
{ regex: /^from\s+(\.[\w.]*|\w[\w.]*)\s+import\b/gm },
|
|
703
|
+
// import package.module
|
|
704
|
+
{ regex: /^import\s+([\w.]+)/gm }
|
|
705
|
+
],
|
|
706
|
+
resolveImport(importPath, sourceFile, rootDir, projectFiles) {
|
|
707
|
+
if (importPath.startsWith(".")) {
|
|
708
|
+
const dots = importPath.match(/^\.+/)?.[0].length ?? 1;
|
|
709
|
+
let base = dirname(sourceFile);
|
|
710
|
+
for (let i = 1; i < dots; i++) base = dirname(base);
|
|
711
|
+
const rest = importPath.slice(dots).replace(/\./g, "/");
|
|
712
|
+
return tryPythonResolve(join3(base, rest), projectFiles);
|
|
713
|
+
}
|
|
714
|
+
const parts = importPath.replace(/\./g, "/");
|
|
715
|
+
return tryPythonResolve(join3(rootDir, parts), projectFiles);
|
|
716
|
+
},
|
|
717
|
+
defaultExclude: ["__pycache__", "\\.venv", "venv", "\\.egg-info", "dist", "build"]
|
|
718
|
+
};
|
|
719
|
+
function tryPythonResolve(base, projectFiles) {
|
|
720
|
+
if (projectFiles.has(base + ".py")) return base + ".py";
|
|
721
|
+
if (projectFiles.has(join3(base, "__init__.py"))) return join3(base, "__init__.py");
|
|
722
|
+
return null;
|
|
113
723
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
724
|
+
var rust = {
|
|
725
|
+
id: "rust",
|
|
726
|
+
extensions: [".rs"],
|
|
727
|
+
commentStyle: "c-style",
|
|
728
|
+
importPatterns: [],
|
|
729
|
+
// handled by extractImports
|
|
730
|
+
extractImports(content) {
|
|
731
|
+
const imports = [];
|
|
732
|
+
const modRegex = /\bmod\s+(\w+)\s*;/gm;
|
|
733
|
+
let match;
|
|
734
|
+
while ((match = modRegex.exec(content)) !== null) {
|
|
735
|
+
imports.push(match[1]);
|
|
736
|
+
}
|
|
737
|
+
const useRegex = /\buse\s+crate::([\s\S]*?);/gm;
|
|
738
|
+
while ((match = useRegex.exec(content)) !== null) {
|
|
739
|
+
const body = match[1].trim();
|
|
740
|
+
extractRustUsePaths(body, "", imports);
|
|
741
|
+
}
|
|
742
|
+
return imports;
|
|
743
|
+
},
|
|
744
|
+
resolveImport(importPath, sourceFile, rootDir, projectFiles) {
|
|
745
|
+
const srcDir = join3(rootDir, "src");
|
|
746
|
+
if (!importPath.includes("::")) {
|
|
747
|
+
const parentDir = dirname(sourceFile);
|
|
748
|
+
const asFile = join3(parentDir, importPath + ".rs");
|
|
749
|
+
if (projectFiles.has(asFile)) return asFile;
|
|
750
|
+
const asDir = join3(parentDir, importPath, "mod.rs");
|
|
751
|
+
if (projectFiles.has(asDir)) return asDir;
|
|
752
|
+
return null;
|
|
753
|
+
}
|
|
754
|
+
const segments = importPath.split("::");
|
|
755
|
+
for (let i = segments.length; i > 0; i--) {
|
|
756
|
+
const path = segments.slice(0, i).join("/");
|
|
757
|
+
const asFile = join3(srcDir, path + ".rs");
|
|
758
|
+
if (projectFiles.has(asFile)) return asFile;
|
|
759
|
+
const asDir = join3(srcDir, path, "mod.rs");
|
|
760
|
+
if (projectFiles.has(asDir)) return asDir;
|
|
761
|
+
}
|
|
762
|
+
return null;
|
|
763
|
+
},
|
|
764
|
+
defaultExclude: ["target"]
|
|
765
|
+
};
|
|
766
|
+
function extractRustUsePaths(body, prefix, results) {
|
|
767
|
+
const trimmed = body.trim();
|
|
768
|
+
const braceStart = trimmed.indexOf("{");
|
|
769
|
+
if (braceStart === -1) {
|
|
770
|
+
const path = prefix ? `${prefix}::${trimmed}` : trimmed;
|
|
771
|
+
if (path && !path.includes("{")) {
|
|
772
|
+
results.push(path);
|
|
773
|
+
}
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
let pathPrefix = trimmed.slice(0, braceStart).trim();
|
|
777
|
+
if (pathPrefix.endsWith("::")) {
|
|
778
|
+
pathPrefix = pathPrefix.slice(0, -2);
|
|
779
|
+
}
|
|
780
|
+
const fullPrefix = prefix ? `${prefix}::${pathPrefix}` : pathPrefix;
|
|
781
|
+
let depth = 0;
|
|
782
|
+
let braceEnd = -1;
|
|
783
|
+
for (let i = braceStart; i < trimmed.length; i++) {
|
|
784
|
+
if (trimmed[i] === "{") depth++;
|
|
785
|
+
else if (trimmed[i] === "}") {
|
|
786
|
+
depth--;
|
|
787
|
+
if (depth === 0) {
|
|
788
|
+
braceEnd = i;
|
|
789
|
+
break;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
if (braceEnd === -1) return;
|
|
794
|
+
const inner = trimmed.slice(braceStart + 1, braceEnd).trim();
|
|
795
|
+
const items = splitByTopLevelComma(inner);
|
|
796
|
+
for (const item of items) {
|
|
797
|
+
const cleaned = item.trim();
|
|
798
|
+
if (cleaned === "self") {
|
|
799
|
+
results.push(fullPrefix);
|
|
800
|
+
} else if (cleaned) {
|
|
801
|
+
extractRustUsePaths(cleaned, fullPrefix, results);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
function splitByTopLevelComma(s) {
|
|
806
|
+
const parts = [];
|
|
807
|
+
let depth = 0;
|
|
808
|
+
let start = 0;
|
|
809
|
+
for (let i = 0; i < s.length; i++) {
|
|
810
|
+
if (s[i] === "{") depth++;
|
|
811
|
+
else if (s[i] === "}") depth--;
|
|
812
|
+
else if (s[i] === "," && depth === 0) {
|
|
813
|
+
parts.push(s.slice(start, i));
|
|
814
|
+
start = i + 1;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
parts.push(s.slice(start));
|
|
818
|
+
return parts;
|
|
819
|
+
}
|
|
820
|
+
var go = {
|
|
821
|
+
id: "go",
|
|
822
|
+
extensions: [".go"],
|
|
823
|
+
commentStyle: "c-style",
|
|
824
|
+
importPatterns: [],
|
|
825
|
+
// handled by extractImports
|
|
826
|
+
extractImports(content) {
|
|
827
|
+
const imports = [];
|
|
828
|
+
const singleRegex = /\bimport\s+(?:\w+\s+)?"([^"]+)"/gm;
|
|
829
|
+
let match;
|
|
830
|
+
while ((match = singleRegex.exec(content)) !== null) {
|
|
831
|
+
imports.push(match[1]);
|
|
832
|
+
}
|
|
833
|
+
const blockRegex = /\bimport\s*\(([^)]*)\)/gms;
|
|
834
|
+
while ((match = blockRegex.exec(content)) !== null) {
|
|
835
|
+
const block = match[1];
|
|
836
|
+
const entryRegex = /(?:\w+\s+)?"([^"]+)"/g;
|
|
837
|
+
let entry;
|
|
838
|
+
while ((entry = entryRegex.exec(block)) !== null) {
|
|
839
|
+
imports.push(entry[1]);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
return imports;
|
|
843
|
+
},
|
|
844
|
+
resolveImport(importPath, _sourceFile, rootDir, projectFiles) {
|
|
845
|
+
const modPrefix = goModulePrefix(rootDir);
|
|
846
|
+
if (!modPrefix || !importPath.startsWith(modPrefix)) return null;
|
|
847
|
+
const relPath = importPath.slice(modPrefix.length + 1);
|
|
848
|
+
const pkgDir = join3(rootDir, relPath);
|
|
849
|
+
for (const f of projectFiles) {
|
|
850
|
+
if (f.startsWith(pkgDir + "/") && f.endsWith(".go")) return f;
|
|
851
|
+
}
|
|
852
|
+
return null;
|
|
853
|
+
},
|
|
854
|
+
defaultExclude: ["vendor"]
|
|
855
|
+
};
|
|
856
|
+
var goModCache = /* @__PURE__ */ new Map();
|
|
857
|
+
function goModulePrefix(rootDir) {
|
|
858
|
+
if (goModCache.has(rootDir)) return goModCache.get(rootDir);
|
|
859
|
+
try {
|
|
860
|
+
const content = readFileSync(join3(rootDir, "go.mod"), "utf-8");
|
|
861
|
+
const match = content.match(/^module\s+(.+)$/m);
|
|
862
|
+
const prefix = match ? match[1].trim() : null;
|
|
863
|
+
goModCache.set(rootDir, prefix);
|
|
864
|
+
return prefix;
|
|
865
|
+
} catch {
|
|
866
|
+
goModCache.set(rootDir, null);
|
|
867
|
+
return null;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
var java = {
|
|
871
|
+
id: "java",
|
|
872
|
+
extensions: [".java"],
|
|
873
|
+
commentStyle: "c-style",
|
|
874
|
+
importPatterns: [
|
|
875
|
+
// import com.example.ClassName;
|
|
876
|
+
{ regex: /^import\s+(?:static\s+)?([\w.]+);/gm }
|
|
877
|
+
],
|
|
878
|
+
resolveImport(importPath, _sourceFile, rootDir, projectFiles) {
|
|
879
|
+
const filePath = importPath.replace(/\./g, "/") + ".java";
|
|
880
|
+
for (const srcRoot of ["", "src/main/java/", "src/", "app/src/main/java/"]) {
|
|
881
|
+
const full = join3(rootDir, srcRoot, filePath);
|
|
882
|
+
if (projectFiles.has(full)) return full;
|
|
883
|
+
}
|
|
884
|
+
return null;
|
|
885
|
+
},
|
|
886
|
+
defaultExclude: ["build", "target", "\\.gradle", "\\.idea"]
|
|
887
|
+
};
|
|
888
|
+
var cCpp = {
|
|
889
|
+
id: "c-cpp",
|
|
890
|
+
extensions: [".c", ".cpp", ".cc", ".cxx", ".h", ".hpp"],
|
|
891
|
+
commentStyle: "c-style",
|
|
892
|
+
importPatterns: [
|
|
893
|
+
// #include "file.h" (skip <system> includes)
|
|
894
|
+
{ regex: /^#include\s+"([^"]+)"/gm }
|
|
895
|
+
],
|
|
896
|
+
resolveImport(importPath, sourceFile, rootDir, projectFiles) {
|
|
897
|
+
const fromSource = resolve3(dirname(sourceFile), importPath);
|
|
898
|
+
if (projectFiles.has(fromSource)) return fromSource;
|
|
899
|
+
const fromRoot = join3(rootDir, importPath);
|
|
900
|
+
if (projectFiles.has(fromRoot)) return fromRoot;
|
|
901
|
+
for (const incDir of ["include", "src"]) {
|
|
902
|
+
const full = join3(rootDir, incDir, importPath);
|
|
903
|
+
if (projectFiles.has(full)) return full;
|
|
904
|
+
}
|
|
905
|
+
return null;
|
|
906
|
+
},
|
|
907
|
+
defaultExclude: ["build", "cmake-build", "\\.o$", "\\.obj$"]
|
|
908
|
+
};
|
|
909
|
+
var ruby = {
|
|
910
|
+
id: "ruby",
|
|
911
|
+
extensions: [".rb"],
|
|
912
|
+
commentStyle: "ruby",
|
|
913
|
+
importPatterns: [
|
|
914
|
+
// require_relative 'path'
|
|
915
|
+
{ regex: /\brequire_relative\s+['"]([^'"]+)['"]/gm },
|
|
916
|
+
// require 'path' (for project-internal requires)
|
|
917
|
+
{ regex: /\brequire\s+['"]([^'"]+)['"]/gm }
|
|
918
|
+
],
|
|
919
|
+
resolveImport(importPath, sourceFile, rootDir, projectFiles) {
|
|
920
|
+
const withExt = importPath.endsWith(".rb") ? importPath : importPath + ".rb";
|
|
921
|
+
const fromSource = resolve3(dirname(sourceFile), withExt);
|
|
922
|
+
if (projectFiles.has(fromSource)) return fromSource;
|
|
923
|
+
const fromRoot = join3(rootDir, withExt);
|
|
924
|
+
if (projectFiles.has(fromRoot)) return fromRoot;
|
|
925
|
+
const fromLib = join3(rootDir, "lib", withExt);
|
|
926
|
+
if (projectFiles.has(fromLib)) return fromLib;
|
|
927
|
+
return null;
|
|
928
|
+
},
|
|
929
|
+
defaultExclude: ["vendor", "\\.bundle"]
|
|
930
|
+
};
|
|
931
|
+
var php = {
|
|
932
|
+
id: "php",
|
|
933
|
+
extensions: [".php"],
|
|
934
|
+
commentStyle: "php",
|
|
935
|
+
importPatterns: [
|
|
936
|
+
// require/include/require_once/include_once 'path'
|
|
937
|
+
{ regex: /\b(?:require|include)(?:_once)?\s+['"]([^'"]+)['"]/gm },
|
|
938
|
+
// use Namespace\Class (PSR-4 style)
|
|
939
|
+
{ regex: /^use\s+([\w\\]+)/gm }
|
|
940
|
+
],
|
|
941
|
+
resolveImport(importPath, sourceFile, rootDir, projectFiles) {
|
|
942
|
+
if (importPath.includes("/") || importPath.endsWith(".php")) {
|
|
943
|
+
const withExt = importPath.endsWith(".php") ? importPath : importPath + ".php";
|
|
944
|
+
const fromSource = resolve3(dirname(sourceFile), withExt);
|
|
945
|
+
if (projectFiles.has(fromSource)) return fromSource;
|
|
946
|
+
const fromRoot2 = join3(rootDir, withExt);
|
|
947
|
+
if (projectFiles.has(fromRoot2)) return fromRoot2;
|
|
948
|
+
return null;
|
|
949
|
+
}
|
|
950
|
+
const filePath = importPath.replace(/\\/g, "/") + ".php";
|
|
951
|
+
const fromRoot = join3(rootDir, filePath);
|
|
952
|
+
if (projectFiles.has(fromRoot)) return fromRoot;
|
|
953
|
+
const fromSrc = join3(rootDir, "src", filePath);
|
|
954
|
+
if (projectFiles.has(fromSrc)) return fromSrc;
|
|
955
|
+
return null;
|
|
956
|
+
},
|
|
957
|
+
defaultExclude: ["vendor"]
|
|
958
|
+
};
|
|
959
|
+
var swift = {
|
|
960
|
+
id: "swift",
|
|
961
|
+
extensions: [".swift"],
|
|
962
|
+
commentStyle: "c-style",
|
|
963
|
+
importPatterns: [
|
|
964
|
+
// import ModuleName (for cross-module dependencies)
|
|
965
|
+
{ regex: /^import\s+(?:class\s+|struct\s+|enum\s+|protocol\s+|func\s+|var\s+|let\s+|typealias\s+)?(\w+)/gm }
|
|
966
|
+
],
|
|
967
|
+
resolveImport(importPath, sourceFile, rootDir, projectFiles) {
|
|
968
|
+
const spmDir = join3(rootDir, "Sources", importPath);
|
|
969
|
+
for (const f of projectFiles) {
|
|
970
|
+
if (f.startsWith(spmDir + "/") && f.endsWith(".swift")) return f;
|
|
971
|
+
}
|
|
972
|
+
return null;
|
|
973
|
+
},
|
|
974
|
+
defaultExclude: ["\\.build", "DerivedData"]
|
|
975
|
+
};
|
|
976
|
+
var kotlin = {
|
|
977
|
+
id: "kotlin",
|
|
978
|
+
extensions: [".kt", ".kts"],
|
|
979
|
+
commentStyle: "c-style",
|
|
980
|
+
importPatterns: [
|
|
981
|
+
// import com.example.ClassName
|
|
982
|
+
{ regex: /^import\s+([\w.]+)/gm }
|
|
983
|
+
],
|
|
984
|
+
resolveImport(importPath, _sourceFile, rootDir, projectFiles) {
|
|
985
|
+
const filePath = importPath.replace(/\./g, "/");
|
|
986
|
+
for (const ext of [".kt", ".kts"]) {
|
|
987
|
+
for (const srcRoot of [
|
|
988
|
+
"",
|
|
989
|
+
"src/main/kotlin/",
|
|
990
|
+
"src/main/java/",
|
|
991
|
+
"src/",
|
|
992
|
+
"app/src/main/kotlin/",
|
|
993
|
+
"app/src/main/java/"
|
|
994
|
+
]) {
|
|
995
|
+
const full = join3(rootDir, srcRoot, filePath + ext);
|
|
996
|
+
if (projectFiles.has(full)) return full;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
return null;
|
|
1000
|
+
},
|
|
1001
|
+
defaultExclude: ["build", "\\.gradle", "\\.idea"]
|
|
1002
|
+
};
|
|
1003
|
+
var LANGUAGE_CONFIGS = {
|
|
1004
|
+
javascript: null,
|
|
1005
|
+
// handled by DependencyCruiserEngine
|
|
1006
|
+
python,
|
|
1007
|
+
rust,
|
|
1008
|
+
go,
|
|
1009
|
+
java,
|
|
1010
|
+
"c-cpp": cCpp,
|
|
1011
|
+
ruby,
|
|
1012
|
+
php,
|
|
1013
|
+
swift,
|
|
1014
|
+
kotlin
|
|
1015
|
+
};
|
|
1016
|
+
function getLanguageConfig(id) {
|
|
1017
|
+
return LANGUAGE_CONFIGS[id] ?? null;
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
// src/analyzer/analyze.ts
|
|
1021
|
+
async function analyzeProject(rootDir, options = {}) {
|
|
1022
|
+
const absRootDir = resolve4(rootDir);
|
|
1023
|
+
const language = options.language ?? await detectLanguage(absRootDir);
|
|
1024
|
+
try {
|
|
1025
|
+
if (language === "javascript") {
|
|
1026
|
+
return await new DependencyCruiserEngine().analyze(absRootDir, options);
|
|
1027
|
+
}
|
|
1028
|
+
const config = getLanguageConfig(language);
|
|
1029
|
+
if (!config) {
|
|
1030
|
+
throw new AnalyzerError(`No analyzer config for language: ${language}`);
|
|
1031
|
+
}
|
|
1032
|
+
return await new RegexEngine(config).analyze(absRootDir, options);
|
|
1033
|
+
} catch (error) {
|
|
1034
|
+
if (error instanceof AnalyzerError) throw error;
|
|
1035
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1036
|
+
throw new AnalyzerError(message, { cause: error });
|
|
1037
|
+
}
|
|
119
1038
|
}
|
|
120
1039
|
var AnalyzerError = class extends Error {
|
|
121
1040
|
constructor(message, options) {
|
|
@@ -382,8 +1301,8 @@ function formatAnalysisReport(graph, options = {}) {
|
|
|
382
1301
|
}
|
|
383
1302
|
|
|
384
1303
|
// src/storage/snapshot.ts
|
|
385
|
-
import { mkdir, writeFile, readFile, access } from "fs/promises";
|
|
386
|
-
import { join } from "path";
|
|
1304
|
+
import { mkdir, writeFile, readFile as readFile2, access } from "fs/promises";
|
|
1305
|
+
import { join as join4 } from "path";
|
|
387
1306
|
import { z } from "zod";
|
|
388
1307
|
var ARCHTRACKER_DIR = ".archtracker";
|
|
389
1308
|
var SNAPSHOT_FILE = "snapshot.json";
|
|
@@ -412,8 +1331,8 @@ var SnapshotSchema = z.object({
|
|
|
412
1331
|
graph: DependencyGraphSchema
|
|
413
1332
|
});
|
|
414
1333
|
async function saveSnapshot(projectRoot, graph) {
|
|
415
|
-
const dirPath =
|
|
416
|
-
const filePath =
|
|
1334
|
+
const dirPath = join4(projectRoot, ARCHTRACKER_DIR);
|
|
1335
|
+
const filePath = join4(dirPath, SNAPSHOT_FILE);
|
|
417
1336
|
const snapshot = {
|
|
418
1337
|
version: SCHEMA_VERSION,
|
|
419
1338
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -425,10 +1344,10 @@ async function saveSnapshot(projectRoot, graph) {
|
|
|
425
1344
|
return snapshot;
|
|
426
1345
|
}
|
|
427
1346
|
async function loadSnapshot(projectRoot) {
|
|
428
|
-
const filePath =
|
|
1347
|
+
const filePath = join4(projectRoot, ARCHTRACKER_DIR, SNAPSHOT_FILE);
|
|
429
1348
|
let raw;
|
|
430
1349
|
try {
|
|
431
|
-
raw = await
|
|
1350
|
+
raw = await readFile2(filePath, "utf-8");
|
|
432
1351
|
} catch (error) {
|
|
433
1352
|
if (isNodeError(error) && error.code === "ENOENT") {
|
|
434
1353
|
return null;
|
|
@@ -457,7 +1376,7 @@ async function loadSnapshot(projectRoot) {
|
|
|
457
1376
|
}
|
|
458
1377
|
async function hasArchtrackerDir(projectRoot) {
|
|
459
1378
|
try {
|
|
460
|
-
await access(
|
|
1379
|
+
await access(join4(projectRoot, ARCHTRACKER_DIR));
|
|
461
1380
|
return true;
|
|
462
1381
|
} catch {
|
|
463
1382
|
return false;
|