3 var childProcessPromise
= require('./child-process-promise');
4 var path
= require('path');
5 var es
= require('event-stream');
6 var gutil
= require('gulp-util');
8 function resolveBinary(moduleName
) {
9 var moduleLocation
= require
.resolve(moduleName
);
10 var moduleDirectory
= path
.dirname(moduleLocation
);
11 return path
.resolve(moduleDirectory
, '../.bin/' + moduleName
);
14 function serializeCompilers(compilers
) {
15 if (!compilers
) return;
16 return Object
.keys(compilers
)
17 .map(function(extension
) {
18 var m
= compilers
[extension
];
19 return extension
+ ':' + m
;
24 function testRunner(options
, files
) {
27 var mochaBinary
= resolveBinary('mocha');
29 function pushBooleanArg(argName
, bool
) {
35 function pushArg(argName
, value
) {
42 function addMultiArgs(argName
, extensions
) {
43 if (!extensions
) return;
44 extensions
.forEach(function(ext
) {
50 function addOptional(options
, argName
, keyName
) {
51 if (options
.hasOwnProperty(keyName
)) {
53 args
.push(options
[keyName
]);
58 var nyc
= options
.nyc
;
59 executable
= resolveBinary('nyc');
62 pushBooleanArg('--cache', nyc
.cache
);
63 pushArg('--report-dir', nyc
.reportDir
);
64 pushArg('--reporter', nyc
.reporter
);
65 addMultiArgs('--extension', nyc
.extensions
);
66 addMultiArgs('--require', nyc
.requires
);
67 addOptional(nyc
, '--instrument', 'instrument');
68 addOptional(nyc
, '--source-map', 'sourceMap');
69 args
.push(mochaBinary
);
71 executable
= mochaBinary
;
75 pushArg('-R', options
.reporter
);
76 pushBooleanArg('--recursive', options
.recursive
);
77 pushArg('--grep', options
.grep
);
78 pushBooleanArg('--invert', options
.invert
);
79 pushBooleanArg('--bail', options
.invert
);
80 pushArg('--timeout', options
.timeout
);
81 pushArg('--compilers', serializeCompilers(options
.compilers
));
83 args
= args
.concat(files
);
85 gutil
.log('Spawning ', executable
, args
.join(' '));
86 return childProcessPromise
.spawn(executable
, args
, options
.env
);
89 function pipe(options
) {
93 files
.push(data
.path
);
95 this.emit('data', data
);
98 testRunner(options
, files
)
103 .catch(function(err
) {
104 this.emit('error', err
);
110 module
.exports
= pipe
;