UNPKG

@lattice/core

Version:
83 lines (82 loc) 3.23 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var fs = __importStar(require("fs")); var path = __importStar(require("path")); var inject_1 = require("../inject"); var _1 = require("."); var relativePrefix = __dirname.substring(process.cwd().length, __dirname.length).split('').reduce(function (result, char) { if (char == '/' || char == '\\') result += '../'; return result; }, './'); var EndpointScanner = /** @class */ (function () { function EndpointScanner(entrance, regx) { this.entrance = entrance; this.regx = regx; this.exclues = ['node_modules']; } EndpointScanner.prototype.createServer = function () { this.dfsImport('node_modules/@lattice', false); this.dfsImport(this.entrance); return inject_1.rootContainer.get(_1.Application); }; EndpointScanner.prototype.import = function (src, hasCheckIgnore) { if (hasCheckIgnore === void 0) { hasCheckIgnore = false; } this.dfsImport(src, hasCheckIgnore); return this; }; EndpointScanner.prototype.dfsImport = function (entrance, hasCheckIgnore) { var _this = this; if (hasCheckIgnore === void 0) { hasCheckIgnore = true; } var files = fs.readdirSync(entrance); files.map(function (file) { var filePath = path.join(entrance, file); if (hasCheckIgnore && _this.checkIgnore(filePath)) return; var stats = fs.statSync(filePath); if (stats.isFile() && _this.regx.test(filePath)) { _this.require(filePath); } if (stats.isDirectory()) { _this.dfsImport(filePath, hasCheckIgnore); } }); }; EndpointScanner.prototype.checkIgnore = function (filePath) { for (var _i = 0, _a = this.exclues; _i < _a.length; _i++) { var ignore = _a[_i]; if (ignore instanceof RegExp) { if (ignore.test(filePath)) return true; } else { if (filePath.includes(ignore)) return true; } } return false; }; EndpointScanner.prototype.require = function (filePath) { if (filePath.indexOf('node_modules/') == 0) filePath = filePath.replace(/node_modules\//, ''); else if (filePath.indexOf('node_modules\\') == 0) filePath = filePath.replace(/node_modules\\/, ''); else filePath = path.join(relativePrefix, filePath); require(filePath); }; EndpointScanner.with = function (path, regx) { if (path === void 0) { path = '.'; } if (regx === void 0) { regx = new RegExp('.*\.(controller|middleware|service|provider)\.[js|ts]'); } return new this(path, regx); }; return EndpointScanner; }()); exports.EndpointScanner = EndpointScanner;