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