UNPKG

@allgemein/moduls

Version:

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

127 lines 4.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClassesLoader = void 0; const lodash_1 = require("lodash"); const AbstractModuleLoader_1 = require("../AbstractModuleLoader"); const ClassesHandle_1 = require("./ClassesHandle"); const base_1 = require("@allgemein/base"); class ClassesLoader extends AbstractModuleLoader_1.AbstractModuleLoader { getClasses(topic) { let classes = []; for (let handle of this.handles()) { let cls = handle.getClasses(topic); if (!(0, lodash_1.isEmpty)(cls)) { classes = classes.concat(cls); } } return classes; } getClassesWithFilter(topic, excludeFilter) { let classes = []; for (let handle of this.handles()) { let cls = handle.getClasses(topic); if (!(0, lodash_1.isEmpty)(cls)) { cls.forEach(c => { const className = base_1.ClassLoader.getClassName(c); if (excludeFilter && excludeFilter(className, handle.module.name)) { return; } classes.push(c); }); } } return classes; } getClassesByModule(topic) { let classes = {}; for (let handle of this.handles()) { let modulClasses = handle.getClasses(topic); if (!(0, lodash_1.isEmpty)(modulClasses)) { classes[handle.module.name] = modulClasses; } } return classes; } async loadOne(modul) { let handle = new ClassesHandle_1.ClassesHandle(modul); // for (let lib of this._options.libs) { const libs = await Promise.all(this._options.libs.map(async (lib) => { const _lib = { topic: lib.topic, refs: [], classes: [] }; const topic = lib.topic; // let refs = []; for (let _path of lib.refs) { let lib_path = base_1.PlatformUtils.join(modul.path, _path); let res = await base_1.Glob.async(lib_path); if (!(0, lodash_1.isEmpty)(res)) { for (let r of res) { if (base_1.PlatformUtils.fileExist(r) && base_1.PlatformUtils.isDir(r)) { _lib.refs.push(base_1.PlatformUtils.join(r, '*')); } else if (base_1.PlatformUtils.fileExist(r) && base_1.PlatformUtils.isFile(r)) { _lib.refs.push(r); } } } else if (base_1.PlatformUtils.fileExist(lib_path + '.js') && base_1.PlatformUtils.isFile(lib_path + '.js')) { _lib.refs.push(lib_path + '.js'); } else if (base_1.PlatformUtils.fileExist(lib_path + '.ts') && base_1.PlatformUtils.isFile(lib_path + '.ts')) { // if ts-node is used on start _lib.refs.push(lib_path + '.ts'); } } if (!(0, lodash_1.isEmpty)(_lib.refs)) { _lib.classes = await this.loadClasses(_lib.refs, modul.name, topic); // handle: ClassesHandle, } return _lib; })); for (const lib of libs) { handle.add(lib.topic, lib.refs, lib.classes); } // } // // if (promises.length > 0) { // await Promise.all(promises); // } return handle.hasAnyClasses() ? handle : null; } async loadClasses(refs, modulName, topic) { let classes = await base_1.ClassLoader.importClassesFromAnyAsync(refs); if (!(0, lodash_1.isEmpty)(classes)) { // @ts-ignore if (Reflect && Reflect['getOwnMetadata']) { classes.forEach(cls => { // @ts-ignore Reflect['defineMetadata'](base_1.__MODULNAME__, modulName, cls); }); } else { classes.forEach(cls => { cls[base_1.__MODULNAME__] = modulName; }); } return classes; } return []; } static getSource(cls) { return base_1.ClassLoader.getSource(cls); } static getModulName(cls) { // @ts-ignore if (Reflect && Reflect['getOwnMetadata']) { // @ts-ignore return Reflect['getOwnMetadata'](base_1.__MODULNAME__, cls); } else { return cls[base_1.__MODULNAME__] ? cls[base_1.__MODULNAME__] : null; } } } exports.ClassesLoader = ClassesLoader; //# sourceMappingURL=ClassesLoader.js.map