Skip to content

Commit

Permalink
feat(core): module path use glob package
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwangchong committed Aug 8, 2023
1 parent 95c8014 commit 67f8a3b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 64 deletions.
9 changes: 5 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@
"reflect-metadata": "^0.1.13"
},
"dependencies": {
"@types/koa": "^2.13.8",
"koa": "^2.13.0",
"chalk": "^4.0.0",
"@kever/ioc": "workspace:*",
"@kever/router": "workspace:*",
"@kever/shared": "workspace:*"
"@kever/shared": "workspace:*",
"@types/koa": "^2.13.8",
"chalk": "^4.0.0",
"glob": "^10.3.3",
"koa": "^2.13.0"
},
"gitHead": "9e62d20cc11b371c2b5ba9b1234d3a5b0648ba90"
}
17 changes: 14 additions & 3 deletions packages/core/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,19 @@ export interface AppOptions {
*/
middlewares?: (string | Middleware)[]
/**
* auto load modules file path
* Auto load modules file path,follow the glob npm package rules.
* By default it will include 'src/app/**\/*.{ts,js}'
* The current working directory in which to search. Defaults to `process.cwd()`.
* @see — https://www.npmjs.com/package/glob
*/
modulePath?: []
include?: string[]
/**
* Exclude load modules file path,follow the glob npm package rules.
* By default it will exclude 'node_modules/**'.
* The current working directory in which to search. Defaults to `process.cwd()`.
* @see — https://www.npmjs.com/package/glob
*/
exclude?: string[]
/**
* app env, default is development
*/
Expand All @@ -63,7 +73,8 @@ const DEFAULT_OPTION: Required<AppOptions> = {
host: '127.0.0.1',
port: 8080,
middlewares: [],
modulePath: [],
exclude: ['src/app/**/*.{ts,js}'],
include: ['node_modules/**'],
env: Env.DEV,
tsconfig: 'tsconfig.json',
logger: defaultLogger,
Expand Down
34 changes: 8 additions & 26 deletions packages/core/src/loadModules.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
import { readFile } from 'node:fs/promises'
import { getFilesPath, loadModule } from '@kever/shared'
import { type AppOptions, Env } from './application'
import { glob } from 'glob'
import { loadModule } from '@kever/shared'
import { type AppOptions } from './application'

export const loadModules = async (middlewares: string[], options: Required<AppOptions>) => {
const { env, tsconfig: tsconfigFileName, modulePath, logger } = options
const baseDir = process.cwd()
const { exclude, include, logger } = options
try {
let moduleRootPath: string
if (env === Env.DEV) {
moduleRootPath = `${baseDir}/src`
} else {
const tsconfigPath = `${baseDir}/${tsconfigFileName}`
const tsconfigTxt = await readFile(tsconfigPath, { encoding: 'utf8' })
const tsconfig = JSON.parse(tsconfigTxt)
const outDir = tsconfig.compilerOptions.outDir
moduleRootPath = `${baseDir}/${outDir}`
}
const moduleFilesPath = getFilesPath(`${moduleRootPath}/app`, logger)
const otherModulesRootPath = modulePath.map((module) => getFilesPath(`${moduleRootPath}/${module}`, logger))

const allModulesPath = (
await Promise.all([moduleFilesPath, ...otherModulesRootPath])
)
.map((modules) => [...modules])
.flat()
.concat(middlewares)

await Promise.all(allModulesPath.map((path) => loadModule(path, logger)))
const moduleFiles = await glob(include, {
ignore: exclude
})
await Promise.all((moduleFiles.concat(middlewares)).map((path) => loadModule(path, logger)))
} catch (err) {
logger.error(`${err.message} \n ${err.stack}`)
}
Expand Down
33 changes: 2 additions & 31 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 67f8a3b

Please sign in to comment.