Skip to content

Commit

Permalink
Merge pull request #57 from micalevisk/fix-issue-56
Browse files Browse the repository at this point in the history
fix: exclude ignored modules from other modules `imports` array
  • Loading branch information
jmcdo29 authored Oct 5, 2024
2 parents 4c0dfac + e3ac557 commit 974010c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-meals-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nestjs-spelunker': patch
---

do not display excluded modules on `imports` list of other modules
18 changes: 12 additions & 6 deletions src/exploration.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { SpelunkedTree } from './spelunker.interface';
import { UndefinedProvider } from './spelunker.messages';

export type ExplorationOpts = {
ignoreImports: Array<RegExp | ((moduleName: string) => boolean)>;
ignoreImports?: Array<RegExp | ((moduleName: string) => boolean)>;
};

type ShouldIncludeModuleFn = (module: NestModule) => boolean;

export class ExplorationModule {
static explore(
app: INestApplicationContext,
Expand All @@ -29,8 +31,9 @@ export class ExplorationModule {
? (moduleName: string) => ignoreImportFnOrRegex.test(moduleName)
: ignoreImportFnOrRegex,
);

const shouldIncludeModule = (module: NestModule): boolean => {
const shouldIncludeModule: ShouldIncludeModuleFn = (
module: NestModule,
): boolean => {
const moduleName = module.metatype.name;
return (
module.metatype !== InternalCoreModule &&
Expand All @@ -44,7 +47,7 @@ export class ExplorationModule {
if (shouldIncludeModule(nestjsModule)) {
dependencyMap.push({
name: nestjsModule.metatype.name,
imports: this.getImports(nestjsModule),
imports: this.getImports(nestjsModule, shouldIncludeModule),
providers: this.getProviders(nestjsModule),
controllers: this.getControllers(nestjsModule),
exports: this.getExports(nestjsModule),
Expand All @@ -54,11 +57,14 @@ export class ExplorationModule {
return dependencyMap;
}

private static getImports(module: NestModule): string[] {
private static getImports(
module: NestModule,
shouldIncludeModuleFn: ShouldIncludeModuleFn,
): string[] {
// NOTE: Using for..of here instead of filter+map for performance reasons.
const importsNames: string[] = [];
for (const importedModule of module.imports.values()) {
if (importedModule.metatype.name !== InternalCoreModule.name) {
if (shouldIncludeModuleFn(importedModule)) {
importsNames.push(importedModule.metatype.name);
}
}
Expand Down

0 comments on commit 974010c

Please sign in to comment.