|
| 1 | +/** |
| 2 | + * IOC - CONTAINER |
| 3 | + * ---------------------------------------- |
| 4 | + * |
| 5 | + * Bind every controller and service to the ioc container. All controllers |
| 6 | + * will then be bonded to the express structure with their defined routes. |
| 7 | + */ |
| 8 | + |
| 9 | +import * as fs from 'fs'; |
| 10 | +import { interfaces } from 'inversify-express-utils'; |
| 11 | +import { Container } from 'inversify'; |
| 12 | +import { Types } from '../constants/Types'; |
| 13 | +import { Core, Controller, Model, Service, Repository } from '../constants/Targets'; |
| 14 | + |
| 15 | +import { events, EventEmitter } from './api/events'; |
| 16 | +import { Log } from './log'; |
| 17 | + |
| 18 | + |
| 19 | +class IoC { |
| 20 | + |
| 21 | + public container: Container; |
| 22 | + public customConfiguration: (container: Container) => Container; |
| 23 | + |
| 24 | + constructor() { |
| 25 | + this.container = new Container(); |
| 26 | + } |
| 27 | + |
| 28 | + public get Container(): Container { |
| 29 | + return this.container; |
| 30 | + } |
| 31 | + |
| 32 | + public configure(configuration: (container: Container) => Container): void { |
| 33 | + this.customConfiguration = configuration; |
| 34 | + } |
| 35 | + |
| 36 | + public async bindModules(): Promise<void> { |
| 37 | + // this.bindCore(); |
| 38 | + // this.bindModels(); |
| 39 | + // this.bindControllers(); |
| 40 | + |
| 41 | + this.container = this.customConfiguration(this.container); |
| 42 | + } |
| 43 | + |
| 44 | + private bindCore(): void { |
| 45 | + this.container.bind<typeof Log>(Types.Core).toConstantValue(Log).whenTargetNamed(Core.Log); |
| 46 | + } |
| 47 | + |
| 48 | + private async bindControllers(): Promise<void> { |
| 49 | + this.getFiles('/controllers', (files: string[]) => { |
| 50 | + files.forEach((file: any) => { |
| 51 | + console.log(file); |
| 52 | + const fileExport = require(`${file.path}/${file.fileName}`); |
| 53 | + console.log(fileExport); |
| 54 | + this.container |
| 55 | + .bind<interfaces.Controller>(Types.Controller) |
| 56 | + .to(fileExport[file.name]) |
| 57 | + .whenTargetNamed(Controller[file.name]); |
| 58 | + }); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + private bindModels(): void { |
| 63 | + console.log('Models'); |
| 64 | + this.getFiles('/models', (files: string[]) => { |
| 65 | + files.forEach((file: any) => { |
| 66 | + const fileExport = require(`${file.path}/${file.fileName}`); |
| 67 | + this.validateExport(fileExport[file.name]); |
| 68 | + this.validateTarget(Model, file.name); |
| 69 | + console.log(Model[file.name]); |
| 70 | + this.container |
| 71 | + .bind<any>(Types.Model) |
| 72 | + .to(fileExport[file.name]) |
| 73 | + .whenTargetNamed(Model[file.name]); |
| 74 | + }); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + private getBasePath(): string { |
| 79 | + const baseFolder = __dirname.indexOf('/src/') >= 0 ? '/src/' : '/dist/'; |
| 80 | + const baseRoot = __dirname.substring(0, __dirname.indexOf(baseFolder)); |
| 81 | + return `${baseRoot}${baseFolder}api`; |
| 82 | + } |
| 83 | + |
| 84 | + private getFiles(path: string, done: (files: any[]) => void): void { |
| 85 | + fs.readdir(this.getBasePath() + path, (err: any, files: string[]): void => { |
| 86 | + if (err) { |
| 87 | + console.error(err); |
| 88 | + } |
| 89 | + done(files.map((fileName: string) => ({ |
| 90 | + path: this.getBasePath() + path, |
| 91 | + fileName: fileName, |
| 92 | + name: this.parseName(fileName) |
| 93 | + }))); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + private parseName(fileName: string): string { |
| 98 | + return fileName.substring(0, fileName.length - 3); |
| 99 | + } |
| 100 | + |
| 101 | + private validateExport(value: any): void { |
| 102 | + if (!value) { |
| 103 | + throw new Error(`${value} is not defined in the target constants`); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private validateTarget(target: any, value: any): void { |
| 108 | + if (target && target[value] === undefined) { |
| 109 | + throw new Error(`${value} is not defined in the target constants`); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +export const ioc = new IoC(); |
0 commit comments