diff --git a/packages/lifeforge-server-utils/src/utils/extractProjectRoot.ts b/packages/lifeforge-server-utils/src/utils/extractProjectRoot.ts new file mode 100644 index 000000000..ab9a2faf0 --- /dev/null +++ b/packages/lifeforge-server-utils/src/utils/extractProjectRoot.ts @@ -0,0 +1,29 @@ +import path from 'path' + +/** + * Gets the project root directory name from a file path. + * Supports both app modules (/apps/) and core modules (/server/). + */ +export default function getProjectRootDir( + filePath: string +): string | undefined { + // For app modules: /path/to/projectRoot/apps/moduleId/server/... + const appsIndex = filePath.indexOf('/apps/') + + if (appsIndex !== -1) { + const pathBeforeApps = filePath.substring(0, appsIndex) + + return path.basename(pathBeforeApps) + } else { + // For core modules: /path/to/projectRoot/server/src/... + const serverIndex = filePath.indexOf('/server/') + + if (serverIndex !== -1) { + const pathBeforeServer = filePath.substring(0, serverIndex) + + return path.basename(pathBeforeServer) + } + } + + return undefined +} diff --git a/packages/lifeforge-server-utils/src/utils/getCallerModuleId.ts b/packages/lifeforge-server-utils/src/utils/getCallerModuleId.ts index f5b8bc115..1847487d3 100644 --- a/packages/lifeforge-server-utils/src/utils/getCallerModuleId.ts +++ b/packages/lifeforge-server-utils/src/utils/getCallerModuleId.ts @@ -1,3 +1,5 @@ +import getProjectRootDir from './extractProjectRoot' + export default function getCallerModuleId(): | { source: 'app' | 'core'; id: string } | undefined { @@ -17,8 +19,14 @@ export default function getCallerModuleId(): if (!filePath) return undefined - // Try external app module: /apps/{moduleId}/server/ - const appMatch = filePath.match(/lifeforge\/apps\/([^/]+)\/server\//) + const projectRoot = getProjectRootDir(filePath) + + if (!projectRoot) return undefined + + // Try external app module: /{projectRoot}/apps/{moduleId}/server/ + const appMatch = filePath.match( + new RegExp(`${projectRoot}\\/apps\\/([^/]+)\\/server\\/`) + ) if (appMatch) return { @@ -27,10 +35,10 @@ export default function getCallerModuleId(): } // Try core module: - // - /lifeforge/server/src/lib/{coreModuleId}/ - // - /lifeforge/server/src/core/{coreModuleId}/ + // - /{projectRoot}/server/src/lib/{coreModuleId}/ + // - /{projectRoot}/server/src/core/{coreModuleId}/ const coreMatch = filePath.match( - /lifeforge\/server\/src\/(?:lib|core)\/([^/]+)\// + new RegExp(`${projectRoot}\\/server\\/src\\/(?:lib|core)\\/([^/]+)\\/`) ) if (coreMatch) diff --git a/server/src/core/functions/utils/ensureRootName.ts b/server/src/core/functions/utils/ensureRootName.ts deleted file mode 100644 index 711b226ab..000000000 --- a/server/src/core/functions/utils/ensureRootName.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { coreLogger } from '@functions/logging' -import path from 'path' - - -/** - * Ensures the root directory name is 'lifeforge'. - * Exits the process if the root directory name is not 'lifeforge'. - */ -export default function ensureRootName(): void { - const projectRoot = path.basename( - path.resolve(import.meta.dirname.split('server')[0]) - ) - - if (projectRoot !== 'lifeforge') { - coreLogger.error( - `Project root directory must be named 'lifeforge', but found '${projectRoot}'. Please rename the root directory.` - ) - process.exit(1) - } -} diff --git a/server/src/index.ts b/server/src/index.ts index c1e901628..1bddb6b9a 100755 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -10,7 +10,6 @@ import { LocaleService } from '@functions/initialization/localeService' import traceRouteStack from '@functions/initialization/traceRouteStack' import { LOG_LEVELS, type LogLevel, coreLogger } from '@functions/logging' import createSocketServer from '@functions/socketio/createSocketServer' -import ensureRootName from '@functions/utils/ensureRootName' import app from './core/app' @@ -57,7 +56,6 @@ function startServer(server: ReturnType): void { async function main(): Promise { LocaleService.validateAndLoad() - ensureRootName() ensureDirectories() ensureCredentials() await checkDB() diff --git a/tools/src/commands/db/handlers/generateMigrationsHandler.ts b/tools/src/commands/db/handlers/generateMigrationsHandler.ts index c85a08186..01ab09264 100644 --- a/tools/src/commands/db/handlers/generateMigrationsHandler.ts +++ b/tools/src/commands/db/handlers/generateMigrationsHandler.ts @@ -4,6 +4,7 @@ import getPBInstance from '@/utils/pocketbase' import { cleanupOldMigrations } from '../functions/migration-generation/cleanupOldMigrations' import stageMigration from '../functions/migration-generation/stageMigrations' import { importSchemaModules } from '../utils' +import chalk from 'chalk' /** * Command handler for generating database migrations @@ -29,9 +30,9 @@ export async function generateMigrationsHandler( killPB?.() - logger.success('Migrations generated successfully') + logger.success(`Migrations generated successfully for module ${chalk.green(targetModule ? `${targetModule}` : 'all modules')}`) } catch (error) { - logger.error('Migration script failed') + logger.error(`Migration script failed for module ${chalk.red(targetModule ? `${targetModule}` : 'all modules')}.`) logger.debug(`Error details: ${error}`) killPB?.()