77 */
88
99import * as fs from 'fs' ;
10+ import * as glob from 'glob' ;
1011import { interfaces } from 'inversify-express-utils' ;
1112import { Container } from 'inversify' ;
1213import { Types } from '../constants/Types' ;
@@ -37,10 +38,11 @@ class IoC {
3738
3839 public async bindModules ( ) : Promise < void > {
3940 this . bindCore ( ) ;
41+ await this . bindControllers ( ) ;
42+
4043 await this . bindModels ( ) ;
4144 await this . bindRepositories ( ) ;
4245 await this . bindServices ( ) ;
43- await this . bindControllers ( ) ;
4446
4547 this . container = this . customConfiguration ( this . container ) ;
4648 }
@@ -51,7 +53,7 @@ class IoC {
5153 }
5254
5355 private bindModels ( ) : Promise < void > {
54- return this . bindFiles ( '/models' , Model , ( name : any , value : any ) => {
56+ return this . bindFiles ( '/models/**/*.ts ' , Model , ( name : any , value : any ) => {
5557 this . container
5658 . bind < any > ( Types . Model )
5759 . toConstantValue ( value )
@@ -60,7 +62,7 @@ class IoC {
6062 }
6163
6264 private bindRepositories ( ) : Promise < void > {
63- return this . bindFiles ( '/repositories' , Repository , ( name : any , value : any ) => {
65+ return this . bindFiles ( '/repositories/**/*Repository.ts ' , Repository , ( name : any , value : any ) => {
6466 this . container
6567 . bind < any > ( Types . Repository )
6668 . to ( value )
@@ -69,7 +71,7 @@ class IoC {
6971 }
7072
7173 private bindServices ( ) : Promise < void > {
72- return this . bindFiles ( '/services' , Service , ( name : any , value : any ) => {
74+ return this . bindFiles ( '/services/**/*Service.ts ' , Service , ( name : any , value : any ) => {
7375 this . container
7476 . bind < any > ( Types . Service )
7577 . to ( value )
@@ -78,7 +80,7 @@ class IoC {
7880 }
7981
8082 private bindControllers ( ) : Promise < void > {
81- return this . bindFiles ( '/controllers' , Controller , ( name : any , value : any ) => {
83+ return this . bindFiles ( '/controllers/**/*Controller.ts ' , Controller , ( name : any , value : any ) => {
8284 this . container
8385 . bind < any > ( Types . Controller )
8486 . to ( value )
@@ -90,9 +92,10 @@ class IoC {
9092 return new Promise < void > ( ( resolve , reject ) => {
9193 this . getFiles ( path , ( files : string [ ] ) => {
9294 files . forEach ( ( file : any ) => {
93- let fileExport ;
95+ let fileExport , fileClass , fileTarget ;
96+ const isRecursive = file . name . indexOf ( '.' ) > 0 ;
9497 try {
95- fileExport = require ( `${ file . path } / ${ file . fileName } ` ) ;
98+ fileExport = require ( `${ file . path } ` ) ;
9699 } catch ( e ) {
97100 log . warn ( e . message ) ;
98101 return ;
@@ -101,45 +104,84 @@ class IoC {
101104 log . warn ( `Could not find the file ${ file . name } !` ) ;
102105 return ;
103106 }
104- if ( fileExport [ file . name ] === undefined ) {
107+ if ( isRecursive ) {
108+ fileClass = this . getClassOfFileExport ( file . name , fileExport ) ;
109+ fileTarget = this . getTargetOfFile ( file . name , target ) ;
110+
111+ } else {
112+ fileClass = fileExport [ file . name ] ;
113+ fileTarget = target && target [ file . name ] ;
114+ }
115+
116+ if ( fileClass === undefined ) {
105117 log . warn ( `Name of the file '${ file . name } ' does not match to the class name!` ) ;
106118 return ;
107119 }
108- if ( target && target [ file . name ] === undefined ) {
120+
121+ if ( fileTarget === undefined ) {
109122 log . warn ( `Please define your '${ file . name } ' class is in the target constants.` ) ;
110123 return ;
111124 }
112- callback ( target [ file . name ] , fileExport [ file . name ] ) ;
125+
126+ callback ( fileTarget , fileClass ) ;
113127 } ) ;
114128 resolve ( ) ;
115129 } ) ;
116130 } ) ;
117131 }
118132
133+ private getClassOfFileExport ( name : string , fileExport : any ) : any {
134+ const fileParts = name . split ( '.' ) ;
135+ let fileClass = fileExport ;
136+ fileParts . forEach ( ( part ) => {
137+ fileClass = fileClass [ part ] ;
138+ } ) ;
139+ return fileClass ;
140+ }
141+
142+ private getTargetOfFile ( name : string , target : any ) : any {
143+ const fileParts = name . split ( '.' ) ;
144+ let fileTarget = target ;
145+ fileParts . forEach ( ( part ) => {
146+ fileTarget = fileTarget [ part ] ;
147+ } ) ;
148+ return fileTarget ;
149+ }
150+
119151 private getBasePath ( ) : string {
120152 const baseFolder = __dirname . indexOf ( '/src/' ) >= 0 ? '/src/' : '/dist/' ;
121153 const baseRoot = __dirname . substring ( 0 , __dirname . indexOf ( baseFolder ) ) ;
122154 return `${ baseRoot } ${ baseFolder } api` ;
123155 }
124156
125157 private getFiles ( path : string , done : ( files : any [ ] ) => void ) : void {
126- fs . readdir ( this . getBasePath ( ) + path , ( err : any , files : string [ ] ) : void => {
158+ glob ( this . getBasePath ( ) + path , ( err : any , files : string [ ] ) => {
127159 if ( err ) {
128160 log . warn ( `Could not read the folder ${ path } !` ) ;
129161 return ;
130162 }
131- done ( files . map ( ( fileName : string ) => ( {
132- path : this . getBasePath ( ) + path ,
133- fileName : fileName ,
134- name : this . parseName ( fileName )
135- } ) ) ) ;
163+ done ( files . map ( ( p : string ) => this . parseFilePath ( p ) ) ) ;
136164 } ) ;
137165 }
138166
139167 private parseName ( fileName : string ) : string {
140168 return fileName . substring ( 0 , fileName . length - 3 ) ;
141169 }
142170
171+ private parseFilePath ( path : string ) : any {
172+ const filePath = path . substring ( this . getBasePath ( ) . length + 1 ) ;
173+ const dir = filePath . split ( '/' ) [ 0 ] ;
174+ const file = filePath . substr ( dir . length + 1 ) ;
175+ const name = file . replace ( '/' , '.' ) . substring ( 0 , file . length - 3 ) ;
176+ return {
177+ path : path ,
178+ filePath : filePath ,
179+ dir : dir ,
180+ file : file ,
181+ name : name
182+ } ;
183+ }
184+
143185}
144186
145187export const ioc = new IoC ( ) ;
0 commit comments