@girardelli/architect-core 8.1.0
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/src/core/analyzer.d.ts +42 -0
- package/dist/src/core/analyzer.js +431 -0
- package/dist/src/core/analyzer.js.map +1 -0
- package/dist/src/core/analyzers/forecast.d.ts +84 -0
- package/dist/src/core/analyzers/forecast.js +338 -0
- package/dist/src/core/analyzers/forecast.js.map +1 -0
- package/dist/src/core/analyzers/index.d.ts +9 -0
- package/dist/src/core/analyzers/index.js +7 -0
- package/dist/src/core/analyzers/index.js.map +1 -0
- package/dist/src/core/analyzers/temporal-scorer.d.ts +71 -0
- package/dist/src/core/analyzers/temporal-scorer.js +141 -0
- package/dist/src/core/analyzers/temporal-scorer.js.map +1 -0
- package/dist/src/core/anti-patterns.d.ts +28 -0
- package/dist/src/core/anti-patterns.js +264 -0
- package/dist/src/core/anti-patterns.js.map +1 -0
- package/dist/src/core/ast/ast-parser.interface.d.ts +20 -0
- package/dist/src/core/ast/ast-parser.interface.js +2 -0
- package/dist/src/core/ast/ast-parser.interface.js.map +1 -0
- package/dist/src/core/ast/path-resolver.d.ts +13 -0
- package/dist/src/core/ast/path-resolver.js +54 -0
- package/dist/src/core/ast/path-resolver.js.map +1 -0
- package/dist/src/core/ast/tree-sitter-parser.d.ts +10 -0
- package/dist/src/core/ast/tree-sitter-parser.js +142 -0
- package/dist/src/core/ast/tree-sitter-parser.js.map +1 -0
- package/dist/src/core/config.d.ts +11 -0
- package/dist/src/core/config.js +112 -0
- package/dist/src/core/config.js.map +1 -0
- package/dist/src/core/diagram.d.ts +9 -0
- package/dist/src/core/diagram.js +101 -0
- package/dist/src/core/diagram.js.map +1 -0
- package/dist/src/core/i18n.d.ts +14 -0
- package/dist/src/core/i18n.js +54 -0
- package/dist/src/core/i18n.js.map +1 -0
- package/dist/src/core/locales/en.d.ts +2 -0
- package/dist/src/core/locales/en.js +337 -0
- package/dist/src/core/locales/en.js.map +1 -0
- package/dist/src/core/locales/pt-BR.d.ts +172 -0
- package/dist/src/core/locales/pt-BR.js +337 -0
- package/dist/src/core/locales/pt-BR.js.map +1 -0
- package/dist/src/core/locales/types.d.ts +86 -0
- package/dist/src/core/locales/types.js +2 -0
- package/dist/src/core/locales/types.js.map +1 -0
- package/dist/src/core/plugin-loader.d.ts +11 -0
- package/dist/src/core/plugin-loader.js +67 -0
- package/dist/src/core/plugin-loader.js.map +1 -0
- package/dist/src/core/project-summarizer.d.ts +16 -0
- package/dist/src/core/project-summarizer.js +37 -0
- package/dist/src/core/project-summarizer.js.map +1 -0
- package/dist/src/core/refactor-engine.d.ts +18 -0
- package/dist/src/core/refactor-engine.js +87 -0
- package/dist/src/core/refactor-engine.js.map +1 -0
- package/dist/src/core/rules/barrel-optimizer.d.ts +13 -0
- package/dist/src/core/rules/barrel-optimizer.js +76 -0
- package/dist/src/core/rules/barrel-optimizer.js.map +1 -0
- package/dist/src/core/rules/dead-code-detector.d.ts +21 -0
- package/dist/src/core/rules/dead-code-detector.js +116 -0
- package/dist/src/core/rules/dead-code-detector.js.map +1 -0
- package/dist/src/core/rules/hub-splitter.d.ts +13 -0
- package/dist/src/core/rules/hub-splitter.js +117 -0
- package/dist/src/core/rules/hub-splitter.js.map +1 -0
- package/dist/src/core/rules/import-organizer.d.ts +13 -0
- package/dist/src/core/rules/import-organizer.js +84 -0
- package/dist/src/core/rules/import-organizer.js.map +1 -0
- package/dist/src/core/rules/module-grouper.d.ts +13 -0
- package/dist/src/core/rules/module-grouper.js +116 -0
- package/dist/src/core/rules/module-grouper.js.map +1 -0
- package/dist/src/core/rules-engine.d.ts +7 -0
- package/dist/src/core/rules-engine.js +89 -0
- package/dist/src/core/rules-engine.js.map +1 -0
- package/dist/src/core/scorer.d.ts +15 -0
- package/dist/src/core/scorer.js +165 -0
- package/dist/src/core/scorer.js.map +1 -0
- package/dist/src/core/summarizer/keyword-extractor.d.ts +6 -0
- package/dist/src/core/summarizer/keyword-extractor.js +38 -0
- package/dist/src/core/summarizer/keyword-extractor.js.map +1 -0
- package/dist/src/core/summarizer/module-inferrer.d.ts +11 -0
- package/dist/src/core/summarizer/module-inferrer.js +171 -0
- package/dist/src/core/summarizer/module-inferrer.js.map +1 -0
- package/dist/src/core/summarizer/package-reader.d.ts +3 -0
- package/dist/src/core/summarizer/package-reader.js +33 -0
- package/dist/src/core/summarizer/package-reader.js.map +1 -0
- package/dist/src/core/summarizer/purpose-inferrer.d.ts +8 -0
- package/dist/src/core/summarizer/purpose-inferrer.js +179 -0
- package/dist/src/core/summarizer/purpose-inferrer.js.map +1 -0
- package/dist/src/core/summarizer/readme-reader.d.ts +3 -0
- package/dist/src/core/summarizer/readme-reader.js +24 -0
- package/dist/src/core/summarizer/readme-reader.js.map +1 -0
- package/dist/src/core/types/architect-rules.d.ts +27 -0
- package/dist/src/core/types/architect-rules.js +2 -0
- package/dist/src/core/types/architect-rules.js.map +1 -0
- package/dist/src/core/types/core.d.ts +87 -0
- package/dist/src/core/types/core.js +2 -0
- package/dist/src/core/types/core.js.map +1 -0
- package/dist/src/core/types/infrastructure.d.ts +38 -0
- package/dist/src/core/types/infrastructure.js +2 -0
- package/dist/src/core/types/infrastructure.js.map +1 -0
- package/dist/src/core/types/plugin.d.ts +12 -0
- package/dist/src/core/types/plugin.js +2 -0
- package/dist/src/core/types/plugin.js.map +1 -0
- package/dist/src/core/types/rules.d.ts +53 -0
- package/dist/src/core/types/rules.js +2 -0
- package/dist/src/core/types/rules.js.map +1 -0
- package/dist/src/core/types/summarizer.d.ts +12 -0
- package/dist/src/core/types/summarizer.js +2 -0
- package/dist/src/core/types/summarizer.js.map +1 -0
- package/dist/src/infrastructure/git-cache.d.ts +6 -0
- package/dist/src/infrastructure/git-cache.js +41 -0
- package/dist/src/infrastructure/git-cache.js.map +1 -0
- package/dist/src/infrastructure/git-history.d.ts +112 -0
- package/dist/src/infrastructure/git-history.js +340 -0
- package/dist/src/infrastructure/git-history.js.map +1 -0
- package/dist/src/infrastructure/logger.d.ts +20 -0
- package/dist/src/infrastructure/logger.js +57 -0
- package/dist/src/infrastructure/logger.js.map +1 -0
- package/dist/src/infrastructure/scanner.d.ts +31 -0
- package/dist/src/infrastructure/scanner.js +334 -0
- package/dist/src/infrastructure/scanner.js.map +1 -0
- package/dist/tests/analyzers-integration.test.d.ts +7 -0
- package/dist/tests/analyzers-integration.test.js +140 -0
- package/dist/tests/analyzers-integration.test.js.map +1 -0
- package/dist/tests/anti-patterns.test.d.ts +1 -0
- package/dist/tests/anti-patterns.test.js +81 -0
- package/dist/tests/anti-patterns.test.js.map +1 -0
- package/dist/tests/ast-parser.test.d.ts +1 -0
- package/dist/tests/ast-parser.test.js +94 -0
- package/dist/tests/ast-parser.test.js.map +1 -0
- package/dist/tests/fixtures/monorepo/packages/app/src/index.d.ts +1 -0
- package/dist/tests/fixtures/monorepo/packages/app/src/index.js +9 -0
- package/dist/tests/fixtures/monorepo/packages/app/src/index.js.map +1 -0
- package/dist/tests/fixtures/monorepo/packages/core/src/index.d.ts +2 -0
- package/dist/tests/fixtures/monorepo/packages/core/src/index.js +11 -0
- package/dist/tests/fixtures/monorepo/packages/core/src/index.js.map +1 -0
- package/dist/tests/forecast.test.d.ts +7 -0
- package/dist/tests/forecast.test.js +380 -0
- package/dist/tests/forecast.test.js.map +1 -0
- package/dist/tests/git-history.test.d.ts +7 -0
- package/dist/tests/git-history.test.js +193 -0
- package/dist/tests/git-history.test.js.map +1 -0
- package/dist/tests/i18n.test.d.ts +1 -0
- package/dist/tests/i18n.test.js +39 -0
- package/dist/tests/i18n.test.js.map +1 -0
- package/dist/tests/monorepo-scan.test.d.ts +11 -0
- package/dist/tests/monorepo-scan.test.js +143 -0
- package/dist/tests/monorepo-scan.test.js.map +1 -0
- package/dist/tests/plugin-loader.test.d.ts +1 -0
- package/dist/tests/plugin-loader.test.js +31 -0
- package/dist/tests/plugin-loader.test.js.map +1 -0
- package/dist/tests/rules-engine.test.d.ts +1 -0
- package/dist/tests/rules-engine.test.js +112 -0
- package/dist/tests/rules-engine.test.js.map +1 -0
- package/dist/tests/scanner.test.d.ts +1 -0
- package/dist/tests/scanner.test.js +44 -0
- package/dist/tests/scanner.test.js.map +1 -0
- package/dist/tests/scorer.test.d.ts +1 -0
- package/dist/tests/scorer.test.js +610 -0
- package/dist/tests/scorer.test.js.map +1 -0
- package/dist/tests/temporal-scorer.test.d.ts +7 -0
- package/dist/tests/temporal-scorer.test.js +239 -0
- package/dist/tests/temporal-scorer.test.js.map +1 -0
- package/package.json +29 -0
- package/src/core/analyzer.ts +499 -0
- package/src/core/analyzers/forecast.ts +497 -0
- package/src/core/analyzers/index.ts +33 -0
- package/src/core/analyzers/temporal-scorer.ts +227 -0
- package/src/core/anti-patterns.ts +324 -0
- package/src/core/ast/ast-parser.interface.ts +21 -0
- package/src/core/ast/path-resolver.ts +61 -0
- package/src/core/ast/tree-sitter-parser.ts +158 -0
- package/src/core/config.ts +125 -0
- package/src/core/diagram.ts +129 -0
- package/src/core/i18n.ts +64 -0
- package/src/core/locales/en.ts +340 -0
- package/src/core/locales/pt-BR.ts +341 -0
- package/src/core/locales/types.ts +95 -0
- package/src/core/plugin-loader.ts +80 -0
- package/src/core/project-summarizer.ts +42 -0
- package/src/core/refactor-engine.ts +112 -0
- package/src/core/rules/barrel-optimizer.ts +99 -0
- package/src/core/rules/dead-code-detector.ts +134 -0
- package/src/core/rules/hub-splitter.ts +135 -0
- package/src/core/rules/import-organizer.ts +100 -0
- package/src/core/rules/module-grouper.ts +133 -0
- package/src/core/rules-engine.ts +100 -0
- package/src/core/scorer.ts +181 -0
- package/src/core/summarizer/keyword-extractor.ts +53 -0
- package/src/core/summarizer/module-inferrer.ts +194 -0
- package/src/core/summarizer/package-reader.ts +34 -0
- package/src/core/summarizer/purpose-inferrer.ts +197 -0
- package/src/core/summarizer/readme-reader.ts +24 -0
- package/src/core/types/architect-rules.ts +29 -0
- package/src/core/types/core.ts +94 -0
- package/src/core/types/infrastructure.ts +41 -0
- package/src/core/types/plugin.ts +19 -0
- package/src/core/types/rules.ts +51 -0
- package/src/core/types/summarizer.ts +8 -0
- package/src/infrastructure/git-cache.ts +52 -0
- package/src/infrastructure/git-history.ts +496 -0
- package/src/infrastructure/logger.ts +68 -0
- package/src/infrastructure/scanner.ts +349 -0
- package/tests/analyzers-integration.test.ts +174 -0
- package/tests/anti-patterns.test.ts +95 -0
- package/tests/ast-parser.test.ts +102 -0
- package/tests/fixtures/monorepo/package.json +6 -0
- package/tests/fixtures/monorepo/packages/app/package.json +12 -0
- package/tests/fixtures/monorepo/packages/app/src/index.ts +6 -0
- package/tests/fixtures/monorepo/packages/core/package.json +7 -0
- package/tests/fixtures/monorepo/packages/core/src/index.ts +7 -0
- package/tests/forecast.test.ts +504 -0
- package/tests/git-history.test.ts +254 -0
- package/tests/i18n.test.ts +47 -0
- package/tests/monorepo-scan.test.ts +170 -0
- package/tests/plugin-loader.test.ts +40 -0
- package/tests/rules-engine.test.ts +131 -0
- package/tests/scanner.test.ts +54 -0
- package/tests/scorer.test.ts +675 -0
- package/tests/temporal-scorer.test.ts +306 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import { readFileSync, existsSync } from 'fs';
|
|
2
|
+
// @ts-ignore - Audit cleanup unused variable
|
|
3
|
+
import { extname, relative, dirname, resolve, join } from 'path';
|
|
4
|
+
import { DependencyEdge, Layer } from './types/core.js';
|
|
5
|
+
import { FileNode } from './types/infrastructure.js';
|
|
6
|
+
import { ASTParser } from './ast/ast-parser.interface.js';
|
|
7
|
+
import { TreeSitterParser } from './ast/tree-sitter-parser.js';
|
|
8
|
+
import { PathResolver } from './ast/path-resolver.js';
|
|
9
|
+
|
|
10
|
+
export class ArchitectureAnalyzer {
|
|
11
|
+
// @ts-ignore - Audit cleanup unused variable
|
|
12
|
+
private projectPath: string;
|
|
13
|
+
private dependencyGraph: Map<string, Set<string>> = new Map();
|
|
14
|
+
// @ts-ignore - Audit cleanup unused variable
|
|
15
|
+
private fileExtensions: Map<string, string> = new Map();
|
|
16
|
+
private astParser: ASTParser;
|
|
17
|
+
private pathResolver: PathResolver;
|
|
18
|
+
private isInitialized = false;
|
|
19
|
+
|
|
20
|
+
constructor(projectPath: string) {
|
|
21
|
+
this.projectPath = projectPath;
|
|
22
|
+
this.astParser = new TreeSitterParser();
|
|
23
|
+
this.pathResolver = new PathResolver(projectPath);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async initialize(): Promise<void> {
|
|
27
|
+
if (this.isInitialized) return;
|
|
28
|
+
try {
|
|
29
|
+
await this.astParser.initialize();
|
|
30
|
+
this.pathResolver.initialize();
|
|
31
|
+
} catch (err) {
|
|
32
|
+
// Silent failure allows automatic Regex Fallback usage later
|
|
33
|
+
}
|
|
34
|
+
this.isInitialized = true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
analyzeDependencies(fileTree: FileNode): DependencyEdge[] {
|
|
38
|
+
this.getProjectPackageNames(fileTree);
|
|
39
|
+
this.buildDependencyGraph(fileTree);
|
|
40
|
+
return this.buildEdgeList();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
detectLayers(fileTree: FileNode): Layer[] {
|
|
44
|
+
const layers: Layer[] = [];
|
|
45
|
+
const apiFiles: string[] = [];
|
|
46
|
+
const serviceFiles: string[] = [];
|
|
47
|
+
const dataFiles: string[] = [];
|
|
48
|
+
const uiFiles: string[] = [];
|
|
49
|
+
const infraFiles: string[] = [];
|
|
50
|
+
|
|
51
|
+
this.categorizeFiles(fileTree, apiFiles, serviceFiles, dataFiles, uiFiles, infraFiles);
|
|
52
|
+
|
|
53
|
+
if (apiFiles.length > 0) {
|
|
54
|
+
layers.push({
|
|
55
|
+
name: 'API',
|
|
56
|
+
files: apiFiles,
|
|
57
|
+
description: 'API layer - handles external interfaces and routing',
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (serviceFiles.length > 0) {
|
|
62
|
+
layers.push({
|
|
63
|
+
name: 'Service',
|
|
64
|
+
files: serviceFiles,
|
|
65
|
+
description: 'Service layer - business logic and orchestration',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (dataFiles.length > 0) {
|
|
70
|
+
layers.push({
|
|
71
|
+
name: 'Data',
|
|
72
|
+
files: dataFiles,
|
|
73
|
+
description: 'Data layer - database access and persistence',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (uiFiles.length > 0) {
|
|
78
|
+
layers.push({
|
|
79
|
+
name: 'UI',
|
|
80
|
+
files: uiFiles,
|
|
81
|
+
description: 'UI layer - user interface components and views',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (infraFiles.length > 0) {
|
|
86
|
+
layers.push({
|
|
87
|
+
name: 'Infrastructure',
|
|
88
|
+
files: infraFiles,
|
|
89
|
+
description: 'Infrastructure layer - configuration and setup',
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return layers;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private buildDependencyGraph(node: FileNode): void {
|
|
97
|
+
if (node.type === 'file') {
|
|
98
|
+
const rawImports = this.parseImports(node.path);
|
|
99
|
+
const resolvedImports = rawImports.map(imp => this.resolveImportPath(node.path, imp));
|
|
100
|
+
this.dependencyGraph.set(node.path, new Set(resolvedImports));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (node.children) {
|
|
104
|
+
for (const child of node.children) {
|
|
105
|
+
this.buildDependencyGraph(child);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Resolve a relative import path to an absolute file path.
|
|
112
|
+
* Tries common extensions (.ts, .tsx, .js, .jsx, /index.ts, etc.)
|
|
113
|
+
*/
|
|
114
|
+
private resolveImportPath(fromFile: string, importPath: string): string {
|
|
115
|
+
// Non-relative imports (Python module names, etc.) — return as-is
|
|
116
|
+
if (!importPath.startsWith('.')) return importPath;
|
|
117
|
+
|
|
118
|
+
const dir = dirname(fromFile);
|
|
119
|
+
const base = resolve(dir, importPath);
|
|
120
|
+
|
|
121
|
+
// Common extensions to try
|
|
122
|
+
const extensions = ['.ts', '.tsx', '.js', '.jsx', '.py', '.dart', '.go', '.java', '.rb'];
|
|
123
|
+
const indexFiles = extensions.map(ext => join(base, `index${ext}`));
|
|
124
|
+
|
|
125
|
+
// Try exact match first
|
|
126
|
+
if (existsSync(base) && !existsSync(base + '/')) return base;
|
|
127
|
+
|
|
128
|
+
// Try with extensions
|
|
129
|
+
for (const ext of extensions) {
|
|
130
|
+
const candidate = base + ext;
|
|
131
|
+
if (existsSync(candidate)) return candidate;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Try as directory with index file
|
|
135
|
+
for (const indexFile of indexFiles) {
|
|
136
|
+
if (existsSync(indexFile)) return indexFile;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Fallback: return the resolved path even if file not found
|
|
140
|
+
return base;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Known Python standard library modules (partial list of most common ones)
|
|
145
|
+
*/
|
|
146
|
+
private static readonly PYTHON_STDLIB: Set<string> = new Set([
|
|
147
|
+
'abc', 'aifc', 'argparse', 'array', 'ast', 'asynchat', 'asyncio', 'asyncore',
|
|
148
|
+
'atexit', 'base64', 'bdb', 'binascii', 'binhex', 'bisect', 'builtins',
|
|
149
|
+
'bz2', 'calendar', 'cgi', 'cgitb', 'chunk', 'cmath', 'cmd', 'code',
|
|
150
|
+
'codecs', 'codeop', 'collections', 'colorsys', 'compileall', 'concurrent',
|
|
151
|
+
'configparser', 'contextlib', 'contextvars', 'copy', 'copyreg', 'cProfile',
|
|
152
|
+
'crypt', 'csv', 'ctypes', 'curses', 'dataclasses', 'datetime', 'dbm',
|
|
153
|
+
'decimal', 'difflib', 'dis', 'distutils', 'doctest', 'email', 'encodings',
|
|
154
|
+
'enum', 'errno', 'faulthandler', 'fcntl', 'filecmp', 'fileinput', 'fnmatch',
|
|
155
|
+
'fractions', 'ftplib', 'functools', 'gc', 'getopt', 'getpass', 'gettext',
|
|
156
|
+
'glob', 'grp', 'gzip', 'hashlib', 'heapq', 'hmac', 'html', 'http',
|
|
157
|
+
'idlelib', 'imaplib', 'imghdr', 'imp', 'importlib', 'inspect', 'io',
|
|
158
|
+
'ipaddress', 'itertools', 'json', 'keyword', 'lib2to3', 'linecache',
|
|
159
|
+
'locale', 'logging', 'lzma', 'mailbox', 'mailcap', 'marshal', 'math',
|
|
160
|
+
'mimetypes', 'mmap', 'modulefinder', 'multiprocessing', 'netrc', 'nis',
|
|
161
|
+
'nntplib', 'numbers', 'operator', 'optparse', 'os', 'ossaudiodev',
|
|
162
|
+
'pathlib', 'pdb', 'pickle', 'pickletools', 'pipes', 'pkgutil', 'platform',
|
|
163
|
+
'plistlib', 'poplib', 'posix', 'posixpath', 'pprint', 'profile', 'pstats',
|
|
164
|
+
'pty', 'pwd', 'py_compile', 'pyclbr', 'pydoc', 'queue', 'quopri',
|
|
165
|
+
'random', 're', 'readline', 'reprlib', 'resource', 'rlcompleter', 'runpy',
|
|
166
|
+
'sched', 'secrets', 'select', 'selectors', 'shelve', 'shlex', 'shutil',
|
|
167
|
+
'signal', 'site', 'smtpd', 'smtplib', 'sndhdr', 'socket', 'socketserver',
|
|
168
|
+
'sqlite3', 'ssl', 'stat', 'statistics', 'string', 'stringprep', 'struct',
|
|
169
|
+
'subprocess', 'sunau', 'symtable', 'sys', 'sysconfig', 'syslog', 'tabnanny',
|
|
170
|
+
'tarfile', 'telnetlib', 'tempfile', 'termios', 'test', 'textwrap', 'threading',
|
|
171
|
+
'time', 'timeit', 'tkinter', 'token', 'tokenize', 'tomllib', 'trace',
|
|
172
|
+
'traceback', 'tracemalloc', 'tty', 'turtle', 'turtledemo', 'types',
|
|
173
|
+
'typing', 'unicodedata', 'unittest', 'urllib', 'uu', 'uuid', 'venv',
|
|
174
|
+
'warnings', 'wave', 'weakref', 'webbrowser', 'winreg', 'winsound',
|
|
175
|
+
'wsgiref', 'xdrlib', 'xml', 'xmlrpc', 'zipapp', 'zipfile', 'zipimport',
|
|
176
|
+
'zlib', '_thread',
|
|
177
|
+
]);
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Resolve the project's root Python package name from directory structure.
|
|
181
|
+
* E.g., for projectPath="/foo/src", if there's a "deepguard/" dir, the package is "deepguard".
|
|
182
|
+
*/
|
|
183
|
+
private projectPackageNames: Set<string> | null = null;
|
|
184
|
+
|
|
185
|
+
private getProjectPackageNames(fileTree: FileNode): Set<string> {
|
|
186
|
+
if (this.projectPackageNames) return this.projectPackageNames;
|
|
187
|
+
this.projectPackageNames = new Set<string>();
|
|
188
|
+
if (fileTree.children) {
|
|
189
|
+
for (const child of fileTree.children) {
|
|
190
|
+
if (child.type === 'directory') {
|
|
191
|
+
this.projectPackageNames.add(child.name);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return this.projectPackageNames;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private parseImports(filePath: string): string[] {
|
|
199
|
+
try {
|
|
200
|
+
const content = readFileSync(filePath, 'utf-8');
|
|
201
|
+
|
|
202
|
+
// Tenta parsing via AST primariamente se estiver inicializado
|
|
203
|
+
if (this.isInitialized) {
|
|
204
|
+
try {
|
|
205
|
+
const rawImports = this.astParser.parseImports(content, filePath);
|
|
206
|
+
|
|
207
|
+
const filteredImports = rawImports.filter((imp) => {
|
|
208
|
+
const ext = extname(filePath);
|
|
209
|
+
if (ext === '.py') {
|
|
210
|
+
return this.isInternalPythonImport(imp);
|
|
211
|
+
}
|
|
212
|
+
// For JS/TS, usually we only care about relative or path-aliased internal paths
|
|
213
|
+
// But AST doesn't know what is internal. `rel` path check or alias resolver helps here.
|
|
214
|
+
// We'll rely on the resolver in `buildDependencyGraph` which naturally filters out non-existent local files.
|
|
215
|
+
return true;
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
return filteredImports;
|
|
219
|
+
} catch (astErr) {
|
|
220
|
+
// Fall through to regex on specific file failures (e.g. absent grammar)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ──────────────────────────────────────────────
|
|
225
|
+
// GRACEFUL FALLBACK (Regex)
|
|
226
|
+
// ──────────────────────────────────────────────
|
|
227
|
+
const ext = extname(filePath);
|
|
228
|
+
const imports: string[] = [];
|
|
229
|
+
|
|
230
|
+
if (ext === '.ts' || ext === '.tsx' || ext === '.js' || ext === '.jsx') {
|
|
231
|
+
const importRegex =
|
|
232
|
+
/(?:import|require)\s*(?:\{[^}]+\}|[^\s]+)\s*from\s*['"]([^'"]+)['"]/g;
|
|
233
|
+
let match;
|
|
234
|
+
while ((match = importRegex.exec(content)) !== null) {
|
|
235
|
+
const importPath = match[1];
|
|
236
|
+
// Regex antigo considerava apenas './' ou '../'.
|
|
237
|
+
// O Resolver depois lida com aliases. Para não quebrar legados guardamos tudo.
|
|
238
|
+
imports.push(importPath);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Match dynamic imports
|
|
242
|
+
const dynamicImportRegex = /import\s*\(['"]([^'"]+)['"]\)/g;
|
|
243
|
+
while ((match = dynamicImportRegex.exec(content)) !== null) {
|
|
244
|
+
imports.push(match[1]);
|
|
245
|
+
}
|
|
246
|
+
} else if (ext === '.py') {
|
|
247
|
+
const fromImportRegex = /^from\s+([\w.]+)\s+import\b/gm;
|
|
248
|
+
let match;
|
|
249
|
+
while ((match = fromImportRegex.exec(content)) !== null) {
|
|
250
|
+
const moduleName = match[1];
|
|
251
|
+
if (this.isInternalPythonImport(moduleName)) {
|
|
252
|
+
imports.push(moduleName);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const directImportRegex = /^import\s+([\w.]+)(?:\s+as\s+\w+)?$/gm;
|
|
256
|
+
while ((match = directImportRegex.exec(content)) !== null) {
|
|
257
|
+
const moduleName = match[1];
|
|
258
|
+
if (this.isInternalPythonImport(moduleName)) {
|
|
259
|
+
imports.push(moduleName);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
} else if (ext === '.java') {
|
|
263
|
+
const importRegex = /import\s+([^\s;]+);/g;
|
|
264
|
+
let match;
|
|
265
|
+
while ((match = importRegex.exec(content)) !== null) {
|
|
266
|
+
imports.push(match[1]);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return imports;
|
|
271
|
+
} catch {
|
|
272
|
+
return [];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Check if a Python import is internal to the project.
|
|
278
|
+
* Internal imports: relative (starts with .) or matches project package names.
|
|
279
|
+
* External imports: stdlib, third-party (numpy, cv2, PIL, etc.)
|
|
280
|
+
*/
|
|
281
|
+
private isInternalPythonImport(moduleName: string): boolean {
|
|
282
|
+
// Relative imports are always internal
|
|
283
|
+
if (moduleName.startsWith('.')) return true;
|
|
284
|
+
|
|
285
|
+
// Get the top-level module name (e.g., "deepguard" from "deepguard.cli")
|
|
286
|
+
const topLevel = moduleName.split('.')[0];
|
|
287
|
+
|
|
288
|
+
// Check against Python stdlib
|
|
289
|
+
if (ArchitectureAnalyzer.PYTHON_STDLIB.has(topLevel)) return false;
|
|
290
|
+
|
|
291
|
+
// Check if it matches a known project package directory
|
|
292
|
+
if (this.projectPackageNames && this.projectPackageNames.has(topLevel)) return true;
|
|
293
|
+
|
|
294
|
+
// Default: treat unknown imports as external (conservative approach)
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private buildEdgeList(): DependencyEdge[] {
|
|
299
|
+
const edges: DependencyEdge[] = [];
|
|
300
|
+
const seenEdges = new Set<string>();
|
|
301
|
+
|
|
302
|
+
for (const [from, toSet] of this.dependencyGraph.entries()) {
|
|
303
|
+
for (const to of toSet) {
|
|
304
|
+
const edgeKey = `${from}->${to}`;
|
|
305
|
+
if (!seenEdges.has(edgeKey)) {
|
|
306
|
+
edges.push({
|
|
307
|
+
from,
|
|
308
|
+
to,
|
|
309
|
+
type: 'import',
|
|
310
|
+
weight: 1,
|
|
311
|
+
});
|
|
312
|
+
seenEdges.add(edgeKey);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return edges;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
private categorizeFiles(
|
|
321
|
+
node: FileNode,
|
|
322
|
+
apiFiles: string[],
|
|
323
|
+
serviceFiles: string[],
|
|
324
|
+
dataFiles: string[],
|
|
325
|
+
uiFiles: string[],
|
|
326
|
+
infraFiles: string[]
|
|
327
|
+
): void {
|
|
328
|
+
if (node.type === 'file') {
|
|
329
|
+
const path = node.path.toLowerCase();
|
|
330
|
+
const name = node.name.toLowerCase();
|
|
331
|
+
|
|
332
|
+
// Skip test files — they don't belong to any architectural layer
|
|
333
|
+
if (
|
|
334
|
+
name.endsWith('.test.ts') || name.endsWith('.test.js') ||
|
|
335
|
+
name.endsWith('.spec.ts') || name.endsWith('.spec.js') ||
|
|
336
|
+
path.includes('/__tests__/') || path.includes('/__mocks__/')
|
|
337
|
+
) {
|
|
338
|
+
// Don't categorize test files into any layer
|
|
339
|
+
}
|
|
340
|
+
// Skip node_modules files (safety barrier)
|
|
341
|
+
else if (path.includes('node_modules')) {
|
|
342
|
+
// Don't categorize third-party files
|
|
343
|
+
}
|
|
344
|
+
// Data layer — check first (more specific patterns)
|
|
345
|
+
else if (
|
|
346
|
+
path.includes('/entities/') ||
|
|
347
|
+
path.includes('/entity/') ||
|
|
348
|
+
path.includes('/migrations/') ||
|
|
349
|
+
path.includes('/migration/') ||
|
|
350
|
+
path.includes('/seeds/') ||
|
|
351
|
+
path.includes('/seeders/') ||
|
|
352
|
+
path.includes('/data/') ||
|
|
353
|
+
path.includes('/db/') ||
|
|
354
|
+
path.includes('/database/') ||
|
|
355
|
+
path.includes('/models/') ||
|
|
356
|
+
path.includes('/schema/') ||
|
|
357
|
+
path.includes('/subscribers/') ||
|
|
358
|
+
name.endsWith('.entity.ts') ||
|
|
359
|
+
name.endsWith('.entity.js') ||
|
|
360
|
+
name.endsWith('.model.ts') ||
|
|
361
|
+
name.endsWith('.model.js') ||
|
|
362
|
+
name.includes('repository') ||
|
|
363
|
+
name.includes('dao') ||
|
|
364
|
+
name.includes('mapper') ||
|
|
365
|
+
name.includes('migration') ||
|
|
366
|
+
name.includes('seed') ||
|
|
367
|
+
name.includes('subscriber')
|
|
368
|
+
) {
|
|
369
|
+
dataFiles.push(node.path);
|
|
370
|
+
}
|
|
371
|
+
// Infrastructure layer
|
|
372
|
+
else if (
|
|
373
|
+
path.includes('/config/') ||
|
|
374
|
+
path.includes('/infra/') ||
|
|
375
|
+
path.includes('/infrastructure/') ||
|
|
376
|
+
path.includes('/setup/') ||
|
|
377
|
+
path.includes('/guards/') ||
|
|
378
|
+
path.includes('/pipes/') ||
|
|
379
|
+
path.includes('/interceptors/') ||
|
|
380
|
+
path.includes('/filters/') ||
|
|
381
|
+
path.includes('/decorators/') ||
|
|
382
|
+
path.includes('/middleware/') ||
|
|
383
|
+
path.includes('/middlewares/') ||
|
|
384
|
+
path.includes('/common/') ||
|
|
385
|
+
path.includes('/shared/') ||
|
|
386
|
+
path.includes('docker') ||
|
|
387
|
+
path.includes('kubernetes') ||
|
|
388
|
+
name.endsWith('.guard.ts') ||
|
|
389
|
+
name.endsWith('.pipe.ts') ||
|
|
390
|
+
name.endsWith('.interceptor.ts') ||
|
|
391
|
+
name.endsWith('.filter.ts') ||
|
|
392
|
+
name.endsWith('.decorator.ts') ||
|
|
393
|
+
name.endsWith('.middleware.ts') ||
|
|
394
|
+
name.includes('.config.') ||
|
|
395
|
+
name.includes('.module.')
|
|
396
|
+
) {
|
|
397
|
+
infraFiles.push(node.path);
|
|
398
|
+
}
|
|
399
|
+
// API layer
|
|
400
|
+
else if (
|
|
401
|
+
path.includes('/api/') ||
|
|
402
|
+
path.includes('/routes/') ||
|
|
403
|
+
path.includes('/controllers/') ||
|
|
404
|
+
name.endsWith('.controller.ts') ||
|
|
405
|
+
name.endsWith('.controller.js') ||
|
|
406
|
+
name.includes('route') ||
|
|
407
|
+
name.includes('controller') ||
|
|
408
|
+
name.includes('handler') ||
|
|
409
|
+
name.endsWith('.dto.ts') ||
|
|
410
|
+
name.endsWith('.dto.js')
|
|
411
|
+
) {
|
|
412
|
+
apiFiles.push(node.path);
|
|
413
|
+
}
|
|
414
|
+
// Service layer
|
|
415
|
+
else if (
|
|
416
|
+
path.includes('/service') ||
|
|
417
|
+
path.includes('/business') ||
|
|
418
|
+
path.includes('/logic') ||
|
|
419
|
+
path.includes('/use-cases/') ||
|
|
420
|
+
path.includes('/usecases/') ||
|
|
421
|
+
name.endsWith('.service.ts') ||
|
|
422
|
+
name.endsWith('.service.js') ||
|
|
423
|
+
name.includes('service') ||
|
|
424
|
+
name.includes('manager') ||
|
|
425
|
+
name.includes('facade') ||
|
|
426
|
+
name.includes('usecase')
|
|
427
|
+
) {
|
|
428
|
+
serviceFiles.push(node.path);
|
|
429
|
+
}
|
|
430
|
+
// UI layer
|
|
431
|
+
else if (
|
|
432
|
+
path.includes('/ui/') ||
|
|
433
|
+
path.includes('/components/') ||
|
|
434
|
+
path.includes('/pages/') ||
|
|
435
|
+
path.includes('/views/') ||
|
|
436
|
+
path.includes('/screens/') ||
|
|
437
|
+
path.includes('/templates/') ||
|
|
438
|
+
node.extension === '.tsx' ||
|
|
439
|
+
node.extension === '.jsx' ||
|
|
440
|
+
node.extension === '.vue' ||
|
|
441
|
+
node.extension === '.html'
|
|
442
|
+
) {
|
|
443
|
+
uiFiles.push(node.path);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (node.children) {
|
|
448
|
+
for (const child of node.children) {
|
|
449
|
+
this.categorizeFiles(
|
|
450
|
+
child,
|
|
451
|
+
apiFiles,
|
|
452
|
+
serviceFiles,
|
|
453
|
+
dataFiles,
|
|
454
|
+
uiFiles,
|
|
455
|
+
infraFiles
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
getModuleBoundaries(fileTree: FileNode): Map<string, string[]> {
|
|
462
|
+
const modules = new Map<string, string[]>();
|
|
463
|
+
this.identifyModules(fileTree, '', modules);
|
|
464
|
+
return modules;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
private identifyModules(
|
|
468
|
+
node: FileNode,
|
|
469
|
+
parentPath: string,
|
|
470
|
+
modules: Map<string, string[]>
|
|
471
|
+
): void {
|
|
472
|
+
if (node.type === 'directory') {
|
|
473
|
+
const moduleFiles: string[] = [];
|
|
474
|
+
this.collectFilesInModule(node, moduleFiles);
|
|
475
|
+
|
|
476
|
+
if (moduleFiles.length > 0) {
|
|
477
|
+
modules.set(node.name, moduleFiles);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (node.children) {
|
|
482
|
+
for (const child of node.children) {
|
|
483
|
+
this.identifyModules(child, parentPath + '/' + node.name, modules);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
private collectFilesInModule(node: FileNode, files: string[]): void {
|
|
489
|
+
if (node.type === 'file') {
|
|
490
|
+
files.push(node.path);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (node.children) {
|
|
494
|
+
for (const child of node.children) {
|
|
495
|
+
this.collectFilesInModule(child, files);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|