baseguard 1.0.2 → 1.0.4

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 (169) hide show
  1. package/.baseguardrc.example.json +63 -63
  2. package/.eslintrc.json +24 -24
  3. package/.prettierrc +7 -7
  4. package/CHANGELOG.md +195 -195
  5. package/DEPLOYMENT.md +624 -624
  6. package/DEPLOYMENT_CHECKLIST.md +239 -239
  7. package/DEPLOYMENT_SUMMARY_v1.0.2.md +202 -202
  8. package/QUICK_START.md +134 -134
  9. package/README.md +488 -488
  10. package/RELEASE_NOTES_v1.0.2.md +434 -434
  11. package/bin/base.js +628 -613
  12. package/dist/ai/fix-manager.d.ts.map +1 -1
  13. package/dist/ai/fix-manager.js +1 -1
  14. package/dist/ai/fix-manager.js.map +1 -1
  15. package/dist/ai/gemini-analyzer.d.ts.map +1 -1
  16. package/dist/ai/gemini-analyzer.js +29 -35
  17. package/dist/ai/gemini-analyzer.js.map +1 -1
  18. package/dist/ai/gemini-code-fixer.d.ts.map +1 -1
  19. package/dist/ai/gemini-code-fixer.js +58 -58
  20. package/dist/ai/gemini-code-fixer.js.map +1 -1
  21. package/dist/ai/jules-implementer.d.ts +3 -0
  22. package/dist/ai/jules-implementer.d.ts.map +1 -1
  23. package/dist/ai/jules-implementer.js +63 -32
  24. package/dist/ai/jules-implementer.js.map +1 -1
  25. package/dist/ai/unified-code-fixer.js.map +1 -1
  26. package/dist/commands/check.d.ts.map +1 -1
  27. package/dist/commands/check.js +1 -1
  28. package/dist/commands/check.js.map +1 -1
  29. package/dist/commands/config.js +2 -1
  30. package/dist/commands/config.js.map +1 -1
  31. package/dist/commands/fix.d.ts.map +1 -1
  32. package/dist/commands/fix.js +44 -15
  33. package/dist/commands/fix.js.map +1 -1
  34. package/dist/core/api-key-manager.js +2 -2
  35. package/dist/core/api-key-manager.js.map +1 -1
  36. package/dist/core/baseguard.d.ts +1 -0
  37. package/dist/core/baseguard.d.ts.map +1 -1
  38. package/dist/core/baseguard.js +13 -10
  39. package/dist/core/baseguard.js.map +1 -1
  40. package/dist/core/baseline-checker.d.ts.map +1 -1
  41. package/dist/core/baseline-checker.js +2 -1
  42. package/dist/core/baseline-checker.js.map +1 -1
  43. package/dist/core/configuration-recovery.d.ts.map +1 -1
  44. package/dist/core/configuration-recovery.js +1 -1
  45. package/dist/core/configuration-recovery.js.map +1 -1
  46. package/dist/core/debug-logger.d.ts.map +1 -1
  47. package/dist/core/debug-logger.js +1 -1
  48. package/dist/core/debug-logger.js.map +1 -1
  49. package/dist/core/error-handler.d.ts.map +1 -1
  50. package/dist/core/error-handler.js +2 -1
  51. package/dist/core/error-handler.js.map +1 -1
  52. package/dist/core/gitignore-manager.js +5 -5
  53. package/dist/core/graceful-degradation-manager.d.ts.map +1 -1
  54. package/dist/core/graceful-degradation-manager.js +16 -16
  55. package/dist/core/graceful-degradation-manager.js.map +1 -1
  56. package/dist/core/lazy-loader.d.ts.map +1 -1
  57. package/dist/core/lazy-loader.js +9 -2
  58. package/dist/core/lazy-loader.js.map +1 -1
  59. package/dist/core/memory-manager.d.ts +0 -3
  60. package/dist/core/memory-manager.d.ts.map +1 -1
  61. package/dist/core/memory-manager.js.map +1 -1
  62. package/dist/core/parser-worker.d.ts +2 -0
  63. package/dist/core/parser-worker.d.ts.map +1 -0
  64. package/dist/core/parser-worker.js +19 -0
  65. package/dist/core/parser-worker.js.map +1 -0
  66. package/dist/core/startup-optimizer.d.ts +2 -0
  67. package/dist/core/startup-optimizer.d.ts.map +1 -1
  68. package/dist/core/startup-optimizer.js +19 -12
  69. package/dist/core/startup-optimizer.js.map +1 -1
  70. package/dist/core/system-error-handler.d.ts.map +1 -1
  71. package/dist/core/system-error-handler.js +18 -11
  72. package/dist/core/system-error-handler.js.map +1 -1
  73. package/dist/git/automation-engine.d.ts.map +1 -1
  74. package/dist/git/automation-engine.js +5 -4
  75. package/dist/git/automation-engine.js.map +1 -1
  76. package/dist/git/github-manager.d.ts.map +1 -1
  77. package/dist/git/github-manager.js.map +1 -1
  78. package/dist/git/hook-manager.js +5 -5
  79. package/dist/git/hook-manager.js.map +1 -1
  80. package/dist/parsers/parser-manager.d.ts.map +1 -1
  81. package/dist/parsers/parser-manager.js +1 -1
  82. package/dist/parsers/parser-manager.js.map +1 -1
  83. package/dist/parsers/svelte-parser.js +1 -1
  84. package/dist/parsers/svelte-parser.js.map +1 -1
  85. package/dist/parsers/vanilla-parser.d.ts.map +1 -1
  86. package/dist/parsers/vanilla-parser.js.map +1 -1
  87. package/dist/parsers/vue-parser.d.ts.map +1 -1
  88. package/dist/parsers/vue-parser.js.map +1 -1
  89. package/dist/ui/components.d.ts +1 -1
  90. package/dist/ui/components.d.ts.map +1 -1
  91. package/dist/ui/components.js +11 -11
  92. package/dist/ui/components.js.map +1 -1
  93. package/dist/ui/terminal-header.js +14 -14
  94. package/package.json +105 -105
  95. package/src/ai/__tests__/gemini-analyzer.test.ts +180 -180
  96. package/src/ai/agentkit-orchestrator.ts +533 -533
  97. package/src/ai/fix-manager.ts +362 -362
  98. package/src/ai/gemini-analyzer.ts +665 -671
  99. package/src/ai/gemini-code-fixer.ts +539 -540
  100. package/src/ai/index.ts +3 -3
  101. package/src/ai/jules-implementer.ts +504 -460
  102. package/src/ai/unified-code-fixer.ts +347 -347
  103. package/src/commands/automation.ts +343 -343
  104. package/src/commands/check.ts +298 -299
  105. package/src/commands/config.ts +584 -583
  106. package/src/commands/fix.ts +264 -238
  107. package/src/commands/index.ts +6 -6
  108. package/src/commands/init.ts +155 -155
  109. package/src/commands/status.ts +306 -306
  110. package/src/core/api-key-manager.ts +298 -298
  111. package/src/core/baseguard.ts +757 -756
  112. package/src/core/baseline-checker.ts +564 -563
  113. package/src/core/cache-manager.ts +271 -271
  114. package/src/core/configuration-recovery.ts +672 -673
  115. package/src/core/configuration.ts +595 -595
  116. package/src/core/debug-logger.ts +590 -590
  117. package/src/core/directory-filter.ts +420 -420
  118. package/src/core/error-handler.ts +518 -517
  119. package/src/core/file-processor.ts +337 -337
  120. package/src/core/gitignore-manager.ts +168 -168
  121. package/src/core/graceful-degradation-manager.ts +596 -596
  122. package/src/core/index.ts +16 -16
  123. package/src/core/lazy-loader.ts +317 -307
  124. package/src/core/memory-manager.ts +290 -295
  125. package/src/core/parser-worker.ts +33 -0
  126. package/src/core/startup-optimizer.ts +246 -243
  127. package/src/core/system-error-handler.ts +755 -750
  128. package/src/git/automation-engine.ts +361 -361
  129. package/src/git/github-manager.ts +190 -192
  130. package/src/git/hook-manager.ts +210 -210
  131. package/src/git/index.ts +3 -3
  132. package/src/index.ts +7 -7
  133. package/src/parsers/feature-validator.ts +558 -558
  134. package/src/parsers/index.ts +7 -7
  135. package/src/parsers/parser-manager.ts +418 -419
  136. package/src/parsers/parser.ts +25 -25
  137. package/src/parsers/react-parser-optimized.ts +160 -160
  138. package/src/parsers/react-parser.ts +358 -358
  139. package/src/parsers/svelte-parser.ts +510 -510
  140. package/src/parsers/vanilla-parser.ts +685 -686
  141. package/src/parsers/vue-parser.ts +476 -478
  142. package/src/types/index.ts +95 -95
  143. package/src/ui/components.ts +567 -567
  144. package/src/ui/help.ts +192 -192
  145. package/src/ui/index.ts +3 -3
  146. package/src/ui/prompts.ts +680 -680
  147. package/src/ui/terminal-header.ts +58 -58
  148. package/test-build.js +40 -40
  149. package/test-config-commands.js +55 -55
  150. package/test-header-simple.js +32 -32
  151. package/test-terminal-header.js +11 -11
  152. package/test-ui.js +28 -28
  153. package/tests/e2e/baseguard.e2e.test.ts +515 -515
  154. package/tests/e2e/cross-platform.e2e.test.ts +419 -419
  155. package/tests/e2e/git-integration.e2e.test.ts +486 -486
  156. package/tests/fixtures/react-project/package.json +13 -13
  157. package/tests/fixtures/react-project/src/App.css +75 -75
  158. package/tests/fixtures/react-project/src/App.tsx +76 -76
  159. package/tests/fixtures/svelte-project/package.json +10 -10
  160. package/tests/fixtures/svelte-project/src/App.svelte +368 -368
  161. package/tests/fixtures/vanilla-project/index.html +75 -75
  162. package/tests/fixtures/vanilla-project/script.js +330 -330
  163. package/tests/fixtures/vanilla-project/styles.css +358 -358
  164. package/tests/fixtures/vue-project/package.json +11 -11
  165. package/tests/fixtures/vue-project/src/App.vue +215 -215
  166. package/tsconfig.json +34 -34
  167. package/vitest.config.ts +11 -11
  168. package/dist/terminal-header.d.ts +0 -12
  169. package/dist/terminal-header.js +0 -45
