feat: Update registerService to handle environment-specific service overrides (#257)

This commit is contained in:
samanhappy
2025-08-05 14:48:48 +08:00
committed by GitHub
parent 48bcf9f5f0
commit a9aa4a9a08

View File

@@ -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);
}