UNPKG

rucken

Version:

Console tools and scripts for nx and not only that I (EndyKaufman) use to automate the workflow and speed up the development process

72 lines (71 loc) 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConsoleScanner = void 0; const tslib_1 = require("tslib"); const constants_1 = require("./constants"); class ConsoleScanner { /** * Get all the modules */ getModules(modulesContainer, include = []) { const allModules = [...modulesContainer.values()]; if (!include.length) { return allModules; } return allModules.filter(({ metatype }) => include.some((item) => item === metatype)); } /** * Get a list of classes methods */ getInstanceMethods(instance) { return Object.getOwnPropertyNames(instance) .concat(Object.getOwnPropertyNames(instance.__proto__)) .filter((m) => Reflect.hasMetadata(constants_1.COMMAND_METADATA_NAME, instance, m)); } /** * Scan an application * @param app * @param includedModules */ scan(app, includedModules) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const set = new Set(); const container = app.container; const modules = this.getModules(container.getModules(), includedModules); yield Promise.all(modules.map((m) => { return Promise.all(Array.from(m.providers.values()).map((p) => tslib_1.__awaiter(this, void 0, void 0, function* () { const { metatype, token } = p; if (typeof metatype !== 'function') { return; } // ignore providers without instance if (!p.instance) { return; } const consoleMetadata = Reflect.getMetadata(constants_1.CONSOLE_METADATA_NAME, p.instance.constructor); // ignore providers without the console decorator if (!consoleMetadata) { return; } // get the provider instance from the container const instance = yield app.resolve(token, undefined, { strict: false, }); const methods = this.getInstanceMethods(instance); // get the metadata of the methods const methodsMetadata = methods.map((methodMetadata) => ({ name: methodMetadata, metadata: Reflect.getMetadata(constants_1.COMMAND_METADATA_NAME, instance, methodMetadata), })); set.add({ instance, metadata: consoleMetadata, methods: methodsMetadata, }); }))); })); return set; }); } } exports.ConsoleScanner = ConsoleScanner;