astroboy
Version:
Astroboy(阿童木)is a Nodejs SFB(Separation of Front and Back ends) framework, built on koa2.
105 lines • 2.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const glob = require("fast-glob");
const path = require("path");
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
*/
globDirs(patterns, callback) {
this.dirs.forEach(item => {
this.globDir(item.baseDir, patterns, callback);
});
}
/**
* ### Resolve Dir
*
* @author Big Mogician
* @protected
* @param {string} baseDir
* @param {(string | string[])} patterns
* @param {(files: EntryItem[]) => void} callback
* @memberof Loader
*/
globDir(baseDir, patterns, callback) {
let newPatterns = [];
if (typeof patterns === 'string') {
newPatterns = [`${baseDir}${patterns}`];
}
else if (Array.isArray(patterns)) {
newPatterns = patterns.map(pattern => {
return `${baseDir}${pattern}`;
});
}
const entries = glob.sync(newPatterns, { dot: true });
callback(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