UNPKG

astroboy

Version:

Astroboy(阿童木)is a Nodejs SFB(Separation of Front and Back ends) framework, built on koa2.

107 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const glob = require("fast-glob"); const path = require("path"); const lodash = require("lodash"); const TYPING_FILE_EXTS = '.d.ts'; const APP_EXTENSIONS = ['js', 'ts']; /** * ### Check the file is typing file or not. * * @author Big Mogician * @param {EntryItem} entry * @returns */ function fileIsNotTypingFile(entry) { return typeof entry === 'string' ? !entry.endsWith(TYPING_FILE_EXTS) : !entry.path.endsWith(TYPING_FILE_EXTS); } /** * ### Base Loader * * @author Big Mogician * @export * @abstract * @class Loader * @template F * @template A */ class Loader { constructor(options = {}) { this.dirs = options.dirs || []; this.config = options.config || {}; this.app = options.app || {}; } /** * ### Resolve Extensions * * @author Big Mogician * @protected * @param {string} path * @param {boolean} [resolveDevide=false] * @returns * @memberof Loader */ resolveExtensions(path, resolveDevide = false) { let newPath = path; APP_EXTENSIONS.forEach(ext => (newPath = newPath.replace(`.${ext}`, ''))); return resolveDevide ? newPath.replace(/\//g, '.') : newPath; } /** * ### Get Dirs * * @author Big Mogician * @protected * @param {(string | string[])} patterns * @param {(files: EntryItem[]) => void} callback * @memberof Loader */ async globDirs(patterns) { const entries = await Promise.all(this.dirs.map((item) => { return this.globDir(item.baseDir, patterns); })); return lodash.flatten(entries); } /** * ### Resolve Dir * * @author Big Mogician * @protected * @param {string} baseDir * @param {(string | string[])} patterns * @param {(files: EntryItem[]) => void} callback * @memberof Loader */ async globDir(baseDir, patterns) { let newPatterns = []; if (typeof patterns === 'string') { newPatterns = [`${baseDir}${patterns}`]; } else if (Array.isArray(patterns)) { newPatterns = patterns.map(pattern => { return `${baseDir}${pattern}`; }); } const entries = await glob(newPatterns, { dot: true }); return entries.filter(fileIsNotTypingFile); } /** * ### 获取插件的根目录 * - 要求插件的入口文件必须放在插件根目录 * * @author Big Mogician * @protected * @param {IPluginEntry} plugin * @returns * @memberof Loader */ getPluginPath(plugin) { if (plugin.path) { return plugin.path; } const name = plugin.package || plugin.name; const entryFile = require.resolve(name); return path.dirname(entryFile); } } exports.Loader = Loader; //# sourceMappingURL=Loader.js.map