@morojs/moro 1.5.14 → 1.5.16

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.
@@ -1,14 +1,16 @@
1
1
  // Auto-discovery system for Moro modules
2
2
  import { readdirSync, statSync } from 'fs';
3
- import { join, extname } from 'path';
3
+ import { join, extname, relative, isAbsolute } from 'path';
4
4
  import { ModuleConfig } from '../../types/module';
5
5
  import { DiscoveryOptions } from '../../types/discovery';
6
+ import { ModuleDefaultsConfig } from '../../types/config';
6
7
  import { createFrameworkLogger } from '../logger';
7
8
 
8
9
  export class ModuleDiscovery {
9
10
  private baseDir: string;
10
11
  private options: DiscoveryOptions;
11
12
  private discoveryLogger = createFrameworkLogger('MODULE_DISCOVERY');
13
+ private watchers: any[] = []; // Store file watchers for cleanup
12
14
 
13
15
  constructor(baseDir: string = process.cwd(), options: DiscoveryOptions = {}) {
14
16
  this.baseDir = baseDir;
@@ -177,22 +179,483 @@ export class ModuleDiscovery {
177
179
  );
178
180
  }
179
181
 
182
+ // Enhanced auto-discovery with advanced configuration
183
+ async discoverModulesAdvanced(
184
+ config: ModuleDefaultsConfig['autoDiscovery']
185
+ ): Promise<ModuleConfig[]> {
186
+ if (!config.enabled) {
187
+ return [];
188
+ }
189
+
190
+ const allModules: ModuleConfig[] = [];
191
+
192
+ // Discover from all configured paths
193
+ for (const searchPath of config.paths) {
194
+ const modules = await this.discoverFromPath(searchPath, config);
195
+ allModules.push(...modules);
196
+ }
197
+
198
+ // Remove duplicates based on name@version
199
+ const uniqueModules = this.deduplicateModules(allModules);
200
+
201
+ // Sort modules based on load order strategy
202
+ const sortedModules = this.sortModules(uniqueModules, config.loadOrder);
203
+
204
+ // Validate dependencies if using dependency order
205
+ if (config.loadOrder === 'dependency') {
206
+ return this.resolveDependencyOrder(sortedModules);
207
+ }
208
+
209
+ return sortedModules;
210
+ }
211
+
212
+ // Discover modules from a specific path with advanced filtering
213
+ private async discoverFromPath(
214
+ searchPath: string,
215
+ config: ModuleDefaultsConfig['autoDiscovery']
216
+ ): Promise<ModuleConfig[]> {
217
+ const modules: ModuleConfig[] = [];
218
+ const fullPath = join(this.baseDir, searchPath);
219
+
220
+ try {
221
+ const stat = statSync(fullPath);
222
+
223
+ if (!stat.isDirectory()) {
224
+ return modules;
225
+ }
226
+ } catch (error) {
227
+ return modules;
228
+ }
229
+
230
+ try {
231
+ const files = await this.findMatchingFilesWithGlob(
232
+ fullPath,
233
+ config.patterns,
234
+ config.ignorePatterns,
235
+ config.maxDepth
236
+ );
237
+
238
+ for (const filePath of files) {
239
+ try {
240
+ // Convert relative path to absolute path for import
241
+ const absolutePath = join(this.baseDir, filePath);
242
+ const module = await this.loadModule(absolutePath);
243
+ if (module && this.validateAdvancedModule(module, config)) {
244
+ modules.push(module);
245
+ this.discoveryLogger.info(
246
+ `Auto-discovered module: ${module.name}@${module.version} from ${filePath}`
247
+ );
248
+ }
249
+ } catch (error) {
250
+ const errorMsg = error instanceof Error ? error.message : String(error);
251
+
252
+ if (config.failOnError) {
253
+ throw new Error(`Failed to load module from ${filePath}: ${errorMsg}`);
254
+ } else {
255
+ this.discoveryLogger.warn(
256
+ `Failed to load module from ${filePath}`,
257
+ 'MODULE_DISCOVERY',
258
+ {
259
+ error: errorMsg,
260
+ }
261
+ );
262
+ }
263
+ }
264
+ }
265
+ } catch (error) {
266
+ if (config.failOnError) {
267
+ throw error;
268
+ }
269
+ // Directory doesn't exist or other error, continue silently
270
+ }
271
+
272
+ return modules;
273
+ }
274
+
275
+ // Find files matching patterns with ignore support
276
+ private findMatchingFiles(
277
+ basePath: string,
278
+ config: ModuleDefaultsConfig['autoDiscovery'],
279
+ currentDepth: number = 0
280
+ ): string[] {
281
+ const files: string[] = [];
282
+
283
+ if (currentDepth >= config.maxDepth) {
284
+ return files;
285
+ }
286
+
287
+ try {
288
+ const items = readdirSync(basePath);
289
+
290
+ for (const item of items) {
291
+ const fullPath = join(basePath, item);
292
+ const relativePath = relative(this.baseDir, fullPath);
293
+
294
+ // Check ignore patterns
295
+ if (this.shouldIgnore(relativePath, config.ignorePatterns)) {
296
+ continue;
297
+ }
298
+
299
+ const stat = statSync(fullPath);
300
+
301
+ if (stat.isDirectory() && config.recursive) {
302
+ files.push(...this.findMatchingFiles(fullPath, config, currentDepth + 1));
303
+ } else if (stat.isFile()) {
304
+ // Check if file matches any pattern
305
+ if (this.matchesPatterns(relativePath, config.patterns)) {
306
+ files.push(fullPath);
307
+ }
308
+ }
309
+ }
310
+ } catch {
311
+ // Directory not accessible, skip
312
+ }
313
+
314
+ return files;
315
+ }
316
+
317
+ // Use native Node.js glob to find matching files
318
+ private async findMatchingFilesWithGlob(
319
+ searchPath: string,
320
+ patterns: string[],
321
+ ignorePatterns: string[],
322
+ maxDepth: number = 5
323
+ ): Promise<string[]> {
324
+ // Force fallback in CI environments or if Node.js version is uncertain
325
+ const isCI = process.env.CI === 'true' || process.env.GITHUB_ACTIONS === 'true';
326
+
327
+ if (isCI) {
328
+ return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
329
+ }
330
+
331
+ const allFiles: string[] = [];
332
+
333
+ try {
334
+ // Try to use native fs.glob if available (Node.js 20+)
335
+ const { glob } = await import('fs/promises');
336
+
337
+ // Check if glob is actually a function and test it
338
+ if (typeof glob !== 'function') {
339
+ return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
340
+ }
341
+
342
+ // Test glob with a simple pattern first
343
+ try {
344
+ const testIterator = glob(join(searchPath, '*'));
345
+ let testCount = 0;
346
+ for await (const _ of testIterator) {
347
+ testCount++;
348
+ if (testCount > 0) break; // Just test that it works
349
+ }
350
+ } catch (testError) {
351
+ return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
352
+ }
353
+
354
+ for (const pattern of patterns) {
355
+ const fullPattern = join(searchPath, pattern);
356
+ try {
357
+ // fs.glob returns an AsyncIterator, need to collect results
358
+ const globIterator = glob(fullPattern);
359
+ const files: string[] = [];
360
+
361
+ for await (const file of globIterator) {
362
+ const filePath = typeof file === 'string' ? file : (file as any).name || String(file);
363
+ const relativePath = relative(this.baseDir, filePath);
364
+
365
+ // Check if file should be ignored and within max depth
366
+ if (
367
+ !this.shouldIgnore(relativePath, ignorePatterns) &&
368
+ this.isWithinMaxDepth(relativePath, searchPath, maxDepth)
369
+ ) {
370
+ files.push(relativePath);
371
+ }
372
+ }
373
+
374
+ allFiles.push(...files);
375
+ } catch (error) {
376
+ // If any glob call fails, fall back to manual discovery
377
+ this.discoveryLogger.warn(`Glob pattern failed: ${pattern}`, String(error));
378
+ return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
379
+ }
380
+ }
381
+ } catch (error) {
382
+ // fs.glob not available, fall back to manual file discovery
383
+ this.discoveryLogger.debug('Native fs.glob not available, using fallback');
384
+ return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
385
+ }
386
+
387
+ return [...new Set(allFiles)]; // Remove duplicates
388
+ }
389
+
390
+ // Fallback for Node.js versions without fs.glob
391
+ private async findMatchingFilesFallback(
392
+ searchPath: string,
393
+ patterns: string[],
394
+ ignorePatterns: string[],
395
+ maxDepth: number = 5
396
+ ): Promise<string[]> {
397
+ const config = {
398
+ patterns,
399
+ ignorePatterns,
400
+ maxDepth,
401
+ recursive: true,
402
+ } as ModuleDefaultsConfig['autoDiscovery'];
403
+
404
+ // Handle both absolute and relative paths
405
+ const fullSearchPath = isAbsolute(searchPath) ? searchPath : join(this.baseDir, searchPath);
406
+
407
+ // Check if search path exists
408
+ try {
409
+ const { access } = await import('fs/promises');
410
+ await access(fullSearchPath);
411
+ } catch (e) {
412
+ return [];
413
+ }
414
+
415
+ // Get files and convert to relative paths
416
+ const files = this.findMatchingFiles(fullSearchPath, config);
417
+ const relativeFiles = files.map(file => relative(this.baseDir, file));
418
+
419
+ return relativeFiles;
420
+ }
421
+
422
+ // Simple pattern matching for fallback (basic glob support)
423
+ private matchesSimplePattern(path: string, pattern: string): boolean {
424
+ try {
425
+ // Normalize path separators
426
+ const normalizedPath = path.replace(/\\/g, '/');
427
+ const normalizedPattern = pattern.replace(/\\/g, '/');
428
+
429
+ // Convert simple glob patterns to regex
430
+ const regexPattern = normalizedPattern
431
+ .replace(/\*\*/g, '___DOUBLESTAR___') // Temporarily replace ** BEFORE escaping
432
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&') // Escape regex chars
433
+ .replace(/\\\*/g, '[^/]*') // * matches anything except /
434
+ .replace(/___DOUBLESTAR___/g, '.*') // ** matches anything including /
435
+ .replace(/\\\?/g, '[^/]') // ? matches single character except /
436
+ .replace(/\\\{([^}]+)\\\}/g, '($1)') // {ts,js} -> (ts|js)
437
+ .replace(/,/g, '|'); // Convert comma to OR
438
+
439
+ const regex = new RegExp(`^${regexPattern}$`, 'i');
440
+ const result = regex.test(normalizedPath);
441
+
442
+ return result;
443
+ } catch (error) {
444
+ this.discoveryLogger.warn(`Pattern matching error for "${pattern}": ${String(error)}`);
445
+ return false;
446
+ }
447
+ }
448
+
449
+ // Check if path should be ignored
450
+ private shouldIgnore(path: string, ignorePatterns: string[]): boolean {
451
+ return ignorePatterns.some(pattern => this.matchesSimplePattern(path, pattern));
452
+ }
453
+
454
+ // Check if path matches any of the patterns
455
+ private matchesPatterns(path: string, patterns: string[]): boolean {
456
+ return patterns.some(pattern => this.matchesSimplePattern(path, pattern));
457
+ }
458
+
459
+ // Check if file is within max depth (for glob results)
460
+ private isWithinMaxDepth(relativePath: string, searchPath: string, maxDepth: number): boolean {
461
+ // Count directory separators to determine depth
462
+ const pathFromSearch = relative(searchPath, join(this.baseDir, relativePath));
463
+ const depth = pathFromSearch.split('/').length - 1; // -1 because file itself doesn't count as depth
464
+ return depth <= maxDepth;
465
+ }
466
+
467
+ // Remove duplicate modules
468
+ private deduplicateModules(modules: ModuleConfig[]): ModuleConfig[] {
469
+ const seen = new Set<string>();
470
+ return modules.filter(module => {
471
+ const key = `${module.name}@${module.version}`;
472
+ if (seen.has(key)) {
473
+ this.discoveryLogger.warn(`Duplicate module found: ${key}`, 'MODULE_DISCOVERY');
474
+ return false;
475
+ }
476
+ seen.add(key);
477
+ return true;
478
+ });
479
+ }
480
+
481
+ // Sort modules based on strategy
482
+ private sortModules(
483
+ modules: ModuleConfig[],
484
+ strategy: ModuleDefaultsConfig['autoDiscovery']['loadOrder']
485
+ ): ModuleConfig[] {
486
+ switch (strategy) {
487
+ case 'alphabetical':
488
+ return modules.sort((a, b) => a.name.localeCompare(b.name));
489
+
490
+ case 'dependency':
491
+ // Will be handled by resolveDependencyOrder
492
+ return modules;
493
+
494
+ case 'custom':
495
+ // Allow custom sorting via module priority (if defined)
496
+ return modules.sort((a, b) => {
497
+ const aPriority = (a.config as any)?.priority || 0;
498
+ const bPriority = (b.config as any)?.priority || 0;
499
+ return bPriority - aPriority; // Higher priority first
500
+ });
501
+
502
+ default:
503
+ return modules;
504
+ }
505
+ }
506
+
507
+ // Resolve dependency order using topological sort
508
+ private resolveDependencyOrder(modules: ModuleConfig[]): ModuleConfig[] {
509
+ const moduleMap = new Map<string, ModuleConfig>();
510
+ const dependencyGraph = new Map<string, string[]>();
511
+
512
+ // Build module map and dependency graph
513
+ modules.forEach(module => {
514
+ const key = `${module.name}@${module.version}`;
515
+ moduleMap.set(key, module);
516
+ dependencyGraph.set(key, module.dependencies || []);
517
+ });
518
+
519
+ // Topological sort
520
+ const visited = new Set<string>();
521
+ const visiting = new Set<string>();
522
+ const sorted: ModuleConfig[] = [];
523
+
524
+ const visit = (moduleKey: string): void => {
525
+ if (visiting.has(moduleKey)) {
526
+ throw new Error(`Circular dependency detected involving ${moduleKey}`);
527
+ }
528
+
529
+ if (visited.has(moduleKey)) {
530
+ return;
531
+ }
532
+
533
+ visiting.add(moduleKey);
534
+
535
+ const dependencies = dependencyGraph.get(moduleKey) || [];
536
+ dependencies.forEach(dep => {
537
+ // Find the dependency in our modules
538
+ const depModule = Array.from(moduleMap.keys()).find(key =>
539
+ key.startsWith(`${dep.split('@')[0]}@`)
540
+ );
541
+ if (depModule) {
542
+ visit(depModule);
543
+ }
544
+ });
545
+
546
+ visiting.delete(moduleKey);
547
+ visited.add(moduleKey);
548
+
549
+ const module = moduleMap.get(moduleKey);
550
+ if (module) {
551
+ sorted.push(module);
552
+ }
553
+ };
554
+
555
+ // Visit all modules
556
+ Array.from(moduleMap.keys()).forEach(key => {
557
+ if (!visited.has(key)) {
558
+ visit(key);
559
+ }
560
+ });
561
+
562
+ return sorted;
563
+ }
564
+
565
+ // Enhanced module validation
566
+ private validateAdvancedModule(
567
+ module: ModuleConfig,
568
+ _config: ModuleDefaultsConfig['autoDiscovery']
569
+ ): boolean {
570
+ // Basic validation
571
+ if (!this.isValidModule(module)) {
572
+ return false;
573
+ }
574
+
575
+ // Additional validation can be added here
576
+ // For example, checking module compatibility, version constraints, etc.
577
+
578
+ return true;
579
+ }
580
+
180
581
  // Watch for module changes (for development)
