UNPKG

@allgemein/moduls

Version:

Commons-moduls handles and manages contextual moduls for complex and modular applications.

186 lines 6.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModuleRegistry = void 0; const lodash_1 = require("lodash"); const ModuleDescriptor_1 = require("./ModuleDescriptor"); const Helper_1 = require("../utils/Helper"); const base_1 = require("@allgemein/base"); const RequireLoader_1 = require("../loader/require/RequireLoader"); const ClassesLoader_1 = require("../loader/classes/ClassesLoader"); const SettingsLoader_1 = require("../loader/settings/SettingsLoader"); const Constants_1 = require("./Constants"); const Constants_2 = require("../Constants"); class ModuleRegistry { constructor(options) { this._modules = []; this.paths = []; (0, lodash_1.defaults)(options, Constants_1.MODUL_REGISTRY_DEFAULT_OPTIONS); this._modules = []; this._options = options; this.paths = options.paths; // Helper.checkPaths(options.paths || []); this._options.depth = this._options.depth || 2; this._options.pattern.unshift(Constants_2.C_NODE_MODULES); this._options.pattern = (0, lodash_1.uniq)(this._options.pattern); } /** * Check if cache is present */ hasCache() { return !!this._options.cache; } /** * Return the cache object */ getCache() { return this._options.cache; } options() { return this._options; } getOptions() { return this.options(); } async rebuild() { ModuleDescriptor_1.ModuleDescriptor.INC = 0; this._modules = []; let modules_lists = await Promise.all((0, lodash_1.map)(this.paths, this._scan_module_path.bind(this))); let to_register = []; let one_list = [].concat(...modules_lists); for (let __modul of one_list) { let _modul = (0, lodash_1.find)(to_register, function (_x) { return _x.name === __modul.name; }); if (!_modul) { to_register.push(__modul); } else { _modul.multi_implements = true; // TODO: if module already exists check version and replace them } } // await Promise.all(map(modules, this.load.bind(this))); this._build_registry(to_register); return this; } modules() { return this._modules; } getModules() { return this.modules(); } async _scan_module_path(node_modules_dir) { const cacheKey = [ModuleRegistry.name.toLowerCase(), 'scan_modul_path', base_1.CryptUtils.shorthash(node_modules_dir)].join('--'); let packageJsons = null; if (this.hasCache()) { try { packageJsons = await this.getCache().get(cacheKey); } catch (e) { } } let options = { filter: this._options.packageFilter, depth: this._options.depth, subModulePaths: this._options.pattern, }; if (this._options.exclude) { options.exclude = this._options.exclude; } if (this._options.include) { options.include = this._options.include; } if ((0, lodash_1.isNull)(packageJsons)) { packageJsons = []; if (base_1.PlatformUtils.fileExist(base_1.PlatformUtils.join(node_modules_dir, Constants_2.C_PACKAGE_JSON))) { options.depth++; let dirname = base_1.PlatformUtils.dirname(node_modules_dir); let basename = base_1.PlatformUtils.basename(node_modules_dir); // TODO!!!! packageJsons = await Helper_1.Helper.lookupNpmInDirectory(dirname, basename, [], options); } else { packageJsons = await Helper_1.Helper.npmls(node_modules_dir, options); } if (this.hasCache() && packageJsons) { try { this.getCache().set(cacheKey, packageJsons); } catch (e) { } } } return (0, lodash_1.map)(packageJsons, (module) => { return ModuleDescriptor_1.ModuleDescriptor.fromOptions(module); }); } _build_registry(modules) { this._modules = modules; for (let _modul of this._modules) { let dependencies = (0, lodash_1.concat)([], Object.keys(_modul.dependencies), Object.keys(_modul.peerDependencies)); let submoduls = []; (0, lodash_1.map)((0, lodash_1.values)(_modul.sub_modules), v => { submoduls.push(...v.modules); }); let children = (0, lodash_1.filter)(this._modules, function (_x) { return dependencies.indexOf(_x.name) > -1; }); for (let _dep_modul of children) { _modul.child_modules.push(_dep_modul.name); } _modul.child_modules = (0, lodash_1.uniq)(_modul.child_modules); let submodules = (0, lodash_1.filter)(this._modules, (_x) => { return submoduls.indexOf(_x.name) > -1; }); submodules.forEach((m) => { m.submodule = true; }); } this._modules.sort((a, b) => { return a.child_modules.length - b.child_modules.length; }); for (let first of this._modules) { let dependents = (0, lodash_1.filter)(this._modules, function (other) { if (other.name == first.name) { return false; } if (other.child_modules.indexOf(first.name) > -1) { return true; } return false; }); if (dependents.length) { for (let x of dependents) { x.weight += (first.weight + 1); } } else { // ??? } } this._modules.sort((a, b) => { if (a.weight === b.weight) { return a.name.localeCompare(b.name); } return a.weight - b.weight; }); return this._modules; } async loader(loaderClazz, options) { let instance = Reflect.construct(loaderClazz, [this, options]); await instance.load(this.modules()); return instance; } createRequireLoader(options) { return this.loader(RequireLoader_1.RequireLoader, options); } async createClassesLoader(options) { return this.loader(ClassesLoader_1.ClassesLoader, options); } async createSettingsLoader(options) { return this.loader(SettingsLoader_1.SettingsLoader, options); } save() { } } exports.ModuleRegistry = ModuleRegistry; //# sourceMappingURL=ModuleRegistry.js.map