11// vim: expandtab:ts=2:sw=2
22
3- const
3+ var
44 fs = require ( 'fs' ) ,
55 path = require ( 'path' ) ,
6- existsSync = fs . existsSync || path . existsSync ,
6+ exists = fs . exists || path . exists ,
77 spawn = require ( 'child_process' ) . spawn ;
88
9+ const ISTANBUL_PATH = path . join ( __dirname , '..' , 'node_modules' , 'istanbul' , 'lib' , 'cli.js' ) ;
910
10- module . exports . genericChildProcess = function spawnGenericChildProcess ( configFile , cb ) {
11- const
12- configFilePath = path . join ( __dirname , 'outband' , configFile ) ,
13- command_args = [ path . join ( __dirname , 'spawn-generic.js' ) , configFilePath ] ;
11+ module . exports . genericChildProcess = _spawnProcess ( 'spawn-generic.js' ) ;
12+ module . exports . childProcess = _spawnProcess ( 'spawn-custom.js' ) ;
1413
15- // make sure that the config file exists
16- if ( ! existsSync ( configFilePath ) )
17- return cb ( new Error ( 'ENOENT: configFile ' + configFilePath + ' does not exist' ) ) ;
14+ function _spawnProcess ( spawnFile ) {
15+ return function ( testCase , configFile , cb ) {
16+ var
17+ configFilePath = path . join ( __dirname , 'outband' , configFile ) ,
18+ commandArgs = [ path . join ( __dirname , spawnFile ) , configFilePath ] ;
1819
19- _do_spawn ( command_args , cb ) ;
20- } ;
20+ exists ( configFilePath , function ( configExists ) {
21+ if ( configExists ) return _doSpawn ( commandArgs , cb ) ;
2122
22- module . exports . childProcess = function spawnChildProcess ( configFile , cb , detach ) {
23- var
24- configFilePath = path . join ( __dirname , 'outband' , configFile ) ,
25- command_args = [ path . join ( __dirname , 'spawn-custom.js' ) , configFilePath ] ;
26-
27- // make sure that the config file exists
28- if ( ! existsSync ( configFilePath ) )
29- return cb ( new Error ( 'ENOENT: configFile ' + configFilePath + ' does not exist' ) ) ;
30-
31- if ( arguments . length > 2 ) {
32- for ( var i = 2 ; i < arguments . length ; i ++ ) {
33- command_args . push ( arguments [ i ] ) ;
34- }
35- }
36-
37- _do_spawn ( command_args , cb , detach ) ;
23+ cb ( new Error ( 'ENOENT: configFile ' + configFilePath + ' does not exist' ) ) ;
24+ } ) ;
25+ } ;
3826}
3927
40- function _do_spawn ( command_args , cb , detach ) {
41- const
28+ function _doSpawn ( commandArgs , cb ) {
29+ var
4230 node_path = process . argv [ 0 ] ,
4331 stdoutBufs = [ ] ,
44- stderrBufs = [ ] ;
45-
46- var
32+ stderrBufs = [ ] ,
4733 child ,
4834 done = false ,
4935 stderrDone = false ,
5036 stdoutDone = false ;
5137
38+ if ( process . env . running_under_istanbul ) {
39+ commandArgs = [
40+ ISTANBUL_PATH , 'cover' , '--report' , 'none' , '--print' , 'none' ,
41+ '--dir' , path . join ( 'coverage' , 'json' ) , '--include-pid' ,
42+ commandArgs [ 0 ] , '--' , commandArgs [ 1 ]
43+ ] ;
44+ }
45+
5246 // spawn doesn’t have the quoting problems that exec does,
5347 // especially when going for Windows portability.
54- child = spawn ( node_path , command_args , detach ? { detached : true } : undefined ) ;
48+ child = spawn ( node_path , commandArgs ) ;
5549 child . stdin . end ( ) ;
56- // TODO:we no longer support node <0.10.0
50+
51+ // TODO we no longer support node 0.6
5752 // Cannot use 'close' event because not on node-0.6.
5853 function _close ( ) {
59- const
54+ var
6055 stderr = _bufferConcat ( stderrBufs ) . toString ( ) ,
6156 stdout = _bufferConcat ( stdoutBufs ) . toString ( ) ;
57+
6258 if ( stderrDone && stdoutDone && ! done ) {
6359 done = true ;
6460 cb ( null , stderr , stdout ) ;
6561 }
6662 }
63+
6764 child . on ( 'error' , function _spawnError ( err ) {
6865 if ( ! done ) {
6966 done = true ;
7067 cb ( err ) ;
7168 }
7269 } ) ;
70+
7371 child . stdout . on ( 'data' , function _stdoutData ( data ) {
7472 stdoutBufs . push ( data ) ;
7573 } ) . on ( 'close' , function _stdoutEnd ( ) {
7674 stdoutDone = true ;
7775 _close ( ) ;
7876 } ) ;
77+
7978 child . stderr . on ( 'data' , function _stderrData ( data ) {
8079 stderrBufs . push ( data ) ;
8180 } ) . on ( 'close' , function _stderrEnd ( ) {
@@ -87,13 +86,13 @@ function _do_spawn(command_args, cb, detach) {
8786function _bufferConcat ( buffers ) {
8887 if ( Buffer . concat ) {
8988 return Buffer . concat . apply ( this , arguments ) ;
90- } else {
91- return new Buffer ( buffers . reduce ( function ( acc , buf ) {
92- for ( var i = 0 ; i < buf . length ; i ++ ) {
93- acc . push ( buf [ i ] ) ;
94- }
95- return acc ;
96- } , [ ] ) ) ;
9789 }
90+
91+ return new Buffer ( buffers . reduce ( function ( acc , buf ) {
92+ for ( var i = 0 ; i < buf . length ; i ++ ) {
93+ acc . push ( buf [ i ] ) ;
94+ }
95+ return acc ;
96+ } , [ ] ) ) ;
9897}
9998
0 commit comments