3
3
const { spawn } = require ( 'child_process' )
4
4
const { EOL } = require ( 'os' )
5
5
6
- const EE_PROPS = Object . getOwnPropertyNames ( require ( 'events' ) . EventEmitter . prototype ) . filter ( name => ! name . startsWith ( '_' ) )
6
+ const EE_PROPS = Object . getOwnPropertyNames (
7
+ require ( 'events' ) . EventEmitter . prototype
8
+ )
9
+ . filter ( name => ! name . startsWith ( '_' ) )
10
+ . concat ( [ 'kill' , 'ref' , 'unref' ] )
7
11
8
- const eos = ( stream , listener , buffer = [ ] ) => stream [ listener ] . on ( 'data' , data => buffer . push ( data ) ) && buffer
12
+ const eos = ( stream , listener , buffer = [ ] ) =>
13
+ stream [ listener ] . on ( 'data' , data => buffer . push ( data ) ) && buffer
9
14
10
15
const clean = str => str . trim ( ) . replace ( / \n $ / , '' )
11
16
12
- const parse = ( buffer , { json } = { } ) => ( encoding , start , end ) => {
13
- const data = clean ( Buffer . concat ( buffer ) . toString ( encoding , start , end ) )
14
- return json ? JSON . parse ( data ) : data
15
- }
17
+ const parse =
18
+ ( buffer , { json } = { } ) =>
19
+ ( encoding , start , end ) => {
20
+ const data = clean ( Buffer . concat ( buffer ) . toString ( encoding , start , end ) )
21
+ return json ? JSON . parse ( data ) : data
22
+ }
16
23
17
24
const extend = defaults => ( input , args , options ) => {
18
- if ( ! ( args instanceof Array ) ) { options = args ; args = [ ] }
25
+ if ( ! ( args instanceof Array ) ) {
26
+ options = args
27
+ args = [ ]
28
+ }
19
29
const [ cmd , ...cmdArgs ] = input . split ( ' ' ) . concat ( args ) . filter ( Boolean )
20
30
let childProcess
21
31
@@ -25,27 +35,36 @@ const extend = defaults => (input, args, options) => {
25
35
const stdout = eos ( childProcess , 'stdout' )
26
36
const stderr = eos ( childProcess , 'stderr' )
27
37
28
- childProcess
29
- . on ( 'error' , reject )
30
- . on ( 'exit' , code => {
31
- Object . defineProperty ( childProcess , 'stdout' , { get : parse ( stdout , opts ) } )
32
- Object . defineProperty ( childProcess , 'stderr' , { get : parse ( stderr ) } )
33
- if ( code === 0 ) return resolve ( childProcess )
34
- const command = `${ cmd } ${ cmdArgs . join ( ' ' ) } `
35
- let message = `The command spawned as:${ EOL } ${ EOL } `
36
- message += ` ${ command } ${ EOL } ${ EOL } `
37
- message += `exited with \`{ code: ${ code } }\` and the following trace:${ EOL } ${ EOL } `
38
- message += String ( stderr ) . split ( EOL ) . map ( line => ` ${ line } ` ) . join ( EOL )
39
- const error = new Error ( message )
40
- error . command = command
41
- error . name = 'ChildProcessError'
42
- Object . keys ( childProcess ) . forEach ( key => { error [ key ] = childProcess [ key ] } )
43
- reject ( error )
38
+ childProcess . on ( 'error' , reject ) . on ( 'exit' , code => {
39
+ Object . defineProperty ( childProcess , 'stdout' , {
40
+ get : parse ( stdout , opts )
41
+ } )
42
+ Object . defineProperty ( childProcess , 'stderr' , { get : parse ( stderr ) } )
43
+ if ( code === 0 ) return resolve ( childProcess )
44
+ const command = `${ cmd } ${ cmdArgs . join ( ' ' ) } `
45
+ let message = `The command spawned as:${ EOL } ${ EOL } `
46
+ message += ` ${ command } ${ EOL } ${ EOL } `
47
+ message += `exited with \`{ code: ${ code } }\` and the following trace:${ EOL } ${ EOL } `
48
+ message += String ( stderr )
49
+ . split ( EOL )
50
+ . map ( line => ` ${ line } ` )
51
+ . join ( EOL )
52
+ const error = new Error ( message )
53
+ error . command = command
54
+ error . name = 'ChildProcessError'
55
+ Object . keys ( childProcess ) . forEach ( key => {
56
+ error [ key ] = childProcess [ key ]
44
57
} )
58
+ reject ( error )
59
+ } )
45
60
} )
46
61
47
62
const subprocess = Object . assign ( promise , childProcess )
48
- if ( childProcess ) EE_PROPS . forEach ( name => ( subprocess [ name ] = childProcess [ name ] . bind ( childProcess ) ) )
63
+ if ( childProcess ) {
64
+ EE_PROPS . forEach (
65
+ name => ( subprocess [ name ] = childProcess [ name ] . bind ( childProcess ) )
66
+ )
67
+ }
49
68
return subprocess
50
69
}
51
70
0 commit comments