33
44const configUtil = require ( '../lib/config-util' )
55const { cliWrapper, suppressEPIPE } = require ( '../lib/commands/helpers' )
6- const foreground = require ( 'foreground-child' )
6+ const { foregroundChild } = require ( 'foreground-child' )
77const resolveFrom = require ( 'resolve-from' )
88const NYC = require ( '../index.js' )
9+ const path = require ( 'path' )
10+ const fs = require ( 'fs' )
911
1012// parse configuration and command-line arguments;
1113// we keep these values in a few different forms,
@@ -86,10 +88,22 @@ async function main () {
8688 // set process.exitCode. Keep track so that both children are run, but
8789 // a non-zero exit codes in either one leads to an overall non-zero exit code.
8890 process . exitCode = 0
89- foreground ( childArgs , async ( ) => {
90- const mainChildExitCode = process . exitCode
91+ foregroundChild ( childArgs , async ( code , signal , processInfo ) => {
92+ let exitCode = process . exitCode || code
9193
9294 try {
95+ // clean up foreground-child watchdog process info
96+ const parentDir = path . resolve ( nyc . tempDirectory ( ) )
97+ const dir = path . resolve ( nyc . tempDirectory ( ) , 'processinfo' )
98+ const files = await nyc . coverageFiles ( dir )
99+ for ( let i = 0 ; i < files . length ; i ++ ) {
100+ const data = await nyc . coverageFileLoad ( files [ i ] , dir )
101+ if ( data . pid === processInfo . watchdogPid ) {
102+ fs . unlinkSync ( path . resolve ( parentDir , files [ i ] ) )
103+ fs . unlinkSync ( path . resolve ( dir , files [ i ] ) )
104+ }
105+ }
106+
93107 await nyc . writeProcessIndex ( )
94108
95109 nyc . maybePurgeSourceMapCache ( )
@@ -100,18 +114,20 @@ async function main () {
100114 branches : argv . branches ,
101115 statements : argv . statements
102116 } , argv [ 'per-file' ] ) . catch ( suppressEPIPE )
103- process . exitCode = process . exitCode || mainChildExitCode
117+ exitCode = process . exitCode || exitCode
104118 }
105119
106120 if ( ! argv . silent ) {
107121 await nyc . report ( ) . catch ( suppressEPIPE )
108122 }
109123 } catch ( error ) {
110124 /* istanbul ignore next */
111- process . exitCode = process . exitCode || mainChildExitCode || 1
125+ exitCode = process . exitCode || exitCode || 1
112126 /* istanbul ignore next */
113127 console . error ( error . message )
114128 }
129+
130+ return exitCode
115131 } )
116132}
117133
0 commit comments