@morojs/moro 1.5.14 → 1.5.15
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/core/config/config-validator.js +21 -0
- package/dist/core/config/config-validator.js.map +1 -1
- package/dist/core/config/schema.js +12 -0
- package/dist/core/config/schema.js.map +1 -1
- package/dist/core/modules/auto-discovery.d.ts +13 -0
- package/dist/core/modules/auto-discovery.js +247 -12
- package/dist/core/modules/auto-discovery.js.map +1 -1
- package/dist/moro.d.ts +9 -0
- package/dist/moro.js +178 -22
- package/dist/moro.js.map +1 -1
- package/dist/types/config.d.ts +12 -0
- package/dist/types/core.d.ts +12 -1
- package/package.json +3 -2
- package/src/core/config/config-validator.ts +36 -0
- package/src/core/config/schema.ts +12 -0
- package/src/core/modules/auto-discovery.ts +322 -11
- package/src/moro.ts +224 -37
- package/src/types/config.ts +12 -0
- package/src/types/core.ts +15 -2
|
@@ -1,14 +1,17 @@
|
|
|
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 } 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';
|
|
8
|
+
import { minimatch } from 'minimatch';
|
|
7
9
|
|
|
8
10
|
export class ModuleDiscovery {
|
|
9
11
|
private baseDir: string;
|
|
10
12
|
private options: DiscoveryOptions;
|
|
11
13
|
private discoveryLogger = createFrameworkLogger('MODULE_DISCOVERY');
|
|
14
|
+
private watchers: any[] = []; // Store file watchers for cleanup
|
|
12
15
|
|
|
13
16
|
constructor(baseDir: string = process.cwd(), options: DiscoveryOptions = {}) {
|
|
14
17
|
this.baseDir = baseDir;
|
|
@@ -177,22 +180,330 @@ export class ModuleDiscovery {
|
|
|
177
180
|
);
|
|
178
181
|
}
|
|
179
182
|
|
|
183
|
+
// Enhanced auto-discovery with advanced configuration
|
|
184
|
+
async discoverModulesAdvanced(
|
|
185
|
+
config: ModuleDefaultsConfig['autoDiscovery']
|
|
186
|
+
): Promise<ModuleConfig[]> {
|
|
187
|
+
if (!config.enabled) {
|
|
188
|
+
return [];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const allModules: ModuleConfig[] = [];
|
|
192
|
+
|
|
193
|
+
// Discover from all configured paths
|
|
194
|
+
for (const searchPath of config.paths) {
|
|
195
|
+
const modules = await this.discoverFromPath(searchPath, config);
|
|
196
|
+
allModules.push(...modules);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Remove duplicates based on name@version
|
|
200
|
+
const uniqueModules = this.deduplicateModules(allModules);
|
|
201
|
+
|
|
202
|
+
// Sort modules based on load order strategy
|
|
203
|
+
const sortedModules = this.sortModules(uniqueModules, config.loadOrder);
|
|
204
|
+
|
|
205
|
+
// Validate dependencies if using dependency order
|
|
206
|
+
if (config.loadOrder === 'dependency') {
|
|
207
|
+
return this.resolveDependencyOrder(sortedModules);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return sortedModules;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Discover modules from a specific path with advanced filtering
|
|
214
|
+
private async discoverFromPath(
|
|
215
|
+
searchPath: string,
|
|
216
|
+
config: ModuleDefaultsConfig['autoDiscovery']
|
|
217
|
+
): Promise<ModuleConfig[]> {
|
|
218
|
+
const modules: ModuleConfig[] = [];
|
|
219
|
+
const fullPath = join(this.baseDir, searchPath);
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
if (!statSync(fullPath).isDirectory()) {
|
|
223
|
+
return modules;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const files = this.findMatchingFiles(fullPath, config);
|
|
227
|
+
|
|
228
|
+
for (const filePath of files) {
|
|
229
|
+
try {
|
|
230
|
+
const module = await this.loadModule(filePath);
|
|
231
|
+
if (module && this.validateAdvancedModule(module, config)) {
|
|
232
|
+
modules.push(module);
|
|
233
|
+
this.discoveryLogger.info(
|
|
234
|
+
`Auto-discovered module: ${module.name}@${module.version} from ${relative(this.baseDir, filePath)}`
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
} catch (error) {
|
|
238
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
239
|
+
|
|
240
|
+
if (config.failOnError) {
|
|
241
|
+
throw new Error(`Failed to load module from ${filePath}: ${errorMsg}`);
|
|
242
|
+
} else {
|
|
243
|
+
this.discoveryLogger.warn(
|
|
244
|
+
`Failed to load module from ${filePath}`,
|
|
245
|
+
'MODULE_DISCOVERY',
|
|
246
|
+
{
|
|
247
|
+
error: errorMsg,
|
|
248
|
+
}
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
} catch (error) {
|
|
254
|
+
if (config.failOnError) {
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
// Directory doesn't exist or other error, continue silently
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return modules;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Find files matching patterns with ignore support
|
|
264
|
+
private findMatchingFiles(
|
|
265
|
+
basePath: string,
|
|
266
|
+
config: ModuleDefaultsConfig['autoDiscovery'],
|
|
267
|
+
currentDepth: number = 0
|
|
268
|
+
): string[] {
|
|
269
|
+
const files: string[] = [];
|
|
270
|
+
|
|
271
|
+
if (currentDepth >= config.maxDepth) {
|
|
272
|
+
return files;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
try {
|
|
276
|
+
const items = readdirSync(basePath);
|
|
277
|
+
|
|
278
|
+
for (const item of items) {
|
|
279
|
+
const fullPath = join(basePath, item);
|
|
280
|
+
const relativePath = relative(this.baseDir, fullPath);
|
|
281
|
+
|
|
282
|
+
// Check ignore patterns
|
|
283
|
+
if (this.shouldIgnore(relativePath, config.ignorePatterns)) {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const stat = statSync(fullPath);
|
|
288
|
+
|
|
289
|
+
if (stat.isDirectory() && config.recursive) {
|
|
290
|
+
files.push(...this.findMatchingFiles(fullPath, config, currentDepth + 1));
|
|
291
|
+
} else if (stat.isFile()) {
|
|
292
|
+
// Check if file matches any pattern
|
|
293
|
+
if (this.matchesPatterns(relativePath, config.patterns)) {
|
|
294
|
+
files.push(fullPath);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
} catch {
|
|
299
|
+
// Directory not accessible, skip
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return files;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Check if path should be ignored
|
|
306
|
+
private shouldIgnore(path: string, ignorePatterns: string[]): boolean {
|
|
307
|
+
return ignorePatterns.some(pattern => minimatch(path, pattern));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// Check if path matches any of the patterns
|
|
311
|
+
private matchesPatterns(path: string, patterns: string[]): boolean {
|
|
312
|
+
return patterns.some(pattern => minimatch(path, pattern));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Remove duplicate modules
|
|
316
|
+
private deduplicateModules(modules: ModuleConfig[]): ModuleConfig[] {
|
|
317
|
+
const seen = new Set<string>();
|
|
318
|
+
return modules.filter(module => {
|
|
319
|
+
const key = `${module.name}@${module.version}`;
|
|
320
|
+
if (seen.has(key)) {
|
|
321
|
+
this.discoveryLogger.warn(`Duplicate module found: ${key}`, 'MODULE_DISCOVERY');
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
seen.add(key);
|
|
325
|
+
return true;
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Sort modules based on strategy
|
|
330
|
+
private sortModules(
|
|
331
|
+
modules: ModuleConfig[],
|
|
332
|
+
strategy: ModuleDefaultsConfig['autoDiscovery']['loadOrder']
|
|
333
|
+
): ModuleConfig[] {
|
|
334
|
+
switch (strategy) {
|
|
335
|
+
case 'alphabetical':
|
|
336
|
+
return modules.sort((a, b) => a.name.localeCompare(b.name));
|
|
337
|
+
|
|
338
|
+
case 'dependency':
|
|
339
|
+
// Will be handled by resolveDependencyOrder
|
|
340
|
+
return modules;
|
|
341
|
+
|
|
342
|
+
case 'custom':
|
|
343
|
+
// Allow custom sorting via module priority (if defined)
|
|
344
|
+
return modules.sort((a, b) => {
|
|
345
|
+
const aPriority = (a.config as any)?.priority || 0;
|
|
346
|
+
const bPriority = (b.config as any)?.priority || 0;
|
|
347
|
+
return bPriority - aPriority; // Higher priority first
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
default:
|
|
351
|
+
return modules;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Resolve dependency order using topological sort
|
|
356
|
+
private resolveDependencyOrder(modules: ModuleConfig[]): ModuleConfig[] {
|
|
357
|
+
const moduleMap = new Map<string, ModuleConfig>();
|
|
358
|
+
const dependencyGraph = new Map<string, string[]>();
|
|
359
|
+
|
|
360
|
+
// Build module map and dependency graph
|
|
361
|
+
modules.forEach(module => {
|
|
362
|
+
const key = `${module.name}@${module.version}`;
|
|
363
|
+
moduleMap.set(key, module);
|
|
364
|
+
dependencyGraph.set(key, module.dependencies || []);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
// Topological sort
|
|
368
|
+
const visited = new Set<string>();
|
|
369
|
+
const visiting = new Set<string>();
|
|
370
|
+
const sorted: ModuleConfig[] = [];
|
|
371
|
+
|
|
372
|
+
const visit = (moduleKey: string): void => {
|
|
373
|
+
if (visiting.has(moduleKey)) {
|
|
374
|
+
throw new Error(`Circular dependency detected involving ${moduleKey}`);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (visited.has(moduleKey)) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
visiting.add(moduleKey);
|
|
382
|
+
|
|
383
|
+
const dependencies = dependencyGraph.get(moduleKey) || [];
|
|
384
|
+
dependencies.forEach(dep => {
|
|
385
|
+
// Find the dependency in our modules
|
|
386
|
+
const depModule = Array.from(moduleMap.keys()).find(key =>
|
|
387
|
+
key.startsWith(`${dep.split('@')[0]}@`)
|
|
388
|
+
);
|
|
389
|
+
if (depModule) {
|
|
390
|
+
visit(depModule);
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
visiting.delete(moduleKey);
|
|
395
|
+
visited.add(moduleKey);
|
|
396
|
+
|
|
397
|
+
const module = moduleMap.get(moduleKey);
|
|
398
|
+
if (module) {
|
|
399
|
+
sorted.push(module);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
// Visit all modules
|
|
404
|
+
Array.from(moduleMap.keys()).forEach(key => {
|
|
405
|
+
if (!visited.has(key)) {
|
|
406
|
+
visit(key);
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
return sorted;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Enhanced module validation
|
|
414
|
+
private validateAdvancedModule(
|
|
415
|
+
module: ModuleConfig,
|
|
416
|
+
_config: ModuleDefaultsConfig['autoDiscovery']
|
|
417
|
+
): boolean {
|
|
418
|
+
// Basic validation
|
|
419
|
+
if (!this.isValidModule(module)) {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Additional validation can be added here
|
|
424
|
+
// For example, checking module compatibility, version constraints, etc.
|
|
425
|
+
|
|
426
|
+
return true;
|
|
427
|
+
}
|
|
428
|
+
|
|
180
429
|
// Watch for module changes (for development)
|
|
181
430
|
watchModules(callback: (modules: ModuleConfig[]) => void): void {
|
|
182
|
-
|
|
183
|
-
|
|
431
|
+
// Use dynamic import for fs to avoid require()
|
|
432
|
+
import('fs')
|
|
433
|
+
.then(fs => {
|
|
434
|
+
const modulePaths = this.findModuleFiles();
|
|
184
435
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
436
|
+
modulePaths.forEach(path => {
|
|
437
|
+
try {
|
|
438
|
+
fs.watchFile(path, async () => {
|
|
439
|
+
this.discoveryLogger.info(`Module file changed: ${path}`);
|
|
440
|
+
const modules = await this.discoverModules();
|
|
441
|
+
callback(modules);
|
|
442
|
+
});
|
|
443
|
+
} catch {
|
|
444
|
+
// File watching not supported or failed
|
|
445
|
+
}
|
|
191
446
|
});
|
|
192
|
-
}
|
|
193
|
-
|
|
447
|
+
})
|
|
448
|
+
.catch(() => {
|
|
449
|
+
// fs module not available
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// Watch modules with advanced configuration
|
|
454
|
+
watchModulesAdvanced(
|
|
455
|
+
config: ModuleDefaultsConfig['autoDiscovery'],
|
|
456
|
+
callback: (modules: ModuleConfig[]) => void
|
|
457
|
+
): void {
|
|
458
|
+
if (!config.watchForChanges) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
import('fs')
|
|
463
|
+
.then(fs => {
|
|
464
|
+
const watchedPaths = new Set<string>();
|
|
465
|
+
|
|
466
|
+
// Watch all configured paths
|
|
467
|
+
config.paths.forEach(searchPath => {
|
|
468
|
+
const fullPath = join(this.baseDir, searchPath);
|
|
469
|
+
|
|
470
|
+
try {
|
|
471
|
+
if (statSync(fullPath).isDirectory() && !watchedPaths.has(fullPath)) {
|
|
472
|
+
watchedPaths.add(fullPath);
|
|
473
|
+
|
|
474
|
+
const watcher = fs.watch(
|
|
475
|
+
fullPath,
|
|
476
|
+
{ recursive: config.recursive },
|
|
477
|
+
async (eventType: string, filename: string | null) => {
|
|
478
|
+
if (filename && this.matchesPatterns(filename, config.patterns)) {
|
|
479
|
+
this.discoveryLogger.info(`Module file changed: ${filename}`);
|
|
480
|
+
const modules = await this.discoverModulesAdvanced(config);
|
|
481
|
+
callback(modules);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
);
|
|
485
|
+
|
|
486
|
+
// Store watcher for cleanup
|
|
487
|
+
this.watchers.push(watcher);
|
|
488
|
+
}
|
|
489
|
+
} catch {
|
|
490
|
+
// Path doesn't exist or not accessible
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
})
|
|
494
|
+
.catch(() => {
|
|
495
|
+
// fs module not available
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// Clean up file watchers
|
|
500
|
+
cleanup(): void {
|
|
501
|
+
this.watchers.forEach(watcher => {
|
|
502
|
+
if (watcher && typeof watcher.close === 'function') {
|
|
503
|
+
watcher.close();
|
|
194
504
|
}
|
|
195
505
|
});
|
|
506
|
+
this.watchers = [];
|
|
196
507
|
}
|
|
197
508
|
}
|
|
198
509
|
|
package/src/moro.ts
CHANGED
|
@@ -5,41 +5,28 @@ import { Moro as MoroCore } from './core/framework';
|
|
|
5
5
|
import { HttpRequest, HttpResponse, middleware } from './core/http';
|
|
6
6
|
import { ModuleConfig, InternalRouteDefinition } from './types/module';
|
|
7
7
|
import { MoroOptions } from './types/core';
|
|
8
|
+
import { ModuleDefaultsConfig } from './types/config';
|
|
8
9
|
import { MoroEventBus } from './core/events';
|
|
9
|
-
import {
|
|
10
|
-
createFrameworkLogger,
|
|
11
|
-
logger as globalLogger,
|
|
12
|
-
applyLoggingConfiguration,
|
|
13
|
-
} from './core/logger';
|
|
10
|
+
import { createFrameworkLogger, applyLoggingConfiguration } from './core/logger';
|
|
14
11
|
import { Logger } from './types/logger';
|
|
15
12
|
import { MiddlewareManager } from './core/middleware';
|
|
16
13
|
import { IntelligentRoutingManager } from './core/routing/app-integration';
|
|
17
14
|
import { RouteBuilder, RouteSchema, CompiledRoute } from './core/routing';
|
|
18
15
|
import { AppDocumentationManager, DocsConfig } from './core/docs';
|
|
19
|
-
import { readdirSync, statSync } from 'fs';
|
|
20
|
-
import { join } from 'path';
|
|
21
16
|
import { EventEmitter } from 'events';
|
|
22
17
|
// Configuration System Integration
|
|
23
|
-
import {
|
|
24
|
-
initializeConfig,
|
|
25
|
-
getGlobalConfig,
|
|
26
|
-
loadConfigWithOptions,
|
|
27
|
-
type AppConfig,
|
|
28
|
-
} from './core/config';
|
|
18
|
+
import { initializeConfig, type AppConfig } from './core/config';
|
|
29
19
|
// Runtime System Integration
|
|
30
|
-
import {
|
|
31
|
-
RuntimeAdapter,
|
|
32
|
-
RuntimeType,
|
|
33
|
-
createRuntimeAdapter,
|
|
34
|
-
NodeRuntimeAdapter,
|
|
35
|
-
} from './core/runtime';
|
|
20
|
+
import { RuntimeAdapter, RuntimeType, createRuntimeAdapter } from './core/runtime';
|
|
36
21
|
|
|
37
22
|
export class Moro extends EventEmitter {
|
|
38
23
|
private coreFramework: MoroCore;
|
|
39
24
|
private routes: InternalRouteDefinition[] = [];
|
|
40
25
|
private moduleCounter = 0;
|
|
41
26
|
private loadedModules = new Set<string>();
|
|
27
|
+
private lazyModules = new Map<string, ModuleConfig>();
|
|
42
28
|
private routeHandlers: Record<string, Function> = {};
|
|
29
|
+
private moduleDiscovery?: any; // Store for cleanup
|
|
43
30
|
// Enterprise event system integration
|
|
44
31
|
private eventBus: MoroEventBus;
|
|
45
32
|
// Application logger
|
|
@@ -133,7 +120,12 @@ export class Moro extends EventEmitter {
|
|
|
133
120
|
|
|
134
121
|
// Auto-discover modules if enabled
|
|
135
122
|
if (options.autoDiscover !== false) {
|
|
136
|
-
|
|
123
|
+
// Initialize auto-discovery asynchronously
|
|
124
|
+
this.initializeAutoDiscovery(options).catch(error => {
|
|
125
|
+
this.logger.error('Auto-discovery initialization failed', 'Framework', {
|
|
126
|
+
error: error instanceof Error ? error.message : String(error),
|
|
127
|
+
});
|
|
128
|
+
});
|
|
137
129
|
}
|
|
138
130
|
|
|
139
131
|
// Emit initialization event through enterprise event bus
|
|
@@ -890,29 +882,215 @@ export class Moro extends EventEmitter {
|
|
|
890
882
|
this.use(middleware.bodySize({ limit: '10mb' }));
|
|
891
883
|
}
|
|
892
884
|
|
|
893
|
-
|
|
885
|
+
// Enhanced auto-discovery initialization
|
|
886
|
+
private async initializeAutoDiscovery(options: MoroOptions): Promise<void> {
|
|
887
|
+
const { ModuleDiscovery } = await import('./core/modules/auto-discovery');
|
|
888
|
+
|
|
889
|
+
// Merge auto-discovery configuration
|
|
890
|
+
const autoDiscoveryConfig = this.mergeAutoDiscoveryConfig(options);
|
|
891
|
+
|
|
892
|
+
if (!autoDiscoveryConfig.enabled) {
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
this.moduleDiscovery = new ModuleDiscovery(process.cwd());
|
|
897
|
+
|
|
894
898
|
try {
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
// No index.ts, skip
|
|
899
|
+
// Discover modules based on configuration
|
|
900
|
+
const modules = await this.moduleDiscovery.discoverModulesAdvanced(autoDiscoveryConfig);
|
|
901
|
+
|
|
902
|
+
// Load modules based on strategy
|
|
903
|
+
await this.loadDiscoveredModules(modules, autoDiscoveryConfig);
|
|
904
|
+
|
|
905
|
+
// Setup file watching if enabled
|
|
906
|
+
if (autoDiscoveryConfig.watchForChanges) {
|
|
907
|
+
this.moduleDiscovery.watchModulesAdvanced(
|
|
908
|
+
autoDiscoveryConfig,
|
|
909
|
+
async (updatedModules: ModuleConfig[]) => {
|
|
910
|
+
await this.handleModuleChanges(updatedModules);
|
|
908
911
|
}
|
|
912
|
+
);
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
this.logger.info(
|
|
916
|
+
`Auto-discovery completed: ${modules.length} modules loaded`,
|
|
917
|
+
'ModuleDiscovery'
|
|
918
|
+
);
|
|
919
|
+
} catch (error) {
|
|
920
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
921
|
+
|
|
922
|
+
if (autoDiscoveryConfig.failOnError) {
|
|
923
|
+
throw new Error(`Module auto-discovery failed: ${errorMsg}`);
|
|
924
|
+
} else {
|
|
925
|
+
this.logger.warn(`Module auto-discovery failed: ${errorMsg}`, 'ModuleDiscovery');
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// Merge auto-discovery configuration from multiple sources
|
|
931
|
+
private mergeAutoDiscoveryConfig(options: MoroOptions) {
|
|
932
|
+
const defaultConfig = this.config.modules.autoDiscovery;
|
|
933
|
+
|
|
934
|
+
// Handle legacy modulesPath option
|
|
935
|
+
if (options.modulesPath && !options.autoDiscover) {
|
|
936
|
+
return {
|
|
937
|
+
...defaultConfig,
|
|
938
|
+
paths: [options.modulesPath],
|
|
939
|
+
};
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
// Handle boolean autoDiscover option
|
|
943
|
+
if (typeof options.autoDiscover === 'boolean') {
|
|
944
|
+
return {
|
|
945
|
+
...defaultConfig,
|
|
946
|
+
enabled: options.autoDiscover,
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
// Handle object autoDiscover option
|
|
951
|
+
if (typeof options.autoDiscover === 'object') {
|
|
952
|
+
return {
|
|
953
|
+
...defaultConfig,
|
|
954
|
+
...options.autoDiscover,
|
|
955
|
+
};
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
return defaultConfig;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
// Load discovered modules based on strategy
|
|
962
|
+
private async loadDiscoveredModules(
|
|
963
|
+
modules: ModuleConfig[],
|
|
964
|
+
config: ModuleDefaultsConfig['autoDiscovery']
|
|
965
|
+
): Promise<void> {
|
|
966
|
+
switch (config.loadingStrategy) {
|
|
967
|
+
case 'eager':
|
|
968
|
+
// Load all modules immediately
|
|
969
|
+
for (const module of modules) {
|
|
970
|
+
await this.loadModule(module);
|
|
971
|
+
}
|
|
972
|
+
break;
|
|
973
|
+
|
|
974
|
+
case 'lazy':
|
|
975
|
+
// Register modules for lazy loading
|
|
976
|
+
this.registerLazyModules(modules);
|
|
977
|
+
break;
|
|
978
|
+
|
|
979
|
+
case 'conditional':
|
|
980
|
+
// Load modules based on conditions
|
|
981
|
+
await this.loadConditionalModules(modules);
|
|
982
|
+
break;
|
|
983
|
+
|
|
984
|
+
default:
|
|
985
|
+
// Default to eager loading
|
|
986
|
+
for (const module of modules) {
|
|
987
|
+
await this.loadModule(module);
|
|
909
988
|
}
|
|
910
|
-
});
|
|
911
|
-
} catch {
|
|
912
|
-
// Modules directory doesn't exist, that's fine
|
|
913
989
|
}
|
|
914
990
|
}
|
|
915
991
|
|
|
992
|
+
// Register modules for lazy loading
|
|
993
|
+
private registerLazyModules(modules: ModuleConfig[]): void {
|
|
994
|
+
modules.forEach(module => {
|
|
995
|
+
// Store module for lazy loading when first route is accessed
|
|
996
|
+
this.lazyModules.set(module.name, module);
|
|
997
|
+
|
|
998
|
+
// Register placeholder routes that trigger lazy loading
|
|
999
|
+
if (module.routes) {
|
|
1000
|
+
module.routes.forEach(route => {
|
|
1001
|
+
const basePath = `/api/v${module.version}/${module.name}`;
|
|
1002
|
+
const fullPath = `${basePath}${route.path}`;
|
|
1003
|
+
|
|
1004
|
+
// Note: Lazy loading will be implemented when route is accessed
|
|
1005
|
+
// For now, we'll store the module for later loading
|
|
1006
|
+
this.logger.debug(
|
|
1007
|
+
`Registered lazy route: ${route.method} ${fullPath}`,
|
|
1008
|
+
'ModuleDiscovery'
|
|
1009
|
+
);
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
});
|
|
1013
|
+
|
|
1014
|
+
this.logger.info(`Registered ${modules.length} modules for lazy loading`, 'ModuleDiscovery');
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
// Load modules conditionally based on environment or configuration
|
|
1018
|
+
private async loadConditionalModules(modules: ModuleConfig[]): Promise<void> {
|
|
1019
|
+
for (const module of modules) {
|
|
1020
|
+
const shouldLoad = this.shouldLoadModule(module);
|
|
1021
|
+
|
|
1022
|
+
if (shouldLoad) {
|
|
1023
|
+
await this.loadModule(module);
|
|
1024
|
+
} else {
|
|
1025
|
+
this.logger.debug(`Skipping module ${module.name} due to conditions`, 'ModuleDiscovery');
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
// Determine if a module should be loaded based on conditions
|
|
1031
|
+
private shouldLoadModule(module: ModuleConfig): boolean {
|
|
1032
|
+
const moduleConfig = module.config as any;
|
|
1033
|
+
|
|
1034
|
+
// Check environment conditions
|
|
1035
|
+
if (moduleConfig?.conditions?.environment) {
|
|
1036
|
+
const requiredEnv = moduleConfig.conditions.environment;
|
|
1037
|
+
const currentEnv = process.env.NODE_ENV || 'development';
|
|
1038
|
+
|
|
1039
|
+
if (Array.isArray(requiredEnv)) {
|
|
1040
|
+
if (!requiredEnv.includes(currentEnv)) {
|
|
1041
|
+
return false;
|
|
1042
|
+
}
|
|
1043
|
+
} else if (requiredEnv !== currentEnv) {
|
|
1044
|
+
return false;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// Check feature flags
|
|
1049
|
+
if (moduleConfig?.conditions?.features) {
|
|
1050
|
+
const requiredFeatures = moduleConfig.conditions.features;
|
|
1051
|
+
|
|
1052
|
+
for (const feature of requiredFeatures) {
|
|
1053
|
+
if (!process.env[`FEATURE_${feature.toUpperCase()}`]) {
|
|
1054
|
+
return false;
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
// Check custom conditions
|
|
1060
|
+
if (moduleConfig?.conditions?.custom) {
|
|
1061
|
+
const customCondition = moduleConfig.conditions.custom;
|
|
1062
|
+
|
|
1063
|
+
if (typeof customCondition === 'function') {
|
|
1064
|
+
return customCondition();
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
return true;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
// Handle module changes during development
|
|
1072
|
+
private async handleModuleChanges(modules: ModuleConfig[]): Promise<void> {
|
|
1073
|
+
this.logger.info('Module changes detected, reloading...', 'ModuleDiscovery');
|
|
1074
|
+
|
|
1075
|
+
// Unload existing modules (if supported)
|
|
1076
|
+
// For now, just log the change
|
|
1077
|
+
this.eventBus.emit('modules:changed', {
|
|
1078
|
+
modules: modules.map(m => ({ name: m.name, version: m.version })),
|
|
1079
|
+
timestamp: new Date(),
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// Legacy method for backward compatibility
|
|
1084
|
+
private autoDiscoverModules(modulesPath: string) {
|
|
1085
|
+
// Redirect to new system
|
|
1086
|
+
this.initializeAutoDiscovery({
|
|
1087
|
+
autoDiscover: {
|
|
1088
|
+
enabled: true,
|
|
1089
|
+
paths: [modulesPath],
|
|
1090
|
+
},
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
|
|
916
1094
|
private async importModule(modulePath: string): Promise<ModuleConfig> {
|
|
917
1095
|
const module = await import(modulePath);
|
|
918
1096
|
return module.default || module;
|
|
@@ -1216,6 +1394,15 @@ export class Moro extends EventEmitter {
|
|
|
1216
1394
|
}
|
|
1217
1395
|
}
|
|
1218
1396
|
|
|
1397
|
+
// Clean up module discovery watchers
|
|
1398
|
+
if (this.moduleDiscovery && typeof this.moduleDiscovery.cleanup === 'function') {
|
|
1399
|
+
try {
|
|
1400
|
+
this.moduleDiscovery.cleanup();
|
|
1401
|
+
} catch (error) {
|
|
1402
|
+
// Ignore cleanup errors
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1219
1406
|
// Clean up event listeners
|
|
1220
1407
|
try {
|
|
1221
1408
|
this.eventBus.removeAllListeners();
|
package/src/types/config.ts
CHANGED
|
@@ -84,6 +84,18 @@ export interface ModuleDefaultsConfig {
|
|
|
84
84
|
stripUnknown: boolean;
|
|
85
85
|
abortEarly: boolean;
|
|
86
86
|
};
|
|
87
|
+
autoDiscovery: {
|
|
88
|
+
enabled: boolean;
|
|
89
|
+
paths: string[];
|
|
90
|
+
patterns: string[];
|
|
91
|
+
recursive: boolean;
|
|
92
|
+
loadingStrategy: 'eager' | 'lazy' | 'conditional';
|
|
93
|
+
watchForChanges: boolean;
|
|
94
|
+
ignorePatterns: string[];
|
|
95
|
+
loadOrder: 'alphabetical' | 'dependency' | 'custom';
|
|
96
|
+
failOnError: boolean;
|
|
97
|
+
maxDepth: number;
|
|
98
|
+
};
|
|
87
99
|
}
|
|
88
100
|
|
|
89
101
|
export interface LoggingConfig {
|