Skip to content

Commit

Permalink
feat: load all module in app by default
Browse files Browse the repository at this point in the history
  • Loading branch information
hubvue committed Mar 25, 2022
1 parent 012a59c commit 74da864
Showing 1 changed file with 10 additions and 38 deletions.
48 changes: 10 additions & 38 deletions packages/core/src/loadModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,29 @@ export const loadModules = async (
const baseDir = process.cwd()
let moduleRootPath: string
if (env === 'development') {
moduleRootPath = `${baseDir}/src/app`
moduleRootPath = `${baseDir}/src`
} else {
let tsconfigPath = `${baseDir}/${tsconfigFileName}`
const tsconfigTxt = await readFile(tsconfigPath, {
encoding: 'utf8',
})
const tsconfig = JSON.parse(tsconfigTxt)
const outDir = tsconfig.compilerOptions.outDir
moduleRootPath = `${baseDir}/${outDir}/app`
moduleRootPath = `${baseDir}/${outDir}`
}
// controller
const controllerModuleRootPath = `${moduleRootPath}/controller`
// service
const serviceModuleRootPath = `${moduleRootPath}/service`
// model
const modelModuleRootPath = `${moduleRootPath}/model`
// middleware
const middlewareModuleRootPath = `${moduleRootPath}/middleware`
// Other
const otherModuleRootPath = modulePath.map(
(module) => `${moduleRootPath}/${module}`
)
const [controllersPath, servicesPath, middlewaresPath, modelsPath] =
await Promise.all([
getFilesPath(controllerModuleRootPath),
getFilesPath(serviceModuleRootPath),
getFilesPath(middlewareModuleRootPath),
getFilesPath(modelModuleRootPath),
])
const otherModulesPath = await Promise.all(
otherModuleRootPath.map((rootPath) => getFilesPath(rootPath))
const moduleFilesPath = getFilesPath(`${moduleRootPath}/app`)
const otherModulesRootPath = modulePath.map((module) =>
getFilesPath(`${moduleRootPath}/${module}`)
)

const allMiddlewarePath = middlewares.concat([...middlewaresPath])
const loadAllMiddlewarePath = allMiddlewarePath.map((path) =>
loadFile(path)
)
const loadModelPath = Array.from(modelsPath).map((path) => loadFile(path))
const loadModulePath = [...controllersPath, ...servicesPath].map((path) =>
loadFile(path)
const allModulesPath = (
await Promise.all([moduleFilesPath, ...otherModulesRootPath])
)

const loadOtherPath = otherModulesPath
.map((paths) => [...paths])
.map((modules) => [...modules])
.flat()
.map((path) => loadFile(path))
.concat(middlewares)

await Promise.all(loadAllMiddlewarePath)
await Promise.all(loadModelPath)
await Promise.all(loadModulePath)
await Promise.all(loadOtherPath)
await Promise.all(allModulesPath.map((path) => loadFile(path)))
} catch (err) {
logger.error(`${err.message} \n ${err.stack}`)
}
Expand Down

0 comments on commit 74da864

Please sign in to comment.