@@ -12,7 +12,6 @@ var _ = require('lodash');
12
12
var glob = require ( 'glob' ) ;
13
13
var appRoot = path . resolve ( './src' ) ;
14
14
var nodeModules = path . resolve ( './node_modules' ) ;
15
- var checkDirs = [ 'components' , 'providers' , 'directives' , 'pipes' ] ;
16
15
17
16
module . exports = Task . extend ( {
18
17
command : '' ,
@@ -46,17 +45,25 @@ module.exports = Task.extend({
46
45
return this . announceOKCompletion ( ) ;
47
46
}
48
47
49
- return this . installProcedure ( this . autoInjection )
48
+ if ( this . autoInjection ) {
49
+ if ( existsSync ( path . resolve ( process . cwd ( ) , 'src' , 'app.ts' ) ) ) {
50
+ var entryPoint = path . resolve ( process . cwd ( ) , 'src' , 'app.ts' ) ;
51
+ var packageData = this . parseFile ( this . packages [ 0 ] ) ;
52
+ this . importInBootstrap ( entryPoint , this . packages [ 0 ] , packageData ) ;
53
+ }
54
+
55
+ return this . announceOKCompletion ( ) ;
56
+ }
57
+
58
+ return this . installProcedure ( )
50
59
. then ( function ( resp ) {
51
60
return this . announceOKCompletion ( ) ;
52
61
} . bind ( this ) ) ;
53
62
} . bind ( this ) ) ;
54
63
} ,
55
64
56
- installProcedure : function ( autoInjection ) {
57
- var allPackages = { } ;
58
- allPackages . toUninstall = [ ] ;
59
- allPackages . toProcess = [ ] ;
65
+ installProcedure : function ( ) {
66
+ var allPackages = { toUninstall : [ ] , toProcess : [ ] } ;
60
67
var pkgSrcPath ;
61
68
var pkgDirs ;
62
69
@@ -76,46 +83,35 @@ module.exports = Task.extend({
76
83
77
84
return npm ( 'uninstall' , allPackages . toUninstall , this . npmOptions , this . npm )
78
85
. then ( function ( ) {
79
- return this . processPackages ( allPackages . toProcess , autoInjection )
86
+ return this . processPackages ( allPackages . toProcess )
80
87
. then ( function ( resp ) {
81
- resolve ( resp ) ;
88
+ return resolve ( resp ) ;
82
89
} ) ;
83
90
} . bind ( this ) ) ;
84
91
}
85
92
else {
86
- return this . processPackages ( allPackages . toProcess , autoInjection )
93
+ return this . processPackages ( allPackages . toProcess )
87
94
. then ( function ( resp ) {
88
- resolve ( resp ) ;
95
+ return resolve ( resp ) ;
89
96
} ) ;
90
97
}
91
98
92
99
} . bind ( this ) ) ;
93
100
} . bind ( this ) ) ;
94
101
} ,
95
102
96
- processPackages : function ( packages , autoInjection ) {
103
+ processPackages : function ( packages ) {
97
104
if ( ! packages . length ) {
98
105
this . ui . writeLine ( chalk . yellow ( 'No package to process. Quitting.' ) ) ;
99
106
return Promise . resolve ( ) ;
100
107
} else {
101
108
this . ui . writeLine ( chalk . green ( 'Package successfully installed.' ) ) ;
102
- this . ui . writeLine ( '' ) ;
103
-
104
- if ( autoInjection ) {
105
- if ( existsSync ( path . resolve ( process . cwd ( ) , 'src' , 'app.ts' ) ) ) {
106
- var entryPoint = path . resolve ( process . cwd ( ) , 'src' , 'app.ts' ) ;
107
- var packageData = this . parseFile ( this . packages [ 0 ] ) ;
108
- this . importInBootstrap ( entryPoint , this . packages [ 0 ] , packageData ) ;
109
- }
110
-
111
- return Promise . resolve ( ) ;
112
- }
113
109
114
110
return this . ui . prompt ( {
115
111
type : 'text' ,
116
112
name : 'confirm' ,
117
- message : 'Would you like to inject the installed packages into your app automatically ? (N/y )' ,
118
- default : function ( ) { return 'N ' ; } ,
113
+ message : 'Inject the installed package into your app? (Y/n )' ,
114
+ default : function ( ) { return 'Y ' ; } ,
119
115
validate : function ( value ) {
120
116
return / [ Y N ] / i. test ( value ) ? true : 'Enter Y(es) or N(o)' ;
121
117
}
@@ -125,79 +121,54 @@ module.exports = Task.extend({
125
121
return Promise . resolve ( ) ;
126
122
}
127
123
else {
128
- return this . ui . prompt ( {
129
- type : 'text' ,
130
- name : 'entry' ,
131
- message : 'What is the path to the file which bootstraps your app?' ,
132
- default : function ( ) {
133
- if ( existsSync ( path . join ( appRoot , 'app.ts' ) ) ) {
134
- return path . join ( appRoot , 'app.ts' ) ;
135
- } else {
136
- var possibleFiles = glob . sync ( appRoot , '*.ts' ) ;
137
- if ( possibleFiles . length ) {
138
- return possibleFiles [ 0 ] ;
139
- } else {
140
- return '' ;
141
- }
142
- }
143
- } ,
144
- validate : function ( val ) {
145
- return ( existsSync ( val ) ) ? true : 'File not exists.' ;
146
- }
147
- } )
148
- . then ( function ( boot ) {
149
- var entryPoint = boot . entry ;
150
- return packages . reduce ( function ( promise , packageName ) {
151
- return promise . then ( function ( ) {
152
- return this . parsePackage ( packageName , entryPoint , autoInjection ) ;
153
- } . bind ( this ) ) ;
154
- } . bind ( this ) , Promise . resolve ( ) ) ;
155
- } . bind ( this ) ) ;
124
+ return this . parsePackage ( packages [ 0 ] ) ;
156
125
}
157
126
} . bind ( this ) ) ;
158
-
159
127
}
160
128
} ,
161
129
162
- parsePackage : function ( packageName , entryPoint , autoInjection ) {
130
+ parsePackage : function ( packageName , entryPoint ) {
163
131
return new Promise ( function ( resolve , reject ) {
164
132
var packageData ;
165
133
166
- if ( autoInjection ) {
167
- packageData = this . parseFile ( packageName ) ;
168
- this . importInBootstrap ( entryPoint , packageName , packageData ) ;
169
- return resolve ( ) ;
170
- }
171
-
172
- this . ui . writeLine ( '' ) ;
173
-
174
- var msg = 'Would you like to customize the injection of ' +
175
- chalk . yellow ( path . basename ( packageName ) ) + '? (N/y)' ;
134
+ var msg = 'Customize the injection of ' +
135
+ chalk . yellow ( path . basename ( packageName ) ) + '? (Y/n)' ;
176
136
177
137
return this . ui . prompt ( {
178
138
type : 'input' ,
179
139
name : 'option' ,
180
140
message : msg ,
181
- default : function ( ) { return 'N ' ; } ,
141
+ default : function ( ) { return 'Y ' ; } ,
182
142
validate : function ( value ) {
183
143
return / [ Y N ] / i. test ( value ) ? true : 'Enter Y(es) or N(o)' ;
184
144
}
185
145
} )
186
146
. then ( function ( answer ) {
187
147
if ( answer . option . toLowerCase ( ) . substring ( 0 , 1 ) === 'y' ) {
188
- packageData = this . parseFile ( packageName ) ;
189
- this . importInBootstrap ( entryPoint , packageName , packageData ) ;
190
-
191
148
return this . customImport ( packageName )
192
149
. then ( function ( ) {
193
- return resolve ( ) ;
194
- } ) ;
150
+ packageData = this . parseFile ( packageName ) ;
151
+ if ( packageData . Provider && packageData . Provider . length ) {
152
+ return this . importInBootstrapPrompt ( packageName , packageData )
153
+ . then ( function ( ) {
154
+ resolve ( ) ;
155
+ } ) ;
156
+ } else {
157
+ return resolve ( ) ;
158
+ }
159
+ } . bind ( this ) ) ;
195
160
}
196
161
else {
197
162
packageData = this . parseFile ( packageName ) ;
198
- this . importInBootstrap ( entryPoint , packageName , packageData ) ;
199
-
200
- return resolve ( ) ;
163
+ if ( packageData . Provider && packageData . Provider . length ) {
164
+ return this . importInBootstrapPrompt ( packageName , packageData )
165
+ . then ( function ( ) {
166
+ resolve ( ) ;
167
+ } ) ;
168
+ }
169
+ else {
170
+ return resolve ( ) ;
171
+ }
201
172
}
202
173
} . bind ( this ) ) ;
203
174
} . bind ( this ) ) ;
@@ -292,9 +263,11 @@ module.exports = Task.extend({
292
263
possibleFiles [ fileIndex ] ) ;
293
264
294
265
this . ui . writeLine ( chalk . green ( 'Successfully injected.' ) ) ;
295
- this . ui . writeLine ( '' ) ;
296
266
297
- this . customImport ( packageName ) ;
267
+ return this . customImport ( packageName )
268
+ . then ( function ( ) {
269
+ resolve ( ) ;
270
+ } ) ;
298
271
299
272
} . bind ( this ) ) ;
300
273
} . bind ( this ) ) ;
@@ -388,11 +361,54 @@ module.exports = Task.extend({
388
361
389
362
} ,
390
363
364
+ importInBootstrapPrompt : function ( packageName , packageData ) {
365
+ return new Promise ( function ( resolve , reject ) {
366
+ return this . ui . prompt ( {
367
+ type : 'text' ,
368
+ name : 'option' ,
369
+ message : 'Inject providers into bootstrap script? (Y/n)' ,
370
+ default : function ( ) { return 'Y' ; } ,
371
+ validate : function ( value ) {
372
+ return / [ Y N ] / i. test ( value ) ? true : 'Enter Y(es) or N(o)' ;
373
+ }
374
+ } )
375
+ . then ( function ( answer ) {
376
+ if ( answer . option . toLowerCase ( ) . substring ( 0 , 1 ) === 'y' ) {
377
+ return this . ui . prompt ( {
378
+ type : 'text' ,
379
+ name : 'entry' ,
380
+ message : 'Path to the file which bootstraps your app?' ,
381
+ default : function ( ) {
382
+ if ( existsSync ( path . join ( appRoot , 'app.ts' ) ) ) {
383
+ return path . join ( appRoot , 'app.ts' ) ;
384
+ } else {
385
+ var possibleFiles = glob . sync ( appRoot , '*.ts' ) ;
386
+ if ( possibleFiles . length ) {
387
+ return possibleFiles [ 0 ] ;
388
+ } else {
389
+ return '' ;
390
+ }
391
+ }
392
+ } ,
393
+ validate : function ( val ) {
394
+ return ( existsSync ( val ) ) ? true : 'File not exists.' ;
395
+ }
396
+ } )
397
+ . then ( function ( answer ) {
398
+ var entryPoint = answer . entry ;
399
+ this . importInBootstrap ( entryPoint , packageName , packageData ) ;
400
+ return resolve ( ) ;
401
+ } . bind ( this ) ) ;
402
+ }
403
+ else {
404
+ return resolve ( ) ;
405
+ }
406
+ } . bind ( this ) ) ;
407
+ } . bind ( this ) ) ;
408
+ } ,
409
+
391
410
importInBootstrap : function ( entryPoint , packageName , packageData ) {
392
411
var providers = packageData . Provider ;
393
- var directives = packageData . Directive ;
394
- var pipes = packageData . Pipe ;
395
- var all = [ ] . concat ( providers ) . concat ( directives ) . concat ( pipes ) ;
396
412
var contents = fs . readFileSync ( entryPoint , 'utf8' ) ;
397
413
var contentsArr = contents . split ( '\n' ) ;
398
414
var lastIndex ;
@@ -409,10 +425,10 @@ module.exports = Task.extend({
409
425
return false ;
410
426
}
411
427
412
- var imports = 'import {' + all . join ( ',' ) + '} from \'' + packageName + '\';' ;
428
+ var imports = 'import {' + providers . join ( ',' ) + '} from \'' + packageName + '\';' ;
413
429
414
430
if ( imports . length > 100 ) {
415
- imports = 'import {\n' + all . join ( ' ,\n' ) + '}\n from \'' + packageName + '\';' ;
431
+ imports = 'import {\n' + providers . join ( ' ,\n' ) + '}\n from \'' + packageName + '\';' ;
416
432
}
417
433
418
434
contentsArr . forEach ( function ( line , index ) {
@@ -456,7 +472,7 @@ module.exports = Task.extend({
456
472
contentsArr . splice ( lastIndex + 1 , 0 , imports ) ;
457
473
458
474
fs . writeFileSync ( entryPoint , contentsArr . join ( '\n' ) , 'utf8' ) ;
459
- this . ui . writeLine ( chalk . green ( 'Package imported in' , entryPoint ) ) ;
475
+ this . ui . writeLine ( chalk . green ( 'Providers imported in' , entryPoint ) ) ;
460
476
} ,
461
477
462
478
injectItem : function ( type , name , file ) {
@@ -465,6 +481,10 @@ module.exports = Task.extend({
465
481
var replace ;
466
482
var match ;
467
483
484
+ if ( type === 'component' ) {
485
+ type = 'directive' ;
486
+ }
487
+
468
488
contentsArr . forEach ( function ( line , index ) {
469
489
var regExp = new RegExp ( type ) ;
470
490
if ( regExp . test ( line ) ) {
@@ -491,13 +511,17 @@ module.exports = Task.extend({
491
511
var contents = fs . readFileSync ( packagePath , 'utf8' ) ;
492
512
var data = { } ;
493
513
514
+ data . Component = [ ] ;
494
515
data . Directive = [ ] ;
495
516
data . Pipe = [ ] ;
496
517
data . Provider = [ ] ;
497
518
data . styleUrl = [ ] ;
498
519
499
520
var contentsArr = contents . split ( '\n' ) ;
500
521
contentsArr . forEach ( function ( line , index ) {
522
+ if ( / c o m p o n e n t s : / . test ( line ) ) {
523
+ data . Component = this . parseData ( line ) ;
524
+ }
501
525
if ( / d i r e c t i v e s : / . test ( line ) ) {
502
526
data . Directive = this . parseData ( line ) ;
503
527
}
@@ -530,8 +554,6 @@ module.exports = Task.extend({
530
554
} ,
531
555
532
556
checkIfPackageIsAuthentic : function ( pkgName ) {
533
- return true ;
534
-
535
557
if ( ! existsSync ( path . join ( nodeModules , pkgName ) ) ) {
536
558
return false ;
537
559
}
@@ -548,7 +570,6 @@ module.exports = Task.extend({
548
570
} ,
549
571
550
572
announceOKCompletion : function ( ) {
551
- this . ui . writeLine ( '' ) ;
552
573
this . completionOKMessage = 'Done.' ;
553
574
this . ui . writeLine ( chalk . green ( this . completionOKMessage ) ) ;
554
575
this . done ( ) ;
0 commit comments