@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.
Files changed (217) hide show
  1. package/dist/src/core/analyzer.d.ts +42 -0
  2. package/dist/src/core/analyzer.js +431 -0
  3. package/dist/src/core/analyzer.js.map +1 -0
  4. package/dist/src/core/analyzers/forecast.d.ts +84 -0
  5. package/dist/src/core/analyzers/forecast.js +338 -0
  6. package/dist/src/core/analyzers/forecast.js.map +1 -0
  7. package/dist/src/core/analyzers/index.d.ts +9 -0
  8. package/dist/src/core/analyzers/index.js +7 -0
  9. package/dist/src/core/analyzers/index.js.map +1 -0
  10. package/dist/src/core/analyzers/temporal-scorer.d.ts +71 -0
  11. package/dist/src/core/analyzers/temporal-scorer.js +141 -0
  12. package/dist/src/core/analyzers/temporal-scorer.js.map +1 -0
  13. package/dist/src/core/anti-patterns.d.ts +28 -0
  14. package/dist/src/core/anti-patterns.js +264 -0
  15. package/dist/src/core/anti-patterns.js.map +1 -0
  16. package/dist/src/core/ast/ast-parser.interface.d.ts +20 -0
  17. package/dist/src/core/ast/ast-parser.interface.js +2 -0
  18. package/dist/src/core/ast/ast-parser.interface.js.map +1 -0
  19. package/dist/src/core/ast/path-resolver.d.ts +13 -0
  20. package/dist/src/core/ast/path-resolver.js +54 -0
  21. package/dist/src/core/ast/path-resolver.js.map +1 -0
  22. package/dist/src/core/ast/tree-sitter-parser.d.ts +10 -0
  23. package/dist/src/core/ast/tree-sitter-parser.js +142 -0
  24. package/dist/src/core/ast/tree-sitter-parser.js.map +1 -0
  25. package/dist/src/core/config.d.ts +11 -0
  26. package/dist/src/core/config.js +112 -0
  27. package/dist/src/core/config.js.map +1 -0
  28. package/dist/src/core/diagram.d.ts +9 -0
  29. package/dist/src/core/diagram.js +101 -0
  30. package/dist/src/core/diagram.js.map +1 -0
  31. package/dist/src/core/i18n.d.ts +14 -0
  32. package/dist/src/core/i18n.js +54 -0
  33. package/dist/src/core/i18n.js.map +1 -0
  34. package/dist/src/core/locales/en.d.ts +2 -0
  35. package/dist/src/core/locales/en.js +337 -0
  36. package/dist/src/core/locales/en.js.map +1 -0
  37. package/dist/src/core/locales/pt-BR.d.ts +172 -0
  38. package/dist/src/core/locales/pt-BR.js +337 -0
  39. package/dist/src/core/locales/pt-BR.js.map +1 -0
  40. package/dist/src/core/locales/types.d.ts +86 -0
  41. package/dist/src/core/locales/types.js +2 -0
  42. package/dist/src/core/locales/types.js.map +1 -0
  43. package/dist/src/core/plugin-loader.d.ts +11 -0
  44. package/dist/src/core/plugin-loader.js +67 -0
  45. package/dist/src/core/plugin-loader.js.map +1 -0
  46. package/dist/src/core/project-summarizer.d.ts +16 -0
  47. package/dist/src/core/project-summarizer.js +37 -0
  48. package/dist/src/core/project-summarizer.js.map +1 -0
  49. package/dist/src/core/refactor-engine.d.ts +18 -0
  50. package/dist/src/core/refactor-engine.js +87 -0
  51. package/dist/src/core/refactor-engine.js.map +1 -0
  52. package/dist/src/core/rules/barrel-optimizer.d.ts +13 -0
  53. package/dist/src/core/rules/barrel-optimizer.js +76 -0
  54. package/dist/src/core/rules/barrel-optimizer.js.map +1 -0
  55. package/dist/src/core/rules/dead-code-detector.d.ts +21 -0
  56. package/dist/src/core/rules/dead-code-detector.js +116 -0
  57. package/dist/src/core/rules/dead-code-detector.js.map +1 -0
  58. package/dist/src/core/rules/hub-splitter.d.ts +13 -0
  59. package/dist/src/core/rules/hub-splitter.js +117 -0
  60. package/dist/src/core/rules/hub-splitter.js.map +1 -0
  61. package/dist/src/core/rules/import-organizer.d.ts +13 -0
  62. package/dist/src/core/rules/import-organizer.js +84 -0
  63. package/dist/src/core/rules/import-organizer.js.map +1 -0
  64. package/dist/src/core/rules/module-grouper.d.ts +13 -0
  65. package/dist/src/core/rules/module-grouper.js +116 -0
  66. package/dist/src/core/rules/module-grouper.js.map +1 -0
  67. package/dist/src/core/rules-engine.d.ts +7 -0
  68. package/dist/src/core/rules-engine.js +89 -0
  69. package/dist/src/core/rules-engine.js.map +1 -0
  70. package/dist/src/core/scorer.d.ts +15 -0
  71. package/dist/src/core/scorer.js +165 -0
  72. package/dist/src/core/scorer.js.map +1 -0
  73. package/dist/src/core/summarizer/keyword-extractor.d.ts +6 -0
  74. package/dist/src/core/summarizer/keyword-extractor.js +38 -0
  75. package/dist/src/core/summarizer/keyword-extractor.js.map +1 -0
  76. package/dist/src/core/summarizer/module-inferrer.d.ts +11 -0
  77. package/dist/src/core/summarizer/module-inferrer.js +171 -0
  78. package/dist/src/core/summarizer/module-inferrer.js.map +1 -0
  79. package/dist/src/core/summarizer/package-reader.d.ts +3 -0
  80. package/dist/src/core/summarizer/package-reader.js +33 -0
  81. package/dist/src/core/summarizer/package-reader.js.map +1 -0
  82. package/dist/src/core/summarizer/purpose-inferrer.d.ts +8 -0
  83. package/dist/src/core/summarizer/purpose-inferrer.js +179 -0
  84. package/dist/src/core/summarizer/purpose-inferrer.js.map +1 -0
  85. package/dist/src/core/summarizer/readme-reader.d.ts +3 -0
  86. package/dist/src/core/summarizer/readme-reader.js +24 -0
  87. package/dist/src/core/summarizer/readme-reader.js.map +1 -0
  88. package/dist/src/core/types/architect-rules.d.ts +27 -0
  89. package/dist/src/core/types/architect-rules.js +2 -0
  90. package/dist/src/core/types/architect-rules.js.map +1 -0
  91. package/dist/src/core/types/core.d.ts +87 -0
  92. package/dist/src/core/types/core.js +2 -0
  93. package/dist/src/core/types/core.js.map +1 -0
  94. package/dist/src/core/types/infrastructure.d.ts +38 -0
  95. package/dist/src/core/types/infrastructure.js +2 -0
  96. package/dist/src/core/types/infrastructure.js.map +1 -0
  97. package/dist/src/core/types/plugin.d.ts +12 -0
  98. package/dist/src/core/types/plugin.js +2 -0
  99. package/dist/src/core/types/plugin.js.map +1 -0
  100. package/dist/src/core/types/rules.d.ts +53 -0
  101. package/dist/src/core/types/rules.js +2 -0
  102. package/dist/src/core/types/rules.js.map +1 -0
  103. package/dist/src/core/types/summarizer.d.ts +12 -0
  104. package/dist/src/core/types/summarizer.js +2 -0
  105. package/dist/src/core/types/summarizer.js.map +1 -0
  106. package/dist/src/infrastructure/git-cache.d.ts +6 -0
  107. package/dist/src/infrastructure/git-cache.js +41 -0
  108. package/dist/src/infrastructure/git-cache.js.map +1 -0
  109. package/dist/src/infrastructure/git-history.d.ts +112 -0
  110. package/dist/src/infrastructure/git-history.js +340 -0
  111. package/dist/src/infrastructure/git-history.js.map +1 -0
  112. package/dist/src/infrastructure/logger.d.ts +20 -0
  113. package/dist/src/infrastructure/logger.js +57 -0
  114. package/dist/src/infrastructure/logger.js.map +1 -0
  115. package/dist/src/infrastructure/scanner.d.ts +31 -0
  116. package/dist/src/infrastructure/scanner.js +334 -0
  117. package/dist/src/infrastructure/scanner.js.map +1 -0
  118. package/dist/tests/analyzers-integration.test.d.ts +7 -0
  119. package/dist/tests/analyzers-integration.test.js +140 -0
  120. package/dist/tests/analyzers-integration.test.js.map +1 -0
  121. package/dist/tests/anti-patterns.test.d.ts +1 -0
  122. package/dist/tests/anti-patterns.test.js +81 -0
  123. package/dist/tests/anti-patterns.test.js.map +1 -0
  124. package/dist/tests/ast-parser.test.d.ts +1 -0
  125. package/dist/tests/ast-parser.test.js +94 -0
  126. package/dist/tests/ast-parser.test.js.map +1 -0
  127. package/dist/tests/fixtures/monorepo/packages/app/src/index.d.ts +1 -0
  128. package/dist/tests/fixtures/monorepo/packages/app/src/index.js +9 -0
  129. package/dist/tests/fixtures/monorepo/packages/app/src/index.js.map +1 -0
  130. package/dist/tests/fixtures/monorepo/packages/core/src/index.d.ts +2 -0
  131. package/dist/tests/fixtures/monorepo/packages/core/src/index.js +11 -0
  132. package/dist/tests/fixtures/monorepo/packages/core/src/index.js.map +1 -0
  133. package/dist/tests/forecast.test.d.ts +7 -0
  134. package/dist/tests/forecast.test.js +380 -0
  135. package/dist/tests/forecast.test.js.map +1 -0
  136. package/dist/tests/git-history.test.d.ts +7 -0
  137. package/dist/tests/git-history.test.js +193 -0
  138. package/dist/tests/git-history.test.js.map +1 -0
  139. package/dist/tests/i18n.test.d.ts +1 -0
  140. package/dist/tests/i18n.test.js +39 -0
  141. package/dist/tests/i18n.test.js.map +1 -0
  142. package/dist/tests/monorepo-scan.test.d.ts +11 -0
  143. package/dist/tests/monorepo-scan.test.js +143 -0
  144. package/dist/tests/monorepo-scan.test.js.map +1 -0
  145. package/dist/tests/plugin-loader.test.d.ts +1 -0
  146. package/dist/tests/plugin-loader.test.js +31 -0
  147. package/dist/tests/plugin-loader.test.js.map +1 -0
  148. package/dist/tests/rules-engine.test.d.ts +1 -0
  149. package/dist/tests/rules-engine.test.js +112 -0
  150. package/dist/tests/rules-engine.test.js.map +1 -0
  151. package/dist/tests/scanner.test.d.ts +1 -0
  152. package/dist/tests/scanner.test.js +44 -0
  153. package/dist/tests/scanner.test.js.map +1 -0
  154. package/dist/tests/scorer.test.d.ts +1 -0
  155. package/dist/tests/scorer.test.js +610 -0
  156. package/dist/tests/scorer.test.js.map +1 -0
  157. package/dist/tests/temporal-scorer.test.d.ts +7 -0
  158. package/dist/tests/temporal-scorer.test.js +239 -0
  159. package/dist/tests/temporal-scorer.test.js.map +1 -0
  160. package/package.json +29 -0
  161. package/src/core/analyzer.ts +499 -0
  162. package/src/core/analyzers/forecast.ts +497 -0
  163. package/src/core/analyzers/index.ts +33 -0
  164. package/src/core/analyzers/temporal-scorer.ts +227 -0
  165. package/src/core/anti-patterns.ts +324 -0
  166. package/src/core/ast/ast-parser.interface.ts +21 -0
  167. package/src/core/ast/path-resolver.ts +61 -0
  168. package/src/core/ast/tree-sitter-parser.ts +158 -0
  169. package/src/core/config.ts +125 -0
  170. package/src/core/diagram.ts +129 -0
  171. package/src/core/i18n.ts +64 -0
  172. package/src/core/locales/en.ts +340 -0
  173. package/src/core/locales/pt-BR.ts +341 -0
  174. package/src/core/locales/types.ts +95 -0
  175. package/src/core/plugin-loader.ts +80 -0
  176. package/src/core/project-summarizer.ts +42 -0
  177. package/src/core/refactor-engine.ts +112 -0
  178. package/src/core/rules/barrel-optimizer.ts +99 -0
  179. package/src/core/rules/dead-code-detector.ts +134 -0
  180. package/src/core/rules/hub-splitter.ts +135 -0
  181. package/src/core/rules/import-organizer.ts +100 -0
  182. package/src/core/rules/module-grouper.ts +133 -0
  183. package/src/core/rules-engine.ts +100 -0
  184. package/src/core/scorer.ts +181 -0
  185. package/src/core/summarizer/keyword-extractor.ts +53 -0
  186. package/src/core/summarizer/module-inferrer.ts +194 -0
  187. package/src/core/summarizer/package-reader.ts +34 -0
  188. package/src/core/summarizer/purpose-inferrer.ts +197 -0
  189. package/src/core/summarizer/readme-reader.ts +24 -0
  190. package/src/core/types/architect-rules.ts +29 -0
  191. package/src/core/types/core.ts +94 -0
  192. package/src/core/types/infrastructure.ts +41 -0
  193. package/src/core/types/plugin.ts +19 -0
  194. package/src/core/types/rules.ts +51 -0
  195. package/src/core/types/summarizer.ts +8 -0
  196. package/src/infrastructure/git-cache.ts +52 -0
  197. package/src/infrastructure/git-history.ts +496 -0
  198. package/src/infrastructure/logger.ts +68 -0
  199. package/src/infrastructure/scanner.ts +349 -0
  200. package/tests/analyzers-integration.test.ts +174 -0
  201. package/tests/anti-patterns.test.ts +95 -0
  202. package/tests/ast-parser.test.ts +102 -0
  203. package/tests/fixtures/monorepo/package.json +6 -0
  204. package/tests/fixtures/monorepo/packages/app/package.json +12 -0
  205. package/tests/fixtures/monorepo/packages/app/src/index.ts +6 -0
  206. package/tests/fixtures/monorepo/packages/core/package.json +7 -0
  207. package/tests/fixtures/monorepo/packages/core/src/index.ts +7 -0
  208. package/tests/forecast.test.ts +504 -0
  209. package/tests/git-history.test.ts +254 -0
  210. package/tests/i18n.test.ts +47 -0
  211. package/tests/monorepo-scan.test.ts +170 -0
  212. package/tests/plugin-loader.test.ts +40 -0
  213. package/tests/rules-engine.test.ts +131 -0
  214. package/tests/scanner.test.ts +54 -0
  215. package/tests/scorer.test.ts +675 -0
  216. package/tests/temporal-scorer.test.ts +306 -0
  217. 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
+ }