@@ -1,421 +1,421 @@
1
- import { readdir, stat } from 'fs/promises';
2
- import { join, basename, extname } from 'path';
3
- import { glob } from 'glob';
4
-
5
- /**
6
- * Smart directory filtering to skip node_modules and build outputs
7
- */
8
- export class DirectoryFilter {
9
- private readonly defaultExcludePatterns: string[] = [
10
- 'node_modules',
11
- '.git',
12
- '.svn',
13
- '.hg',
14
- 'dist',
15
- 'build',
16
- 'out',
17
- '.next',
18
- '.nuxt',
19
- '.output',
20
- 'coverage',
21
- '.nyc_output',
22
- '.cache',
23
- '.parcel-cache',
24
- '.vscode',
25
- '.idea',
26
- '*.log',
27
- '*.tmp',
28
- '*.temp',
29
- '.DS_Store',
30
- 'Thumbs.db'
31
- ];
32
-
33
- private readonly defaultIncludeExtensions: string[] = [
34
- '.js',
35
- '.jsx',
36
- '.ts',
37
- '.tsx',
38
- '.vue',
39
- '.svelte',
40
- '.css',
41
- '.scss',
42
- '.sass',
43
- '.less',
44
- '.html',
45
- '.htm'
46
- ];
47
-
48
- private readonly excludePatterns: Set<string>;
49
- private readonly includeExtensions: Set<string>;
50
- private readonly maxDepth: number;
51
- private readonly maxFiles: number;
52
-
53
- constructor(options: {
54
- excludePatterns?: string[];
55
- includeExtensions?: string[];
56
- maxDepth?: number;
57
- maxFiles?: number;
58
- } = {}) {
59
- this.excludePatterns = new Set([
60
- ...this.defaultExcludePatterns,
61
- ...(options.excludePatterns || [])
62
- ]);
63
-
64
- this.includeExtensions = new Set(
65
- options.includeExtensions || this.defaultIncludeExtensions
66
- );
67
-
68
- this.maxDepth = options.maxDepth || 10;
69
- this.maxFiles = options.maxFiles || 10000;
70
- }
71
-
72
- /**
73
- * Find files recursively with smart filtering
74
- */
75
- async findFiles(
76
- directories: string[],
77
- options: {
78
- patterns?: string[];
79
- excludePatterns?: string[];
80
- includeHidden?: boolean;
81
- } = {}
82
- ): Promise<string[]> {
83
- const {
84
- patterns = [],
85
- excludePatterns = [],
86
- includeHidden = false
87
- } = options;
88
-
89
- const allExcludePatterns = new Set([
90
- ...this.excludePatterns,
91
- ...excludePatterns
92
- ]);
93
-
94
- const files: string[] = [];
95
- const processedDirs = new Set<string>();
96
-
97
- for (const directory of directories) {
98
- try {
99
- await this.findFilesRecursive(
100
- directory,
101
- files,
102
- allExcludePatterns,
103
- patterns,
104
- includeHidden,
105
- 0,
106
- processedDirs
107
- );
108
- } catch (error) {
109
- console.warn(`Could not scan directory ${directory}: ${error instanceof Error ? error.message : 'Unknown error'}`);
110
- }
111
-
112
- // Stop if we've found too many files
113
- if (files.length >= this.maxFiles) {
114
- console.warn(`Reached maximum file limit (${this.maxFiles}). Some files may be skipped.`);
115
- break;
116
- }
117
- }
118
-
119
- return files.slice(0, this.maxFiles);
120
- }
121
-
122
- /**
123
- * Find files using glob patterns (faster for specific patterns)
124
- */
125
- async findFilesWithGlob(
126
- patterns: string[],
127
- options: {
128
- cwd?: string;
129
- excludePatterns?: string[];
130
- } = {}
131
- ): Promise<string[]> {
132
- const { cwd = process.cwd(), excludePatterns = [] } = options;
133
-
134
- try {
135
- const allFiles = await glob(patterns, {
136
- cwd,
137
- ignore: [
138
- ...Array.from(this.excludePatterns),
139
- ...excludePatterns
140
- ],
141
- absolute: true,
142
- nodir: true
143
- });
144
-
145
- // Filter by supported extensions
146
- return allFiles.filter(file => this.isSupportedFile(file));
147
- } catch (error) {
148
- console.error(`Glob search failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
149
- return [];
150
- }
151
- }
152
-
153
- /**
154
- * Check if a file should be processed
155
- */
156
- isSupportedFile(filePath: string): boolean {
157
- const extension = extname(filePath).toLowerCase();
158
- return this.includeExtensions.has(extension);
159
- }
160
-
161
- /**
162
- * Check if a directory should be excluded
163
- */
164
- isExcludedDirectory(dirPath: string): boolean {
165
- const dirName = basename(dirPath);
166
-
167
- // Check exact matches
168
- if (this.excludePatterns.has(dirName)) {
169
- return true;
170
- }
171
-
172
- // Check pattern matches
173
- for (const pattern of this.excludePatterns) {
174
- if (pattern.includes('*') || pattern.includes('?')) {
175
- // Simple glob pattern matching
176
- const regex = new RegExp(
177
- pattern
178
- .replace(/\*/g, '.*')
179
- .replace(/\?/g, '.')
180
- );
181
- if (regex.test(dirName)) {
182
- return true;
183
- }
184
- }
185
- }
186
-
187
- return false;
188
- }
189
-
190
- /**
191
- * Get file statistics for a directory
192
- */
193
- async getDirectoryStats(directoryPath: string): Promise<{
194
- totalFiles: number;
195
- supportedFiles: number;
196
- directories: number;
197
- excludedDirectories: number;
198
- largestFiles: Array<{ path: string; size: number }>;
199
- }> {
200
- const stats = {
201
- totalFiles: 0,
202
- supportedFiles: 0,
203
- directories: 0,
204
- excludedDirectories: 0,
205
- largestFiles: [] as Array<{ path: string; size: number }>
206
- };
207
-
208
- try {
209
- await this.collectStats(directoryPath, stats, 0);
210
- } catch (error) {
211
- console.warn(`Could not collect stats for ${directoryPath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
212
- }
213
-
214
- // Sort largest files
215
- stats.largestFiles.sort((a, b) => b.size - a.size);
216
- stats.largestFiles = stats.largestFiles.slice(0, 10);
217
-
218
- return stats;
219
- }
220
-
221
- /**
222
- * Recursive file finding with smart filtering
223
- */
224
- private async findFilesRecursive(
225
- currentPath: string,
226
- results: string[],
227
- excludePatterns: Set<string>,
228
- includePatterns: string[],
229
- includeHidden: boolean,
230
- currentDepth: number,
231
- processedDirs: Set<string>
232
- ): Promise<void> {
233
- // Prevent infinite loops with symlinks
234
- if (processedDirs.has(currentPath)) {
235
- return;
236
- }
237
- processedDirs.add(currentPath);
238
-
239
- // Check depth limit
240
- if (currentDepth >= this.maxDepth) {
241
- return;
242
- }
243
-
244
- // Check file limit
245
- if (results.length >= this.maxFiles) {
246
- return;
247
- }
248
-
249
- try {
250
- const entries = await readdir(currentPath);
251
-
252
- for (const entry of entries) {
253
- // Skip hidden files/directories unless explicitly included
254
- if (!includeHidden && entry.startsWith('.')) {
255
- continue;
256
- }
257
-
258
- const fullPath = join(currentPath, entry);
259
-
260
- try {
261
- const stats = await stat(fullPath);
262
-
263
- if (stats.isDirectory()) {
264
- // Check if directory should be excluded
265
- if (this.isExcludedDirectory(fullPath)) {
266
- continue;
267
- }
268
-
269
- // Recurse into directory
270
- await this.findFilesRecursive(
271
- fullPath,
272
- results,
273
- excludePatterns,
274
- includePatterns,
275
- includeHidden,
276
- currentDepth + 1,
277
- processedDirs
278
- );
279
- } else if (stats.isFile()) {
280
- // Check if file matches patterns
281
- if (this.shouldIncludeFile(fullPath, includePatterns)) {
282
- results.push(fullPath);
283
- }
284
- }
285
- } catch (error) {
286
- // Skip files/directories that can't be accessed
287
- continue;
288
- }
289
- }
290
- } catch (error) {
291
- console.warn(`Could not read directory ${currentPath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
292
- }
293
- }
294
-
295
- /**
296
- * Check if file should be included based on patterns
297
- */
298
- private shouldIncludeFile(filePath: string, includePatterns: string[]): boolean {
299
- // If no specific patterns, use extension filtering
300
- if (includePatterns.length === 0) {
301
- return this.isSupportedFile(filePath);
302
- }
303
-
304
- // Check against include patterns
305
- const fileName = basename(filePath);
306
- return includePatterns.some(pattern => {
307
- if (pattern.includes('*') || pattern.includes('?')) {
308
- const regex = new RegExp(
309
- pattern
310
- .replace(/\*/g, '.*')
311
- .replace(/\?/g, '.')
312
- );
313
- return regex.test(fileName);
314
- }
315
- return fileName === pattern;
316
- });
317
- }
318
-
319
- /**
320
- * Collect directory statistics recursively
321
- */
322
- private async collectStats(
323
- currentPath: string,
324
- stats: any,
325
- currentDepth: number
326
- ): Promise<void> {
327
- if (currentDepth >= this.maxDepth) {
328
- return;
329
- }
330
-
331
- try {
332
- const entries = await readdir(currentPath);
333
-
334
- for (const entry of entries) {
335
- const fullPath = join(currentPath, entry);
336
-
337
- try {
338
- const fileStat = await stat(fullPath);
339
-
340
- if (fileStat.isDirectory()) {
341
- stats.directories++;
342
-
343
- if (this.isExcludedDirectory(fullPath)) {
344
- stats.excludedDirectories++;
345
- continue;
346
- }
347
-
348
- await this.collectStats(fullPath, stats, currentDepth + 1);
349
- } else if (fileStat.isFile()) {
350
- stats.totalFiles++;
351
-
352
- if (this.isSupportedFile(fullPath)) {
353
- stats.supportedFiles++;
354
- }
355
-
356
- // Track largest files
357
- if (stats.largestFiles.length < 100) {
358
- stats.largestFiles.push({
359
- path: fullPath,
360
- size: fileStat.size
361
- });
362
- }
363
- }
364
- } catch (error) {
365
- // Skip files/directories that can't be accessed
366
- continue;
367
- }
368
- }
369
- } catch (error) {
370
- // Skip directories that can't be read
371
- }
372
- }
373
-
374
- /**
375
- * Get supported extensions
376
- */
377
- getSupportedExtensions(): string[] {
378
- return Array.from(this.includeExtensions);
379
- }
380
-
381
- /**
382
- * Get exclude patterns
383
- */
384
- getExcludePatterns(): string[] {
385
- return Array.from(this.excludePatterns);
386
- }
387
-
388
- /**
389
- * Add custom exclude pattern
390
- */
391
- addExcludePattern(pattern: string): void {
392
- this.excludePatterns.add(pattern);
393
- }
394
-
395
- /**
396
- * Remove exclude pattern
397
- */
398
- removeExcludePattern(pattern: string): void {
399
- this.excludePatterns.delete(pattern);
400
- }
401
-
402
- /**
403
- * Add supported extension
404
- */
405
- addSupportedExtension(extension: string): void {
406
- if (!extension.startsWith('.')) {
407
- extension = '.' + extension;
408
- }
409
- this.includeExtensions.add(extension.toLowerCase());
410
- }
411
-
412
- /**
413
- * Remove supported extension
414
- */
415
- removeSupportedExtension(extension: string): void {
416
- if (!extension.startsWith('.')) {
417
- extension = '.' + extension;
418
- }
419
- this.includeExtensions.delete(extension.toLowerCase());
420
- }
1
+ import { readdir, stat } from 'fs/promises';
2
+ import { join, basename, extname } from 'path';
3
+ import { glob } from 'glob';
4
+
5
+ /**
6
+ * Smart directory filtering to skip node_modules and build outputs
7
+ */
8
+ export class DirectoryFilter {
9
+ private readonly defaultExcludePatterns: string[] = [
10
+ 'node_modules',
11
+ '.git',
12
+ '.svn',
13
+ '.hg',
14
+ 'dist',
15
+ 'build',
16
+ 'out',
17
+ '.next',
18
+ '.nuxt',
19
+ '.output',
20
+ 'coverage',
21
+ '.nyc_output',
22
+ '.cache',
23
+ '.parcel-cache',
24
+ '.vscode',
25
+ '.idea',
26
+ '*.log',
27
+ '*.tmp',
28
+ '*.temp',
29
+ '.DS_Store',
30
+ 'Thumbs.db'
31
+ ];
32
+
33
+ private readonly defaultIncludeExtensions: string[] = [
34
+ '.js',
35
+ '.jsx',
36
+ '.ts',
37
+ '.tsx',
38
+ '.vue',
39
+ '.svelte',
40
+ '.css',
41
+ '.scss',
42
+ '.sass',
43
+ '.less',
44
+ '.html',
45
+ '.htm'
46
+ ];
47
+
48
+ private readonly excludePatterns: Set<string>;
49
+ private readonly includeExtensions: Set<string>;
50
+ private readonly maxDepth: number;
51
+ private readonly maxFiles: number;
52
+
53
+ constructor(options: {
54
+ excludePatterns?: string[];
55
+ includeExtensions?: string[];
56
+ maxDepth?: number;
57
+ maxFiles?: number;
58
+ } = {}) {
59
+ this.excludePatterns = new Set([
60
+ ...this.defaultExcludePatterns,
61
+ ...(options.excludePatterns || [])
62
+ ]);
63
+
64
+ this.includeExtensions = new Set(
65
+ options.includeExtensions || this.defaultIncludeExtensions
66
+ );
67
+
68
+ this.maxDepth = options.maxDepth || 10;
69
+ this.maxFiles = options.maxFiles || 10000;
70
+ }
71
+
72
+ /**
73
+ * Find files recursively with smart filtering
74
+ */
75
+ async findFiles(
76
+ directories: string[],
77
+ options: {
78
+ patterns?: string[];
79
+ excludePatterns?: string[];
80
+ includeHidden?: boolean;
81
+ } = {}
82
+ ): Promise<string[]> {
83
+ const {
84
+ patterns = [],
85
+ excludePatterns = [],
86
+ includeHidden = false
87
+ } = options;
88
+
89
+ const allExcludePatterns = new Set([
90
+ ...this.excludePatterns,
91
+ ...excludePatterns
92
+ ]);
93
+
94
+ const files: string[] = [];
95
+ const processedDirs = new Set<string>();
96
+
97
+ for (const directory of directories) {
98
+ try {
99
+ await this.findFilesRecursive(
100
+ directory,
101
+ files,
102
+ allExcludePatterns,
103
+ patterns,
104
+ includeHidden,
105
+ 0,
106
+ processedDirs
107
+ );
108
+ } catch (error) {
109
+ console.warn(`Could not scan directory ${directory}: ${error instanceof Error ? error.message : 'Unknown error'}`);
110
+ }
111
+
112
+ // Stop if we've found too many files
113
+ if (files.length >= this.maxFiles) {
114
+ console.warn(`Reached maximum file limit (${this.maxFiles}). Some files may be skipped.`);
115
+ break;
116
+ }
117
+ }
118
+
119
+ return files.slice(0, this.maxFiles);
120
+ }
121
+
122
+ /**
123
+ * Find files using glob patterns (faster for specific patterns)
124
+ */
125
+ async findFilesWithGlob(
126
+ patterns: string[],
127
+ options: {
128
+ cwd?: string;
129
+ excludePatterns?: string[];
130
+ } = {}
131
+ ): Promise<string[]> {
132
+ const { cwd = process.cwd(), excludePatterns = [] } = options;
133
+
134
+ try {
135
+ const allFiles = await glob(patterns, {
136
+ cwd,
137
+ ignore: [
138
+ ...Array.from(this.excludePatterns),
139
+ ...excludePatterns
140
+ ],
141
+ absolute: true,
142
+ nodir: true
143
+ });
144
+
145
+ // Filter by supported extensions
146
+ return allFiles.filter(file => this.isSupportedFile(file));
147
+ } catch (error) {
148
+ console.error(`Glob search failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
149
+ return [];
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Check if a file should be processed
155
+ */
156
+ isSupportedFile(filePath: string): boolean {
157
+ const extension = extname(filePath).toLowerCase();
158
+ return this.includeExtensions.has(extension);
159
+ }
160
+
161
+ /**
162
+ * Check if a directory should be excluded
163
+ */
164
+ isExcludedDirectory(dirPath: string): boolean {
165
+ const dirName = basename(dirPath);
166
+
167
+ // Check exact matches
168
+ if (this.excludePatterns.has(dirName)) {
169
+ return true;
170
+ }
171
+
172
+ // Check pattern matches
173
+ for (const pattern of this.excludePatterns) {
174
+ if (pattern.includes('*') || pattern.includes('?')) {
175
+ // Simple glob pattern matching
176
+ const regex = new RegExp(
177
+ pattern
178
+ .replace(/\*/g, '.*')
179
+ .replace(/\?/g, '.')
180
+ );
181
+ if (regex.test(dirName)) {
182
+ return true;
183
+ }
184
+ }
185
+ }
186
+
187
+ return false;
188
+ }
189
+
190
+ /**
191
+ * Get file statistics for a directory
192
+ */
193
+ async getDirectoryStats(directoryPath: string): Promise<{
194
+ totalFiles: number;
195
+ supportedFiles: number;
196
+ directories: number;
197
+ excludedDirectories: number;
198
+ largestFiles: Array<{ path: string; size: number }>;
199
+ }> {
200
+ const stats = {
201
+ totalFiles: 0,
202
+ supportedFiles: 0,
203
+ directories: 0,
204
+ excludedDirectories: 0,
205
+ largestFiles: [] as Array<{ path: string; size: number }>
206
+ };
207
+
208
+ try {
209
+ await this.collectStats(directoryPath, stats, 0);
210
+ } catch (error) {
211
+ console.warn(`Could not collect stats for ${directoryPath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
212
+ }
213
+
214
+ // Sort largest files
215
+ stats.largestFiles.sort((a, b) => b.size - a.size);
216
+ stats.largestFiles = stats.largestFiles.slice(0, 10);
217
+
218
+ return stats;
219
+ }
220
+
221
+ /**
222
+ * Recursive file finding with smart filtering
223
+ */
224
+ private async findFilesRecursive(
225
+ currentPath: string,
226
+ results: string[],
227
+ excludePatterns: Set<string>,
228
+ includePatterns: string[],
229
+ includeHidden: boolean,
230
+ currentDepth: number,
231
+ processedDirs: Set<string>
232
+ ): Promise<void> {
233
+ // Prevent infinite loops with symlinks
234
+ if (processedDirs.has(currentPath)) {
235
+ return;
236
+ }
237
+ processedDirs.add(currentPath);
238
+
239
+ // Check depth limit
240
+ if (currentDepth >= this.maxDepth) {
241
+ return;
242
+ }
243
+
244
+ // Check file limit
245
+ if (results.length >= this.maxFiles) {
246
+ return;
247
+ }
248
+
249
+ try {
250
+ const entries = await readdir(currentPath);
251
+
252
+ for (const entry of entries) {
253
+ // Skip hidden files/directories unless explicitly included
254
+ if (!includeHidden && entry.startsWith('.')) {
255
+ continue;
256
+ }
257
+
258
+ const fullPath = join(currentPath, entry);
259
+
260
+ try {
261
+ const stats = await stat(fullPath);
262
+
263
+ if (stats.isDirectory()) {
264
+ // Check if directory should be excluded
265
+ if (this.isExcludedDirectory(fullPath)) {
266
+ continue;
267
+ }
268
+
269
+ // Recurse into directory
270
+ await this.findFilesRecursive(
271
+ fullPath,
272
+ results,
273
+ excludePatterns,
274
+ includePatterns,
275
+ includeHidden,
276
+ currentDepth + 1,
277
+ processedDirs
278
+ );
279
+ } else if (stats.isFile()) {
280
+ // Check if file matches patterns
281
+ if (this.shouldIncludeFile(fullPath, includePatterns)) {
282
+ results.push(fullPath);
283
+ }
284
+ }
285
+ } catch (error) {
286
+ // Skip files/directories that can't be accessed
287
+ continue;
288
+ }
289
+ }
290
+ } catch (error) {
291
+ console.warn(`Could not read directory ${currentPath}: ${error instanceof Error ? error.message : 'Unknown error'}`);
292
+ }
293
+ }
294
+
295
+ /**
296
+ * Check if file should be included based on patterns
297
+ */
298
+ private shouldIncludeFile(filePath: string, includePatterns: string[]): boolean {
299
+ // If no specific patterns, use extension filtering
300
+ if (includePatterns.length === 0) {
301
+ return this.isSupportedFile(filePath);
302
+ }
303
+
304
+ // Check against include patterns
305
+ const fileName = basename(filePath);
306
+ return includePatterns.some(pattern => {
307
+ if (pattern.includes('*') || pattern.includes('?')) {
308
+ const regex = new RegExp(
309
+ pattern
310
+ .replace(/\*/g, '.*')
311
+ .replace(/\?/g, '.')
312
+ );
313
+ return regex.test(fileName);
314
+ }
315
+ return fileName === pattern;
316
+ });
317
+ }
318
+
319
+ /**
320
+ * Collect directory statistics recursively
321
+ */
322
+ private async collectStats(
323
+ currentPath: string,
324
+ stats: any,
325
+ currentDepth: number
326
+ ): Promise<void> {
327
+ if (currentDepth >= this.maxDepth) {
328
+ return;
329
+ }
330
+
331
+ try {
332
+ const entries = await readdir(currentPath);
333
+
334
+ for (const entry of entries) {
335
+ const fullPath = join(currentPath, entry);
336
+
337
+ try {
338
+ const fileStat = await stat(fullPath);
339
+
340
+ if (fileStat.isDirectory()) {
341
+ stats.directories++;
342
+
343
+ if (this.isExcludedDirectory(fullPath)) {
344
+ stats.excludedDirectories++;
345
+ continue;
346
+ }
347
+
348
+ await this.collectStats(fullPath, stats, currentDepth + 1);
349
+ } else if (fileStat.isFile()) {
350
+ stats.totalFiles++;
351
+
352
+ if (this.isSupportedFile(fullPath)) {
353
+ stats.supportedFiles++;
354
+ }
355
+
356
+ // Track largest files
357
+ if (stats.largestFiles.length < 100) {
358
+ stats.largestFiles.push({
359
+ path: fullPath,
360
+ size: fileStat.size
361
+ });
362
+ }
363
+ }
364
+ } catch (error) {
365
+ // Skip files/directories that can't be accessed
366
+ continue;
367
+ }
368
+ }
369
+ } catch (error) {
370
+ // Skip directories that can't be read
371
+ }
372
+ }
373
+
374
+ /**
375
+ * Get supported extensions
376
+ */
377
+ getSupportedExtensions(): string[] {
378
+ return Array.from(this.includeExtensions);
379
+ }
380
+
381
+ /**
382
+ * Get exclude patterns
383
+ */
384
+ getExcludePatterns(): string[] {
385
+ return Array.from(this.excludePatterns);
386
+ }
387
+
388
+ /**
389
+ * Add custom exclude pattern
390
+ */
391
+ addExcludePattern(pattern: string): void {
392
+ this.excludePatterns.add(pattern);
393
+ }
394
+
395
+ /**
396
+ * Remove exclude pattern
397
+ */
398
+ removeExcludePattern(pattern: string): void {
399
+ this.excludePatterns.delete(pattern);
400
+ }
401
+
402
+ /**
403
+ * Add supported extension
404
+ */
405
+ addSupportedExtension(extension: string): void {
406
+ if (!extension.startsWith('.')) {
407
+ extension = '.' + extension;
408
+ }
409
+ this.includeExtensions.add(extension.toLowerCase());
410
+ }
411
+
412
+ /**
413
+ * Remove supported extension
414
+ */
415
+ removeSupportedExtension(extension: string): void {
416
+ if (!extension.startsWith('.')) {
417
+ extension = '.' + extension;
418
+ }
419
+ this.includeExtensions.delete(extension.toLowerCase());
420
+ }
421
421
  }