UNPKG

koa-springboot

Version:
54 lines (53 loc) 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const path = require("path"); const Router = require("koa-router"); const routerManager_1 = require("./routerManager"); class Scanner { constructor() { this.router = new Router(); this.models = Object.create(null); this.debug = false; } enableLog() { this.debug = true; } scan() { this.scanModel(); this.scanController(); } scanController() { const controllerPath = path.join(...Scanner.controllerPath); if (fs.existsSync(controllerPath) && fs.statSync(controllerPath).isDirectory()) { fs.readdirSync(controllerPath) .filter(subPath => subPath.endsWith('.js')) .forEach(subPath => { const absolutePath = path.join(controllerPath, subPath); if (this.debug) { console.log(`controller file load: ${absolutePath}`); } require(absolutePath); }); routerManager_1.default.mountRoutes(this.router, this.models); } } scanModel() { if (!Scanner.modelPath) return; const modelPath = path.join(...Scanner.modelPath); if (fs.existsSync(modelPath) && fs.statSync(modelPath).isDirectory()) { fs.readdirSync(modelPath) .filter(subPath => subPath.endsWith('.js')) .forEach(subPath => { const absolutePath = path.join(modelPath, subPath); if (this.debug) { console.log(`model file load: ${absolutePath}`); } const model = require(absolutePath).default; this.models[model.name] = model; }); } } } exports.default = Scanner;