5
5
6
6
const _ = require ( 'lodash' ) ;
7
7
const BbPromise = require ( 'bluebird' ) ;
8
- const childProcess = require ( 'child_process ' ) ;
8
+ const Utils = require ( '../utils ' ) ;
9
9
10
10
class NPM {
11
11
static get lockfileName ( ) { // eslint-disable-line lodash/prefer-constant
@@ -16,39 +16,44 @@ class NPM {
16
16
return true ;
17
17
}
18
18
19
- static getProdDependencies ( cwd , depth , maxExecBufferSize ) {
19
+ static getProdDependencies ( cwd , depth ) {
20
20
// Get first level dependency graph
21
- const command = `npm ls -prod -json -depth=${ depth || 1 } ` ; // Only prod dependencies
21
+ const command = / ^ w i n / . test ( process . platform ) ? 'npm.cmd' : 'npm' ;
22
+ const args = [
23
+ 'ls' ,
24
+ '-prod' , // Only prod dependencies
25
+ '-json' ,
26
+ `-depth=${ depth || 1 } `
27
+ ] ;
22
28
23
29
const ignoredNpmErrors = [
24
30
{ npmError : 'extraneous' , log : false } ,
25
31
{ npmError : 'missing' , log : false } ,
26
32
{ npmError : 'peer dep missing' , log : true } ,
27
33
] ;
28
34
29
- return BbPromise . fromCallback ( cb => {
30
- childProcess . exec ( command , {
31
- cwd : cwd ,
32
- maxBuffer : maxExecBufferSize ,
33
- encoding : 'utf8'
34
- } , ( err , stdout , stderr ) => {
35
- if ( err ) {
36
- // Only exit with an error if we have critical npm errors for 2nd level inside
37
- const errors = _ . split ( stderr , '\n' ) ;
38
- const failed = _ . reduce ( errors , ( failed , error ) => {
39
- if ( failed ) {
40
- return true ;
41
- }
42
- return ! _ . isEmpty ( error ) && ! _ . some ( ignoredNpmErrors , ignoredError => _ . startsWith ( error , `npm ERR! ${ ignoredError . npmError } ` ) ) ;
43
- } , false ) ;
44
-
35
+ return Utils . spawnProcess ( command , args , {
36
+ cwd : cwd
37
+ } )
38
+ . catch ( err => {
39
+ if ( err instanceof Utils . SpawnError ) {
40
+ // Only exit with an error if we have critical npm errors for 2nd level inside
41
+ const errors = _ . split ( err . stderr , '\n' ) ;
42
+ const failed = _ . reduce ( errors , ( failed , error ) => {
45
43
if ( failed ) {
46
- return cb ( err ) ;
44
+ return true ;
47
45
}
46
+ return ! _ . isEmpty ( error ) && ! _ . some ( ignoredNpmErrors , ignoredError => _ . startsWith ( error , `npm ERR! ${ ignoredError . npmError } ` ) ) ;
47
+ } , false ) ;
48
+
49
+ if ( ! failed && ! _ . isEmpty ( err . stdout ) ) {
50
+ return BbPromise . resolve ( { stdout : err . stdout } ) ;
48
51
}
49
- return cb ( null , stdout ) ;
50
- } ) ;
52
+ }
53
+
54
+ return BbPromise . reject ( err ) ;
51
55
} )
56
+ . then ( processOutput => processOutput . stdout )
52
57
. then ( depJson => BbPromise . try ( ( ) => JSON . parse ( depJson ) ) ) ;
53
58
}
54
59
@@ -73,36 +78,32 @@ class NPM {
73
78
}
74
79
}
75
80
76
- static install ( cwd , maxExecBufferSize ) {
77
- return BbPromise . fromCallback ( cb => {
78
- childProcess . exec ( 'npm install' , {
79
- cwd : cwd ,
80
- maxBuffer : maxExecBufferSize ,
81
- encoding : 'utf8'
82
- } , cb ) ;
83
- } )
81
+ static install ( cwd ) {
82
+ const command = / ^ w i n / . test ( process . platform ) ? 'npm.cmd' : 'npm' ;
83
+ const args = [ 'install' ] ;
84
+
85
+ return Utils . spawnProcess ( command , args , { cwd } )
84
86
. return ( ) ;
85
87
}
86
88
87
- static prune ( cwd , maxExecBufferSize ) {
88
- return BbPromise . fromCallback ( cb => {
89
- childProcess . exec ( 'npm prune' , {
90
- cwd : cwd ,
91
- maxBuffer : maxExecBufferSize ,
92
- encoding : 'utf8'
93
- } , cb ) ;
94
- } )
89
+ static prune ( cwd ) {
90
+ const command = / ^ w i n / . test ( process . platform ) ? 'npm.cmd' : 'npm' ;
91
+ const args = [ 'prune' ] ;
92
+
93
+ return Utils . spawnProcess ( command , args , { cwd } )
95
94
. return ( ) ;
96
95
}
97
96
98
- static runScripts ( cwd , maxExecBufferSize , scriptNames ) {
99
- return BbPromise . mapSeries ( scriptNames , scriptName => BbPromise . fromCallback ( cb => {
100
- childProcess . exec ( `npm run ${ scriptName } ` , {
101
- cwd : cwd ,
102
- maxBuffer : maxExecBufferSize ,
103
- encoding : 'utf8'
104
- } , cb ) ;
105
- } ) )
97
+ static runScripts ( cwd , scriptNames ) {
98
+ const command = / ^ w i n / . test ( process . platform ) ? 'npm.cmd' : 'npm' ;
99
+ return BbPromise . mapSeries ( scriptNames , scriptName => {
100
+ const args = [
101
+ 'run' ,
102
+ scriptName
103
+ ] ;
104
+
105
+ return Utils . spawnProcess ( command , args , { cwd } ) ;
106
+ } )
106
107
. return ( ) ;
107
108
}
108
109
}
0 commit comments