From a9aa4a9a08976a8b3505b5b857fef0c0e136377d Mon Sep 17 00:00:00 2001 From: samanhappy Date: Tue, 5 Aug 2025 14:48:48 +0800 Subject: [PATCH] feat: Update registerService to handle environment-specific service overrides (#257) --- src/services/registry.ts | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/services/registry.ts b/src/services/registry.ts index 438ab84..8e8d6f8 100644 --- a/src/services/registry.ts +++ b/src/services/registry.ts @@ -13,18 +13,37 @@ const instances = new Map(); export function registerService(key: string, entry: Service) { // 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); }