1
- 'use strict'
2
-
3
- const runner = require ( 'child_process' )
4
- const debug = require ( 'debug' ) ( 'subchild' )
5
-
6
- const children = [ ]
7
-
8
- function removeChild ( child ) {
9
- const i = children . indexOf ( child )
10
- if ( i !== - 1 ) {
11
- children . slice ( i , 1 )
12
- }
13
- }
14
-
15
- function killAll ( ) {
16
- debug ( 'killing all children' )
17
- let child
18
- while ( ( child = children . shift ( ) ) !== undefined ) {
19
- debug ( child . pid , 'killing' )
20
- child . kill ( ) ;
21
- }
22
- }
23
-
24
- process . once ( 'error' , killAll )
25
- process . once ( 'exit' , killAll )
26
- process . once ( 'SIGTERM' , killAll )
27
- process . once ( 'SIGINT' , killAll )
28
-
29
- function run ( cmd , args , opts ) {
30
- const child = runner . execFile ( cmd , args , opts )
31
- debug ( child . pid , 'new' )
32
-
33
- children . push ( child ) ;
34
- child . once ( 'error' , ( ) => {
35
- debug ( child . pid , 'error' )
36
- removeChild ( child )
37
- } )
38
- child . once ( 'exit' , ( ) => {
39
- debug ( child . pid , 'exit' )
40
- removeChild ( child ) ;
41
- } )
42
- return child
43
- }
44
-
45
- module . exports = run
1
+ 'use strict'
2
+
3
+ const runner = require ( 'child_process' )
4
+ const debug = require ( 'debug' ) ( 'subchild' )
5
+
6
+ const children = [ ]
7
+
8
+ function removeChild ( child ) {
9
+ const i = children . indexOf ( child )
10
+ if ( i !== - 1 ) {
11
+ children . slice ( i , 1 )
12
+ }
13
+ }
14
+
15
+ function killAll ( ) {
16
+ debug ( 'killing all children' )
17
+ let child
18
+ while ( ( child = children . shift ( ) ) !== undefined ) {
19
+ debug ( child . pid , 'killing' )
20
+ child . kill ( )
21
+ }
22
+ }
23
+
24
+ process . once ( 'error' , killAll )
25
+ process . once ( 'exit' , killAll )
26
+ process . once ( 'SIGTERM' , killAll )
27
+ process . once ( 'SIGINT' , killAll )
28
+
29
+ function run ( cmd , args , opts ) {
30
+ const child = runner . execFile ( cmd , args , opts )
31
+ debug ( child . pid , 'new' )
32
+
33
+ children . push ( child )
34
+ child . once ( 'error' , ( ) => {
35
+ debug ( child . pid , 'error' )
36
+ removeChild ( child )
37
+ } )
38
+ child . once ( 'exit' , ( ) => {
39
+ debug ( child . pid , 'exit' )
40
+ removeChild ( child )
41
+ } )
42
+ return child
43
+ }
44
+
45
+ module . exports = run
0 commit comments