UNPKG

lin-mizar

Version:
136 lines (135 loc) 5.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Loader = exports.disableLoading = void 0; const tslib_1 = require("tslib"); const consola_1 = tslib_1.__importDefault(require("consola")); const sequelize_1 = require("sequelize"); const utils_1 = require("../utils"); const lodash_1 = require("lodash"); const koa_router_1 = tslib_1.__importDefault(require("koa-router")); const path_1 = tslib_1.__importDefault(require("path")); const plugin_1 = require("../plugin"); const config_1 = require("../config"); const baseDir = config_1.config.getItem('baseDir', process.cwd()); // 当前文件路由是否挂载 exports.disableLoading = Symbol('disableLoading'); /** * 加载器 * 用于加载插件和路由文件 */ class Loader { constructor(pluginPath, app) { this.plugins = {}; utils_1.assert(!!pluginPath, 'pluginPath must not be empty'); this.pluginPath = pluginPath; this.app = app; this.loadMainApi(app); this.loadPlugins(); } /** * 初始化 * 挂载 loader 和 插件 */ initLoader() { this.app.context.loader = this; this.app.context.plugins = this.plugins; } /** * 加载插件 */ loadPlugins() { Object.keys(this.pluginPath).forEach(item => { // item is name of plugin if (lodash_1.get(this.pluginPath, `${item}.enable`)) { const path1 = lodash_1.get(this.pluginPath, `${item}.path`); let confPath = ''; const scriptType = config_1.config.getItem('scriptType', 'js'); const prod = process.env.NODE_ENV === 'production'; if (prod || scriptType !== 'ts') { confPath = path_1.default.resolve(baseDir, path1, 'config.js'); } else { confPath = path_1.default.resolve(baseDir, path1, 'config.ts'); } const appPath = path_1.default.resolve(baseDir, path1, 'app'); const incomingConf = lodash_1.get(this.pluginPath, item); this.loadConfig(item, confPath, incomingConf); this.loadPlugin(item, appPath); } }); } /** * loadPlugin 加载单个插件 */ loadPlugin(name, path) { const mod = require(path); // const exports = get(mod, "default"); const plugin = new plugin_1.Plugin(name); Object.keys(mod).forEach(key => { if (mod[key] instanceof koa_router_1.default) { plugin.addController(key, mod[key]); } else if (sequelize_1.Model.isPrototypeOf(mod[key])) { // 如果导出的模型继承自Model plugin.addModel(key, mod[key]); } }); this.plugins[name] = plugin; } /** * loadConfig 加载插件配置 */ loadConfig(name, path, incomingConf) { const mod = require(path); // const conf = get(mod, "default"); const newConf = {}; lodash_1.set(newConf, name, Object.assign(Object.assign({}, mod), incomingConf)); config_1.config.getConfigFromObj(newConf); } /** * 加载主应用中的所有路由 */ loadMainApi(app) { const mainRouter = new koa_router_1.default(); this.mainRouter = mainRouter; // 默认api的文件夹 let apiDir = config_1.config.getItem('apiDir', 'app/api'); apiDir = `${baseDir}/${apiDir}`; const files = utils_1.getFiles(apiDir); for (const file of files) { const extension = file.substring(file.lastIndexOf('.'), file.length); // 现在只考虑加载.js文件,后续考虑.ts文件 if (extension === '.js') { const mod = require(file); // 如果mod 为 koa-router实例 // const exports = get(mod, "default"); // 如果disableLoading为true,则不加载这个文件路由 // tslint:disable-next-line:no-empty if (mod instanceof koa_router_1.default) { if (config_1.config.getItem('debug')) { consola_1.default.info(`loading a router instance from file: ${file}`); lodash_1.get(mod, 'stack', []).forEach(ly => { consola_1.default.info(`loading a route: ${lodash_1.get(ly, 'path')}`); }); } mainRouter.use(mod.routes()).use(mod.allowedMethods()); } else if (!mod[exports.disableLoading]) { Object.keys(mod).forEach(key => { if (mod[key] instanceof koa_router_1.default) { if (config_1.config.getItem('debug')) { consola_1.default.info(`loading a router instance :${key} from file: ${file}`); lodash_1.get(mod[key], 'stack', []).forEach(ly => { consola_1.default.info(`loading a route: ${lodash_1.get(ly, 'path')}`); }); } mainRouter.use(mod[key].routes()).use(mod[key].allowedMethods()); } }); } } } app.use(mainRouter.routes()).use(mainRouter.allowedMethods()); } } exports.Loader = Loader;