UNPKG

@brewww/nestjs-plugin-module

Version:

<p align="center"> <a href="http://brewww.com/" target="_blank"><img src="https://github.com/BrewInteractive/nestjs-plugin-module/blob/main/Brew-Logo-Small.png?raw=true" width="300" alt="Brew Logo" /></a> </p>

81 lines 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginTraverser = void 0; const bluebirdPromise = require("bluebird"); const fs = require("fs"); const path = require("path"); const node_modules = require('node_modules-path'); class PluginTraverser { constructor(pluginModuleOptions) { this._directories = [path.resolve(node_modules())]; if (pluginModuleOptions.directories && pluginModuleOptions.directories.length > 0) this._directories = pluginModuleOptions.directories; } async traverseDirectoriesAsync() { const modules = []; await bluebirdPromise.mapSeries(this._directories, async (parentDirectoryPath) => { if ((await fs.promises.stat(parentDirectoryPath)).isDirectory()) await this.explorePluginDirectoryAsync(parentDirectoryPath, modules); }); return modules; } async explorePluginDirectoryAsync(directoryPath, modules) { if (this.isPluginDirectory(directoryPath, '')) { modules.push(...this.processDirectory(directoryPath, '')); return; } const dirents = await fs.promises.readdir(directoryPath, { withFileTypes: true, }); for (const dirent of dirents) { if (dirent.isDirectory()) { const subdir = path.join(directoryPath, dirent.name); await this.explorePluginDirectoryAsync(subdir, modules); } } } isPluginDirectory(parentDirectory, directoryName) { if (this.packageJsonExists(parentDirectory, directoryName)) { const packageJsonPath = this.createPackageJsonPath(parentDirectory, directoryName); if (this.isPluginModule(this.parsePackageJson(packageJsonPath))) return true; } return false; } processDirectory(parentDirectory, directoryName) { return this.importModule(this.createModulePath(parentDirectory, directoryName)); } packageJsonExists(parentDirectory, directoryName) { const packageJsonPath = this.createPackageJsonPath(parentDirectory, directoryName); return fs.existsSync(packageJsonPath); } createPackageJsonPath(parentDirectory, directoryName) { return path.join(parentDirectory, directoryName, 'package.json'); } parsePackageJson(packageJsonPath) { return JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); } isPluginModule(packageJson) { return (packageJson.pluginModule?.name !== undefined && packageJson.pluginModule?.displayName !== undefined); } createModulePath(parentDirectory, directoryName) { const directoryPath = path.join(parentDirectory, directoryName); const srcFolder = path.join(directoryPath, './src'); const dirSrcFolder = path.join(directoryPath, './dist/src'); return fs.existsSync(srcFolder) ? path.join(srcFolder) : path.join(dirSrcFolder); } importModule(modulePath) { try { return Object.values(require(modulePath)); } catch (error) { console.error(`Unable to import module at path: ${modulePath}.`, error); } } } exports.PluginTraverser = PluginTraverser; //# sourceMappingURL=plugin-traverser.js.map