181
582
  watchModules(callback: (modules: ModuleConfig[]) => void): void {
182
- const fs = require('fs');
183
- const modulePaths = this.findModuleFiles();
583
+ // Use dynamic import for fs to avoid require()
584
+ import('fs')
585
+ .then(fs => {
586
+ const modulePaths = this.findModuleFiles();
184
587
 
185
- modulePaths.forEach(path => {
186
- try {
187
- fs.watchFile(path, async () => {
188
- this.discoveryLogger.info(`Module file changed: ${path}`);
189
- const modules = await this.discoverModules();
190
- callback(modules);
588
+ modulePaths.forEach(path => {
589
+ try {
590
+ fs.watchFile(path, async () => {
591
+ this.discoveryLogger.info(`Module file changed: ${path}`);
592
+ const modules = await this.discoverModules();
593
+ callback(modules);
594
+ });
595
+ } catch {
596
+ // File watching not supported or failed
597
+ }
191
598
  });
192
- } catch {
193
- // File watching not supported or failed
599
+ })
600
+ .catch(() => {
601
+ // fs module not available
602
+ });
603
+ }
604
+
605
+ // Watch modules with advanced configuration
606
+ watchModulesAdvanced(
607
+ config: ModuleDefaultsConfig['autoDiscovery'],
608
+ callback: (modules: ModuleConfig[]) => void
609
+ ): void {
610
+ if (!config.watchForChanges) {
611
+ return;
612
+ }
613
+
614
+ import('fs')
615
+ .then(fs => {
616
+ const watchedPaths = new Set<string>();
617
+
618
+ // Watch all configured paths
619
+ config.paths.forEach(searchPath => {
620
+ const fullPath = join(this.baseDir, searchPath);
621
+
622
+ try {
623
+ if (statSync(fullPath).isDirectory() && !watchedPaths.has(fullPath)) {
624
+ watchedPaths.add(fullPath);
625
+
626
+ const watcher = fs.watch(
627
+ fullPath,
628
+ { recursive: config.recursive },
629
+ async (eventType: string, filename: string | null) => {
630
+ if (filename && this.matchesPatterns(filename, config.patterns)) {
631
+ this.discoveryLogger.info(`Module file changed: ${filename}`);
632
+ const modules = await this.discoverModulesAdvanced(config);
633
+ callback(modules);
634
+ }
635
+ }
636
+ );
637
+
638
+ // Store watcher for cleanup
639
+ this.watchers.push(watcher);
640
+ }
641
+ } catch {
642
+ // Path doesn't exist or not accessible
643
+ }
644
+ });
645
+ })
646
+ .catch(() => {
647
+ // fs module not available
648
+ });
649
+ }
650
+
651
+ // Clean up file watchers
652
+ cleanup(): void {
653
+ this.watchers.forEach(watcher => {
654
+ if (watcher && typeof watcher.close === 'function') {
655
+ watcher.close();
194
656
  }
195
657
  });
658
+ this.watchers = [];
196
659
  }
197
660
  }
198
661