refactor(server-utils): extract project root logic into separate function

This commit is contained in:
lukashow
2026-02-18 17:50:21 +08:00
parent 29b4fd1211
commit 16eb381641
2 changed files with 31 additions and 21 deletions

View File

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

View File

@@ -1,4 +1,4 @@
import path from 'path'
import getProjectRootDir from './extractProjectRoot'
export default function getCallerModuleId():
| { source: 'app' | 'core'; id: string }
@@ -19,26 +19,7 @@ export default function getCallerModuleId():
if (!filePath) return undefined
// Extract project root from file path
let projectRoot: 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)
projectRoot = 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)
projectRoot = path.basename(pathBeforeServer)
}
}
const projectRoot = getProjectRootDir(filePath)
if (!projectRoot) return undefined