UNPKG

egg-xc-base

Version:

a base framework with egg.js

73 lines (58 loc) 1.81 kB
'use strict'; const egg = require('egg'); const fs = require('fs'); const path = require('path'); class Loader extends egg.AppWorkerLoader { loadConfig() { super.loadConfig(); } getLoadPaths(target) { const folders = []; let workDir = path.join(this.options.baseDir, '/app'); let directories = fs.readdirSync(workDir); for (const directory of directories) { if (directory.includes('.js')) { folders.push(path.join(workDir, `/${target}`)); } else { folders.push(path.join(workDir, `/${directory}/${target}`)); } } workDir = path.join(this.options.baseDir, '/api'); directories = fs.readdirSync(workDir); for (const directory of directories) { if (directory.includes('.js')) { folders.push(path.join(workDir, `/${target}`)); } else { folders.push(path.join(workDir, `/${directory}/${target}`)); } } return folders } loadCustomService() { const serviceDirs = this.getLoadPaths('service'); this.loadService({ directory: serviceDirs }); } loadCustomController() { const controllerDirs = this.getLoadPaths('controller'); this.loadController({ directory: controllerDirs }); } loadCustomRouter() { const routerPaths = this.getLoadPaths('router.js'); for (const filePath of routerPaths) { this.loadFile(filePath); } } async load() { this.loadApplicationExtend(); this.loadRequestExtend(); this.loadResponseExtend(); this.loadContextExtend(); this.loadHelperExtend(); this.loadCustomApp(); this.loadCustomService(); this.loadMiddleware(); this.loadCustomController(); this.loadCustomRouter() } } module.exports = Loader;