33
44var Promise = require ( 'ember-cli/lib/ext/promise' ) ;
55var Task = require ( 'ember-cli/lib/models/task' ) ;
6- var npm = require ( 'ember-cli/lib/utilities/npm ' ) ;
6+ var sh = require ( 'shelljs ' ) ;
77var existsSync = require ( 'exists-sync' ) ;
88var chalk = require ( 'chalk' ) ;
99var path = require ( 'path' ) ;
@@ -18,50 +18,49 @@ module.exports = Task.extend({
1818 completionOKMessage : '' ,
1919 completionErrorMessage : 'Error installing packages. Did you misspelt it?' ,
2020
21- init : function ( ) {
22- this . npm = this . npm || require ( 'npm' ) ;
23- } ,
2421 run : function ( options ) {
2522 this . packages = options . packages || [ ] ;
2623 this . skipInjection = options . skipInjection || false ;
2724 this . autoInjection = options . autoInjection || false ;
28- this . disableLogger ( ) ;
25+ // this.disableLogger();
2926
30- this . ui . startProgress ( chalk . green ( 'Installing 3rd party package:' ,
27+ this . ui . startProgress ( chalk . green ( 'Installing 3rd party package:' ,
3128 this . packages . join ( ', ' ) ) , chalk . green ( '.' ) ) ;
3229
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' ] ;
4732
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+ }
5646
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 ) ;
5854 }
5955
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 ) ) ;
6564 } ,
6665
6766 installProcedure : function ( ) {
@@ -83,13 +82,20 @@ module.exports = Task.extend({
8382 'as authentic: ' + allPackages . toUninstall . join ( ', ' ) ;
8483 this . ui . writeLine ( chalk . yellow ( msg ) ) ;
8584
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 ) ) ;
9399 }
94100 else {
95101 return this . processPackages ( allPackages . toProcess )
@@ -133,7 +139,7 @@ module.exports = Task.extend({
133139 return new Promise ( function ( resolve , reject ) {
134140 var packageData ;
135141
136- var msg = 'Customize the injection of ' +
142+ var msg = 'Customize the injection of ' +
137143 chalk . yellow ( path . basename ( packageName ) ) + '? (Y/n)' ;
138144
139145 return this . ui . prompt ( {
@@ -226,7 +232,7 @@ module.exports = Task.extend({
226232 type : 'input' ,
227233 name : 'sel' ,
228234 message : msg ,
229- filter : function ( val ) { return val . trim ( ) ; } ,
235+ filter : function ( val ) { return val . trim ( ) ; } ,
230236 validate : function ( value ) {
231237 return value > 0 && value <= packageData [ componentKey ] . length ? true : 'Enter a valid value' ;
232238 }
@@ -260,8 +266,8 @@ module.exports = Task.extend({
260266 packageName ,
261267 packageData [ componentKey ] [ componentIndex ] ) ;
262268
263- this . injectItem ( componentKey . toLowerCase ( ) ,
264- packageData [ componentKey ] [ componentIndex ] ,
269+ this . injectItem ( componentKey . toLowerCase ( ) ,
270+ packageData [ componentKey ] [ componentIndex ] ,
265271 possibleFiles [ fileIndex ] ) ;
266272
267273 this . ui . writeLine ( chalk . green ( 'Successfully injected.' ) ) ;
@@ -336,8 +342,8 @@ module.exports = Task.extend({
336342
337343 if ( removeNextLine ) {
338344 arr . splice ( index , 1 ) ;
339- if ( / ; / . test ( line ) ) {
340- removeNextLine = false ;
345+ if ( / ; / . test ( line ) ) {
346+ removeNextLine = false ;
341347 }
342348 }
343349 } ) ;
@@ -348,12 +354,12 @@ module.exports = Task.extend({
348354 function generateFlatImportLine ( arr , fromIndex ) {
349355 var lineArr = [ ] ;
350356 var lastIndex ;
351-
357+
352358 arr . forEach ( function ( line , index ) {
353359 if ( index >= fromIndex && ! lastIndex ) {
354360 lineArr . push ( line ) ;
355- if ( / ; / . test ( line ) ) {
356- lastIndex = true ;
361+ if ( / ; / . test ( line ) ) {
362+ lastIndex = true ;
357363 }
358364 }
359365 } ) ;
@@ -443,7 +449,7 @@ module.exports = Task.extend({
443449
444450 if ( / \) / . test ( line ) ) {
445451 replace = line . match ( / \( ( .* ?) \) / ) [ 0 ] ;
446- }
452+ }
447453 else {
448454 replace = contentsArr . splice ( index , contentsArr . length - 1 ) . join ( '\n' ) . replace ( / \n / g, '' ) ;
449455 replace = replace . match ( / \( ( .* ?) \) / ) [ 0 ] ;
@@ -495,7 +501,7 @@ module.exports = Task.extend({
495501 match = match . replace ( / [ \[ \] ] / g, '' ) ;
496502 match = match . split ( ',' ) ;
497503 match . push ( name ) ;
498- match = match . filter ( function ( n ) { return n !== '' ; } ) ;
504+ match = match . filter ( function ( n ) { return n !== '' ; } ) ;
499505 contentsArr [ index ] = contentsArr [ index ] . replace ( replace , '[' + match . join ( ',' ) + ']' ) ;
500506 }
501507 } . bind ( this ) ) ;
@@ -505,14 +511,14 @@ module.exports = Task.extend({
505511
506512 parseFile : function ( packageName ) {
507513 var packagePath = path . join ( process . cwd ( ) , 'node_modules' , packageName , packageName + '.ts' ) ;
508-
514+
509515 if ( ! existsSync ( packagePath ) ) {
510516 return false ;
511517 }
512518
513519 var contents = fs . readFileSync ( packagePath , 'utf8' ) ;
514520 var data = { } ;
515-
521+
516522 data . Component = [ ] ;
517523 data . Directive = [ ] ;
518524 data . Pipe = [ ] ;
0 commit comments