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 • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConsoleScanner = void 0;
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) {
const instanceObj = instance;
const prototype = Object.getPrototypeOf(instanceObj);
return [
...Object.getOwnPropertyNames(instanceObj),
...Object.getOwnPropertyNames(prototype),
].filter((m) => Reflect.hasMetadata(constants_1.COMMAND_METADATA_NAME, instanceObj, m));
}
/**
* Scan an application
* @param app
* @param includedModules
*/
async scan(app, includedModules) {
const set = new Set();
const container = app
.container;
const modules = this.getModules(container.getModules(), includedModules);
await Promise.all(modules.map(async (module) => {
await Promise.all(Array.from(module.providers.values()).map(async (provider) => {
const { metatype, token } = provider;
if (typeof metatype !== 'function') {
return;
}
// ignore providers without instance
if (!provider.instance) {
return;
}
const consoleMetadata = Reflect.getMetadata(constants_1.CONSOLE_METADATA_NAME, provider.instance.constructor);
// ignore providers without the console decorator
if (!consoleMetadata) {
return;
}
// Use the already-created provider instance from the container
// This instance has all dependencies injected by NestJS
const instance = provider.instance;
const methods = this.getInstanceMethods(instance);
// get the metadata of the methods
const methodsMetadata = methods.map((methodName) => ({
name: methodName,
metadata: Reflect.getMetadata(constants_1.COMMAND_METADATA_NAME, instance, methodName),
}));
set.add({
instance,
metadata: consoleMetadata,
methods: methodsMetadata,
});
}));
}));
return set;
}
}
exports.ConsoleScanner = ConsoleScanner;
//# sourceMappingURL=scanner.js.map