Skip to content

Commit

Permalink
feat(inversify): Automatically load annotated classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Koenders committed Sep 10, 2018
1 parent deb9ca9 commit c02062a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/ioc/loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { ContainerModule } from 'inversify';
import * as fs from 'fs';
import * as path from 'path';
import { container } from './ioc';

class Loader {
public static load(baseDir: string) {
Loader.readDir(baseDir);
public static load(directories: string[]) {
directories.forEach(Loader.readDir);

container.load(Loader.getContainerModuleForDependencies());
}

private static readDir(dir: string) {
Expand All @@ -25,6 +29,13 @@ class Loader {
require(filePath);
}
}

private static getContainerModuleForDependencies() {
return new ContainerModule((bind) => {
const provideMetadata: any[] = Reflect.getMetadata('inversify-binding-decorators:provide', Reflect) || [];
provideMetadata.map(metadata => metadata.constraint(bind, metadata.implementationType));
});
}
}

export default Loader;
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class Main {
}

private loadJsFiles(loadPaths: string[]) {
loadPaths.forEach((dir) => {
Loader.load(path.join(__dirname, dir));
});
loadPaths = loadPaths.map(dir => path.join(__dirname, dir));

Loader.load(loadPaths);
}

public get App(): express.Application {
Expand Down

0 comments on commit c02062a

Please sign in to comment.