mirror of
https://github.com/Lifeforge-app/lifeforge.git
synced 2026-06-28 06:46:24 +00:00
feat(api): track loaded modules in memory for availability checks
- Add moduleRegistry with hashedKey/fullName tracking - Register modules on load in loadModuleRoutes - Replace filesystem check with in-memory lookup in checkModulesAvailability
This commit is contained in:
@@ -5,6 +5,8 @@ import crypto from 'crypto'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
import { registerModule } from './moduleRegistry'
|
||||
|
||||
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
|
||||
|
||||
const logger = createServiceLogger('Route Loader')
|
||||
@@ -68,6 +70,7 @@ export async function loadModuleRoutes(): Promise<Record<string, unknown>> {
|
||||
}
|
||||
|
||||
modules[key] = mod.default
|
||||
registerModule(key, pkg.name)
|
||||
} catch (error) {
|
||||
logger.error(`Failed to load routes from ${modDir}: ${error}`)
|
||||
}
|
||||
|
||||
22
apps/api/src/core/functions/modules/moduleRegistry.ts
Normal file
22
apps/api/src/core/functions/modules/moduleRegistry.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
interface ModuleEntry {
|
||||
hashedKey: string
|
||||
fullName: string
|
||||
}
|
||||
|
||||
const registeredModules: ModuleEntry[] = []
|
||||
|
||||
export function registerModule(hashedKey: string, fullName: string) {
|
||||
registeredModules.push({ hashedKey, fullName })
|
||||
}
|
||||
|
||||
export function isModuleRegistered(hashedKey: string): boolean {
|
||||
return registeredModules.some(m => m.hashedKey === hashedKey)
|
||||
}
|
||||
|
||||
export function isModuleNameRegistered(fullName: string): boolean {
|
||||
return registeredModules.some(m => m.fullName === fullName)
|
||||
}
|
||||
|
||||
export function getRegisteredModules(): ModuleEntry[] {
|
||||
return [...registeredModules]
|
||||
}
|
||||
@@ -1,15 +1,11 @@
|
||||
import fs from 'fs'
|
||||
import { isModuleNameRegistered } from '../modules/moduleRegistry'
|
||||
|
||||
export async function checkModulesAvailability(
|
||||
moduleId: string
|
||||
): Promise<boolean> {
|
||||
const modulePath = `../modules/${moduleId}/`
|
||||
const fullName = moduleId.startsWith('@')
|
||||
? moduleId
|
||||
: `@lifeforge/${moduleId}`
|
||||
|
||||
try {
|
||||
fs.accessSync(modulePath, fs.constants.R_OK)
|
||||
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
return isModuleNameRegistered(fullName)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user