1
+ 'use strict'
2
+
1
3
// Module Requirements
2
- var _ = require ( 'lodash' ) ,
3
- fs = require ( 'fs' ) ,
4
- through = require ( 'through' ) ,
5
- proc = require ( 'child_process' ) ,
6
- join = require ( 'path' ) . join ,
7
- PluginError = require ( 'gulp-util' ) . PluginError ;
4
+ const _ = require ( 'lodash' )
5
+ const fs = require ( 'fs' )
6
+ const through = require ( 'through' )
7
+ const proc = require ( 'child_process' )
8
+ const join = require ( 'path' ) . join
9
+ const PluginError = require ( 'gulp-util' ) . PluginError
8
10
9
11
/**
10
12
* Returns a stream to use with Gulp which executes the passed files as Mocha
@@ -17,88 +19,111 @@ var _ = require('lodash'),
17
19
*/
18
20
module . exports = function ( ops , coverage ) {
19
21
// Default ops
20
- ops = ops || { } ;
22
+ ops = ops || { }
21
23
// Setup
22
- var ist = ops . istanbul ;
23
- var output = ops . output ;
24
+ let ist = ops . istanbul
25
+ let output = ops . output
24
26
// Using istanbul? Use _mocha, otherwise use mocha in order to support full node options (e.g., --debug-brk)
25
- var bin = ops . bin || join ( require . resolve ( 'mocha' ) , '..' , 'bin' , ist ? '_mocha' : 'mocha' ) ;
26
- var env = _ . extend ( _ . clone ( process . env ) , ops . env || { } ) ;
27
- var cwd = ops . cwd ;
28
- var execPath = ops . execPath || process . execPath ;
29
- ops = _ . omit ( ops , [ 'bin' , 'env' , 'istanbul' , 'cwd' , 'execPath' ] ) ;
27
+ let bin =
28
+ ops . bin ||
29
+ join ( require . resolve ( 'mocha' ) , '..' , 'bin' , ist ? '_mocha' : 'mocha' )
30
+ let env = _ . extend ( _ . clone ( process . env ) , ops . env || { } )
31
+ let cwd = ops . cwd
32
+ let execPath = ops . execPath || process . execPath
33
+ ops = _ . omit ( ops , [ 'bin' , 'env' , 'istanbul' , 'cwd' , 'execPath' ] )
30
34
31
35
// Create stream
32
- var stream = through ( function ( file ) {
33
- this . _files . push ( file . path ) ;
34
- } , function ( ) {
35
- // make sure there are files to test
36
- if ( this . _files . length === 0 ) {
37
- this . emit ( 'end' ) ;
38
- return ;
39
- } ;
36
+ let stream = through (
37
+ function ( file ) {
38
+ this . _files . push ( file . path )
39
+ } ,
40
+ function ( ) {
41
+ // make sure there are files to test
42
+ if ( this . _files . length === 0 ) {
43
+ this . emit ( 'end' )
44
+ return
45
+ }
40
46
41
- // Save refernce to this (bindless context cheat)
42
- var that = this ;
47
+ // Save refernce to this (bindless context cheat)
48
+ let that = this
43
49
44
- // Parse arguments
45
- var args = parseArgs ( ops ) ;
46
- // Using istanbul?
47
- if ( ist ) {
48
- args . unshift ( '--' , bin ) ;
49
- // The non-standard location of istanbul's bin makes me a wee bit nervous. Find it by inspecting package.json.
50
- bin = ist . bin || join ( require . resolve ( 'istanbul' ) , '..' , require ( 'istanbul/package.json' ) . bin . istanbul ) ;
51
- // If the value for istanbul is literally true, just keep the arguments array. Otherwise, parse istanbul options.
52
- args = ist === true ? args : parseArgs ( _ . omit ( ist , [ 'bin' ] ) ) . concat ( args ) ;
53
- args . unshift ( 'cover' ) ;
54
- }
55
- // Execute Mocha, stdin and stdout are inherited
56
- this . _child = proc . fork ( bin , args . concat ( this . _files ) , { cwd : cwd , env : env , execPath : execPath , silent : ! ! output } ) ;
57
- // If there's an error running the process. See http://nodejs.org/api/child_process.html#child_process_event_error
58
- this . _child . on ( 'error' , function ( e ) {
59
- that . emit ( 'error' , new PluginError ( 'gulp-spawn-mocha' , e ) ) ;
60
- that . emit ( 'end' ) ;
61
- } ) ;
62
- // When done...
63
- this . _child . on ( 'close' , function ( code ) {
64
- // If code is not zero (falsy)
65
- if ( code ) {
66
- that . emit ( 'error' , new PluginError ( 'gulp-spawn-mocha' , 'Mocha exited with code ' + code ) ) ;
50
+ // Parse arguments
51
+ let args = parseArgs ( ops )
52
+ // Using istanbul?
53
+ if ( ist ) {
54
+ args . unshift ( '--' , bin )
55
+ // The non-standard location of istanbul's bin makes me a wee bit nervous. Find it by inspecting package.json.
56
+ bin =
57
+ ist . bin ||
58
+ join (
59
+ require . resolve ( 'istanbul' ) ,
60
+ '..' ,
61
+ require ( 'istanbul/package.json' ) . bin . istanbul
62
+ )
63
+ // If the value for istanbul is literally true, just keep the arguments array. Otherwise, parse istanbul options.
64
+ args =
65
+ ist === true ? args : parseArgs ( _ . omit ( ist , [ 'bin' ] ) ) . concat ( args )
66
+ args . unshift ( 'cover' )
67
+ }
68
+ // Execute Mocha, stdin and stdout are inherited
69
+ this . _child = proc . fork ( bin , args . concat ( this . _files ) , {
70
+ cwd,
71
+ env,
72
+ execPath,
73
+ silent : ! ! output
74
+ } )
75
+ // If there's an error running the process. See http://nodejs.org/api/child_process.html#child_process_event_error
76
+ this . _child . on ( 'error' , ( e ) => {
77
+ that . emit ( 'error' , new PluginError ( 'gulp-spawn-mocha' , e ) )
78
+ that . emit ( 'end' )
79
+ } )
80
+ // When done...
81
+ this . _child . on ( 'close' , ( code ) => {
82
+ // If code is not zero (falsy)
83
+ if ( code ) {
84
+ that . emit (
85
+ 'error' ,
86
+ new PluginError (
87
+ 'gulp-spawn-mocha' ,
88
+ `Mocha exited with code ${ code } `
89
+ )
90
+ )
91
+ }
92
+ that . emit ( 'end' )
93
+ } )
94
+ // Output to a file
95
+ if ( output ) {
96
+ let s = _ . isString ( output ) ? fs . createWriteStream ( output ) : output
97
+ that . _child . stdout . pipe ( s )
98
+ that . _child . stderr . pipe ( s )
67
99
}
68
- that . emit ( 'end' ) ;
69
- } ) ;
70
- // Output to a file
71
- if ( output ) {
72
- var s = _ . isString ( output ) ? fs . createWriteStream ( output ) : output ;
73
- that . _child . stdout . pipe ( s ) ;
74
- that . _child . stderr . pipe ( s ) ;
75
100
}
76
- } ) ;
101
+ )
77
102
78
103
// Attach files array to stream
79
- stream . _files = [ ] ;
104
+ stream . _files = [ ]
80
105
// Return stream
81
- return stream ;
82
- } ;
106
+ return stream
107
+ }
83
108
84
109
/**
85
110
* Parses the arugments from a configuration object for passing to a mocha
86
111
* executable.
87
112
* @param {Object } obj The object to parse from.
88
113
* @return {Array } An array of parsed arguments.
89
114
*/
90
- function parseArgs ( obj ) {
91
- var args = [ ] ;
92
- _ . each ( obj , function ( val , key ) {
115
+ function parseArgs ( obj ) {
116
+ let args = [ ]
117
+ _ . each ( obj , ( val , key ) => {
93
118
if ( _ . isArray ( val ) ) {
94
- _ . each ( val , function ( val ) {
95
- addArg ( args , key , val ) ;
96
- } ) ;
119
+ _ . each ( val , ( val ) => {
120
+ addArg ( args , key , val )
121
+ } )
97
122
} else {
98
- addArg ( args , key , val ) ;
123
+ addArg ( args , key , val )
99
124
}
100
- } ) ;
101
- return args ;
125
+ } )
126
+ return args
102
127
}
103
128
104
129
/**
@@ -108,19 +133,19 @@ function parseArgs(obj) {
108
133
* @param {String } val Value of the argument. Returns without doing anything
109
134
* if falsy and not zero.
110
135
*/
111
- function addArg ( args , name , val ) {
136
+ function addArg ( args , name , val ) {
112
137
if ( ! val && val !== 0 ) {
113
- return ;
138
+ return
114
139
}
115
- var arg = name . length > 1 ? '--' + _ . kebabCase ( name ) : '-' + name ;
140
+ let arg = name . length > 1 ? `-- ${ _ . kebabCase ( name ) } ` : `- ${ name } `
116
141
// --max-old-space-size argument requires an `=`
117
142
if ( arg === '--max-old-space-size' ) {
118
- args . push ( arg + '=' + val ) ;
119
- return ;
143
+ args . push ( ` ${ arg } = ${ val } ` )
144
+ return
120
145
} else {
121
- args . push ( arg ) ;
146
+ args . push ( arg )
122
147
}
123
148
if ( _ . isString ( val ) || _ . isNumber ( val ) ) {
124
- args . push ( val ) ;
149
+ args . push ( val )
125
150
}
126
151
}
0 commit comments