3
3
4
4
var Promise = require ( 'ember-cli/lib/ext/promise' ) ;
5
5
var Task = require ( 'ember-cli/lib/models/task' ) ;
6
- var npm = require ( 'ember-cli/lib/utilities/npm ' ) ;
6
+ var sh = require ( 'shelljs ' ) ;
7
7
var existsSync = require ( 'exists-sync' ) ;
8
8
var chalk = require ( 'chalk' ) ;
9
9
var path = require ( 'path' ) ;
@@ -18,50 +18,49 @@ module.exports = Task.extend({
18
18
completionOKMessage : '' ,
19
19
completionErrorMessage : 'Error installing packages. Did you misspelt it?' ,
20
20
21
- init : function ( ) {
22
- this . npm = this . npm || require ( 'npm' ) ;
23
- } ,
24
21
run : function ( options ) {
25
22
this . packages = options . packages || [ ] ;
26
23
this . skipInjection = options . skipInjection || false ;
27
24
this . autoInjection = options . autoInjection || false ;
28
25
this . disableLogger ( ) ;
29
26
30
- this . ui . startProgress ( chalk . green ( 'Installing 3rd party package:' ,
27
+ this . ui . startProgress ( chalk . green ( 'Installing 3rd party package:' ,
31
28
this . packages . join ( ', ' ) ) , chalk . green ( '.' ) ) ;
32
29
33
- this . npmOptions = {
34
- logLevel : 'error' ,
35
- logstream : this . ui . outputStream ,
36
- color : 'always' ,
37
- optional : true ,
38
- 'save-dev' : true ,
39
- 'save-exact' : ! ! options [ 'save-exact' ]
40
- } ;
41
-
42
- return npm ( 'install' , this . packages , this . npmOptions , this . npm )
43
- . then ( function ( npmresp ) {
44
- if ( this . skipInjection ) {
45
- return this . announceOKCompletion ( ) ;
46
- }
30
+ this . npmOptions = ' --loglevel error --color always --optional --save-dev ' +
31
+ '--save-exact ' + ! ! options [ 'save-exact' ] ;
47
32
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
- }
33
+ var npmCommand = 'npm install ' + this . packages . join ( ' ' ) + this . npmOptions ;
34
+
35
+ return new Promise ( function ( resolve , reject ) {
36
+ return sh . exec ( npmCommand , { silent : true } , function ( code , stdout , stderr ) {
37
+ if ( code !== 0 ) reject ( ) ;
38
+ else resolve ( stdout ) ;
39
+ } ) ;
40
+ } )
41
+ . then ( function ( npmresp ) {
42
+
43
+ if ( this . skipInjection ) {
44
+ return this . announceOKCompletion ( ) ;
45
+ }
56
46
57
- return this . announceOKCompletion ( ) ;
47
+ if ( this . autoInjection ) {
48
+ var pkg = this . packages [ 0 ] ;
49
+
50
+ if ( existsSync ( path . resolve ( process . cwd ( ) , 'src' , 'app.ts' ) ) ) {
51
+ var entryPoint = path . resolve ( process . cwd ( ) , 'src' , 'app.ts' ) ;
52
+ var packageData = this . parseFile ( pkg ) ;
53
+ this . importInBootstrap ( entryPoint , pkg , packageData ) ;
58
54
}
59
55
60
- return this . installProcedure ( )
61
- . then ( function ( resp ) {
62
- return this . announceOKCompletion ( ) ;
63
- } . bind ( this ) ) ;
64
- } . bind ( this ) ) ;
56
+ return this . announceOKCompletion ( ) ;
57
+ }
58
+
59
+ return this . installProcedure ( ) ;
60
+ } . bind ( this ) )
61
+ . then ( function ( resp ) {
62
+ return this . announceOKCompletion ( ) ;
63
+ } . bind ( this ) ) ;
65
64
} ,
66
65
67
66
installProcedure : function ( ) {
@@ -83,13 +82,20 @@ module.exports = Task.extend({
83
82
'as authentic: ' + allPackages . toUninstall . join ( ', ' ) ;
84
83
this . ui . writeLine ( chalk . yellow ( msg ) ) ;
85
84
86
- return npm ( 'uninstall' , allPackages . toUninstall , this . npmOptions , this . npm )
87
- . then ( function ( ) {
88
- return this . processPackages ( allPackages . toProcess )
89
- . then ( function ( resp ) {
90
- return resolve ( resp ) ;
91
- } ) ;
92
- } . bind ( this ) ) ;
85
+ var npmCommand = 'npm uninstall ' + allPackages . toUninstall . join ( ' ' ) + this . npmOptions ;
86
+
87
+ return new Promise ( function ( resolve , reject ) {
88
+ return sh . exec ( npmCommand , { silent : true } , function ( code , stdout , stderr ) {
89
+ if ( code !== 0 ) reject ( ) ;
90
+ else resolve ( stdout ) ;
91
+ } ) ;
92
+ } )
93
+ . then ( function ( ) {
94
+ return this . processPackages ( allPackages . toProcess )
95
+ . then ( function ( resp ) {
96
+ return resolve ( resp ) ;
97
+ } ) ;
98
+ } . bind ( this ) ) ;
93
99
}
94
100
else {
95
101
return this . processPackages ( allPackages . toProcess )
@@ -133,7 +139,7 @@ module.exports = Task.extend({
133
139
return new Promise ( function ( resolve , reject ) {
134
140
var packageData ;
135
141
136
- var msg = 'Customize the injection of ' +
142
+ var msg = 'Customize the injection of ' +
137
143
chalk . yellow ( path . basename ( packageName ) ) + '? (Y/n)' ;
138
144
139
145
return this . ui . prompt ( {
@@ -226,7 +232,7 @@ module.exports = Task.extend({
226
232
type : 'input' ,
227
233
name : 'sel' ,
228
234
message : msg ,
229
- filter : function ( val ) { return val . trim ( ) ; } ,
235
+ filter : function ( val ) { return val . trim ( ) ; } ,
230
236
validate : function ( value ) {
231
237
return value > 0 && value <= packageData [ componentKey ] . length ? true : 'Enter a valid value' ;
232
238
}
@@ -260,8 +266,8 @@ module.exports = Task.extend({
260
266
packageName ,
261
267
packageData [ componentKey ] [ componentIndex ] ) ;
262
268
263
- this . injectItem ( componentKey . toLowerCase ( ) ,
264
- packageData [ componentKey ] [ componentIndex ] ,
269
+ this . injectItem ( componentKey . toLowerCase ( ) ,
270
+ packageData [ componentKey ] [ componentIndex ] ,
265
271
possibleFiles [ fileIndex ] ) ;
266
272
267
273
this . ui . writeLine ( chalk . green ( 'Successfully injected.' ) ) ;
@@ -336,8 +342,8 @@ module.exports = Task.extend({
336
342
337
343
if ( removeNextLine ) {
338
344
arr . splice ( index , 1 ) ;
339
- if ( / ; / . test ( line ) ) {
340
- removeNextLine = false ;
345
+ if ( / ; / . test ( line ) ) {
346
+ removeNextLine = false ;
341
347
}
342
348
}
343
349
} ) ;
@@ -348,12 +354,12 @@ module.exports = Task.extend({
348
354
function generateFlatImportLine ( arr , fromIndex ) {
349
355
var lineArr = [ ] ;
350
356
var lastIndex ;
351
-
357
+
352
358
arr . forEach ( function ( line , index ) {
353
359
if ( index >= fromIndex && ! lastIndex ) {
354
360
lineArr . push ( line ) ;
355
- if ( / ; / . test ( line ) ) {
356
- lastIndex = true ;
361
+ if ( / ; / . test ( line ) ) {
362
+ lastIndex = true ;
357
363
}
358
364
}
359
365
} ) ;
@@ -443,7 +449,7 @@ module.exports = Task.extend({
443
449
444
450
if ( / \) / . test ( line ) ) {
445
451
replace = line . match ( / \( ( .* ?) \) / ) [ 0 ] ;
446
- }
452
+ }
447
453
else {
448
454
replace = contentsArr . splice ( index , contentsArr . length - 1 ) . join ( '\n' ) . replace ( / \n / g, '' ) ;
449
455
replace = replace . match ( / \( ( .* ?) \) / ) [ 0 ] ;
@@ -495,7 +501,7 @@ module.exports = Task.extend({
495
501
match = match . replace ( / [ \[ \] ] / g, '' ) ;
496
502
match = match . split ( ',' ) ;
497
503
match . push ( name ) ;
498
- match = match . filter ( function ( n ) { return n !== '' ; } ) ;
504
+ match = match . filter ( function ( n ) { return n !== '' ; } ) ;
499
505
contentsArr [ index ] = contentsArr [ index ] . replace ( replace , '[' + match . join ( ',' ) + ']' ) ;
500
506
}
501
507
} . bind ( this ) ) ;
@@ -505,14 +511,14 @@ module.exports = Task.extend({
505
511
506
512
parseFile : function ( packageName ) {
507
513
var packagePath = path . join ( process . cwd ( ) , 'node_modules' , packageName , packageName + '.ts' ) ;
508
-
514
+
509
515
if ( ! existsSync ( packagePath ) ) {
510
516
return false ;
511
517
}
512
518
513
519
var contents = fs . readFileSync ( packagePath , 'utf8' ) ;
514
520
var data = { } ;
515
-
521
+
516
522
data . Component = [ ] ;
517
523
data . Directive = [ ] ;
518
524
data . Pipe = [ ] ;
0 commit comments