Skip to content

Commit

Permalink
feat(module): implement the first iteration of the spelunker module
Browse files Browse the repository at this point in the history
The spelunker module will run through a Nest applications dependnecies and print out which modules
import others, their providers, controllers, and exports, and show the way that the providers are
provided, whether through value, factory, or class/standard.
  • Loading branch information
jmcdo29 committed Feb 24, 2020
1 parent f374f1d commit 5f50af2
Show file tree
Hide file tree
Showing 30 changed files with 3,121 additions and 770 deletions.
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tag-version-prefix=""
message="chore(release): %s :tada:"
Empty file added .sonarcloud.properties
Empty file.
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The MIT License

Copyright 2019 Jay McDoniel, contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
96 changes: 95 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,98 @@

## Description

Documentation to come later. Once the package has been developed and is stable.
This module does a bit of a dive through the provided module and reads through the dependency tree from the point of entry given. It will find what a module `imports`, `provides`, has `controllers` for, and `exports` and will recursively search through the dependency tree until all modules have been scanned. For `providers` if there is a custom provider, the Spelunker will do its best to determine if Nest is to use a value, a class/standard, or a factory, and if a factory, what value is to be injected.

## Options

As of now, the module expects two values in its static `explore` method: an `INestApplication`, obtained by using `NestFactory.create()`, and an _optional_ `LoggerService`. If no logger is provided, a Nest Logger with the context "SpelunkerModule" will be created.

## Usage

Much like the [SwaggerModule](https://github.com/nestjs/swagger), the `SpelunkerModule` is not a module that you register within Nest's DI system, but rather use after the DI system has done all of the heavy lifting. Simple usage of the Spelunker could be like:

```ts
async function bootstrap() {
const app = await NestFactory.create(AppModule);
SpelunkerModule.explore(app);
app.listen(3000);
}
```

The `SpelunkerModule` will not get in the way of application bootstrapping, and will still allow for the server to listen.

## Sample Output

Currently, the SpelunkerModule only logs to the terminal, though this is temporary and will eventually be to a file so that everything can be stylized. For now, this is a sample of the output:

```js
{
"AppModule": {
"imports": [
"AnimalsModule"
],
"providers": {
"AppService": {
"method": "standard"
}
},
"controllers": [
"AppController"
],
"exports": []
},
"AnimalsModule": {
"imports": [
"DogsModule",
"CatsModule",
"HamstersModule"
],
"providers": {},
"controllers": [],
"exports": []
},
"DogsModule": {
"imports": [],
"providers": {
"DogsService": {
"method": "standard"
}
},
"controllers": [
"DogsController"
],
"exports": []
},
"CatsModule": {
"imports": [],
"providers": {
"CatsService": {
"method": "standard"
}
},
"controllers": [
"CatsController"
],
"exports": []
},
"HamstersModule": {
"imports": [],
"providers": {
"HamstersService": {
"method": "factory",
"injections": []
}
},
"controllers": [
"HamstersController"
],
"exports": []
}
}
```

In this example, `AppModule` imports `AnimalsModule`, and `AnimalsModule` imports `CatsModule`, `DogsModule`, and `HamstersModule` and each of those has its own set of `providers` and `controllers`.

## Caution

This package is in early development, and any bugs found or improvements that can be thought of would be amazingly helpful. You can [log a bug here](/issues/new), and you can reach out to me on Discord at [PerfectOrphan#6003](https://discordapp.com).
24 changes: 24 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'build',
'ci',
'chore',
'revert',
'feat',
'fix',
'improvement',
'docs',
'style',
'refactor',
'perf',
'test',
],
],
'scope-enum': [2, 'always', ['module', 'deps', 'docs', 'release']],
},
};
1 change: 1 addition & 0 deletions lib/spelunker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './spelunker.module';
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import {
Module,
INestApplication,
LoggerService,
Logger,
HttpModule,
} from '@nestjs/common';
import { NestContainer } from '@nestjs/core';
import { InternalCoreModule } from '@nestjs/core/injector/internal-core-module';
import { Module as NestModule } from '@nestjs/core/injector/module';

@Module({})
export class ExplorerModule {
export class SpelunkerModule {
static explore(
app: INestApplication,
logger: LoggerService = new Logger(ExplorerModule.name),
logger: LoggerService = new Logger(SpelunkerModule.name),
): void {
const dependencyMap = {};
const modulesArray = Array.from(
((app as any).container as NestContainer).getModules().values(),
);
modulesArray
.filter(module => module.metatype !== InternalCoreModule)
.filter(
module =>
module.metatype.name !== InternalCoreModule.name &&
module.metatype.name !== HttpModule.name,
)
.forEach(module => {
dependencyMap[module.metatype.name] = {
imports: ExplorerModule.getImports(module),
providers: ExplorerModule.getProviders(module),
controllers: ExplorerModule.getControllers(module),
exports: ExplorerModule.getExports(module),
imports: SpelunkerModule.getImports(module),
providers: SpelunkerModule.getProviders(module),
controllers: SpelunkerModule.getControllers(module),
exports: SpelunkerModule.getExports(module),
};
});
logger.log(dependencyMap);
}

private static getImports(module: NestModule): string[] {
return Array.from(module.imports)
.filter(module => module.metatype !== InternalCoreModule)
.filter(module => module.metatype.name !== InternalCoreModule.name)
.map(module => module.metatype.name);
}

Expand All @@ -48,22 +51,22 @@ export class ExplorerModule {
providerNames.map(prov => {
const provider = module.providers.get(prov);
const metatype = provider.metatype;
const name = metatype && metatype.name || 'useValue';
const name = (metatype && metatype.name) || 'useValue';
switch (name) {
case 'useValue':
providerList[prov] = {
method: 'value'
method: 'value',
};
break;
case 'useClass':
providerList[prov] = {
method: 'class'
method: 'class',
};
break;
case 'useFactory':
providerList[prov] = {
method: 'factory',
injections: provider.inject
injections: provider.inject,
};
break;
default:
Expand All @@ -76,8 +79,9 @@ export class ExplorerModule {
}

private static getControllers(module: NestModule): string[] {
return Array.from(module.controllers.values())
.map(controller => controller.metatype.name);
return Array.from(module.controllers.values()).map(
controller => controller.metatype.name,
);
}

private static getExports(module: NestModule): string[] {
Expand Down
Loading

0 comments on commit 5f50af2

Please sign in to comment.