|
1 | 1 | 'use strict'; |
2 | | - |
3 | 2 | const dargs = require('dargs'); |
4 | 3 | const execa = require('execa'); |
5 | 4 | const gutil = require('gulp-util'); |
6 | 5 | const through = require('through2'); |
7 | 6 |
|
8 | | -module.exports = options => { |
9 | | - const defaults = {colors: true, suppress: false}; |
10 | | - |
11 | | - options = Object.assign(defaults, options); |
12 | | - |
13 | | - if (Object.prototype.toString.call(options.globals) === '[object Array]') { |
14 | | - // typically wouldn't modify passed options, but mocha requires a comma- |
15 | | - // separated list of names, http://mochajs.org/#globals-names, whereas dargs |
16 | | - // will treat arrays differently. |
17 | | - options.globals = options.globals.join(','); |
18 | | - } |
19 | | - |
20 | | - // exposing args for testing |
21 | | - const args = dargs(options, {excludes: ['suppress'], ignoreFalse: true}); |
22 | | - const files = []; |
23 | | - |
24 | | - function aggregate(file, encoding, done) { |
25 | | - if (file.isNull()) { |
26 | | - return done(null, file); |
27 | | - } |
28 | | - |
29 | | - if (file.isStream()) { |
30 | | - return done(new gutil.PluginError('gulp-mocha', 'Streaming not supported')); |
31 | | - } |
32 | | - |
33 | | - files.push(file.path); |
34 | | - |
35 | | - return done(); |
36 | | - } |
37 | | - |
38 | | - function flush(done) { |
39 | | - execa('mocha', files.concat(args)) |
40 | | - .then(result => { |
41 | | - if (!options.suppress) { |
42 | | - process.stdout.write(result.stdout); |
43 | | - } |
44 | | - |
45 | | - this.emit('result', result); |
46 | | - done(); |
47 | | - }) |
48 | | - .catch(err => { |
49 | | - this.emit('error', new gutil.PluginError('gulp-mocha', err)); |
50 | | - done(); |
51 | | - }); |
52 | | - } |
53 | | - |
54 | | - return through.obj(aggregate, flush); |
| 7 | +module.exports = opts => { |
| 8 | + opts = Object.assign({ |
| 9 | + colors: true, |
| 10 | + suppress: false |
| 11 | + }, opts); |
| 12 | + |
| 13 | + if (Array.isArray(opts.globals)) { |
| 14 | + // `globals` option should end up as a comma-separated list |
| 15 | + opts.globals = opts.globals.join(','); |
| 16 | + } |
| 17 | + |
| 18 | + const args = dargs(opts, { |
| 19 | + excludes: ['suppress'], |
| 20 | + ignoreFalse: true |
| 21 | + }); |
| 22 | + |
| 23 | + const files = []; |
| 24 | + |
| 25 | + function aggregate(file, encoding, done) { |
| 26 | + if (file.isNull()) { |
| 27 | + done(null, file); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + if (file.isStream()) { |
| 32 | + done(new gutil.PluginError('gulp-mocha', 'Streaming not supported')); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + files.push(file.path); |
| 37 | + |
| 38 | + done(); |
| 39 | + } |
| 40 | + |
| 41 | + function flush(done) { |
| 42 | + execa('mocha', files.concat(args)) |
| 43 | + .then(result => { |
| 44 | + if (!opts.suppress) { |
| 45 | + process.stdout.write(result.stdout); |
| 46 | + } |
| 47 | + |
| 48 | + // For testing |
| 49 | + this.emit('_result', result); |
| 50 | + |
| 51 | + done(); |
| 52 | + }) |
| 53 | + .catch(err => { |
| 54 | + this.emit('error', new gutil.PluginError('gulp-mocha', err)); |
| 55 | + done(); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + return through.obj(aggregate, flush); |
55 | 60 | }; |
0 commit comments