mirror of
https://github.com/samanhappy/mcphub.git
synced 2025-12-24 02:39:19 -05:00
feat: Update registerService to handle environment-specific service overrides (#257)
This commit is contained in:
@@ -13,18 +13,37 @@ const instances = new Map<string, unknown>();
|
||||
|
||||
export function registerService<T>(key: string, entry: Service<T>) {
|
||||
// Try to load override immediately during registration
|
||||
const overridePath = join(process.cwd(), 'src', 'services', key + 'x.ts');
|
||||
try {
|
||||
const require = createRequire(process.cwd());
|
||||
const mod = require(overridePath);
|
||||
const override = mod[key.charAt(0).toUpperCase() + key.slice(1) + 'x'];
|
||||
if (typeof override === 'function') {
|
||||
entry.override = override;
|
||||
// Try multiple paths and file extensions in order
|
||||
const serviceDirs = ['src/services', 'dist/services'];
|
||||
const fileExts = ['.ts', '.js'];
|
||||
const overrideFileName = key + 'x';
|
||||
|
||||
for (const serviceDir of serviceDirs) {
|
||||
for (const fileExt of fileExts) {
|
||||
const overridePath = join(process.cwd(), serviceDir, overrideFileName + fileExt);
|
||||
|
||||
try {
|
||||
// Use createRequire with a stable path reference
|
||||
const require = createRequire(join(process.cwd(), 'package.json'));
|
||||
const mod = require(overridePath);
|
||||
const override = mod[key.charAt(0).toUpperCase() + key.slice(1) + 'x'];
|
||||
if (typeof override === 'function') {
|
||||
entry.override = override;
|
||||
break; // Found override, exit both loops
|
||||
}
|
||||
} catch (error) {
|
||||
// Continue trying next path/extension combination
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// If override was found, break out of outer loop too
|
||||
if (entry.override) {
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
// Silently ignore if override doesn't exist
|
||||
}
|
||||
|
||||
console.log(`Service registered: ${key} with entry:`, entry);
|
||||
registry.set(key, entry);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user