1
1
//
2
2
3
- import { existsSync , writeFileSync , statSync , readdirSync } from 'fs' ;
4
- import path from 'path' ;
3
+ import { existsSync , writeFileSync } from 'fs' ;
4
+ import { Instrumenter } from '../../instrumenter' ;
5
+ import { hook , Collector , Reporter , matcherFor , config as configuration } from 'istanbul' ;
6
+ import mkdirp from 'mkdirp' ;
5
7
import Module from 'module' ;
6
-
7
- //
8
8
import assign from 'object-assign' ;
9
+ import path from 'path' ;
9
10
import which from 'which' ;
10
- import mkdirp from 'mkdirp' ;
11
- import partial from 'lodash.partial' ;
12
- import nomnom from 'nomnomnomnom' ;
13
- import { hook , Collector , Reporter , matcherFor , config as configuration } from 'istanbul' ;
14
- import { Instrumenter } from './instrumenter' ;
15
11
16
12
//
17
13
14
+ export default coverCmd
18
15
19
16
//
20
17
21
- nomnom . command ( 'cover' )
22
- . help ( "transparently adds coverage information to a node command. Saves coverage.json and reports at the end of execution" )
23
-
24
- . option ( 'cmd' , {
25
- required : true ,
26
- position : 1 ,
27
- help : 'ES6 js files to cover (using babel)'
28
- } )
29
-
30
- . option ( 'config' , {
31
- metavar : '<path-to-config>' ,
32
- help : 'the configuration file to use, defaults to .istanbul.yml'
33
- } )
34
- . option ( 'default-excludes' , {
35
- flag : true ,
36
- help : 'apply default excludes [ **/node_modules/**, **/test/**, **/tests/** ]'
37
- } )
38
- . option ( 'excludes' , {
39
- abbr : 'x' ,
40
- default : [ ] ,
41
- help : 'one or more fileset patterns e.g. "**/vendor/**"' ,
42
- list : true ,
43
- metavar : '<exclude-pattern>'
44
- } )
45
- . option ( 'report' , {
46
- default : 'lcv' ,
47
- metavar : '<format>' ,
48
- list : true ,
49
- help : 'report format'
50
- } )
51
- . option ( 'root' , {
52
- metavar : '<path>' ,
53
- help : 'the root path to look for files to instrument'
54
- } )
55
- . option ( 'include' , {
56
- default : [ '**/*.js' ] ,
57
- metavar : '<include-pattern>' ,
58
- list : true ,
59
- abbr : 'i' ,
60
- help : 'one or more fileset patterns e.g. \'**/*.js\''
61
- } )
62
- . option ( 'verbose' , {
63
- flag : true ,
64
- abbr : 'v' ,
65
- help : 'verbose mode'
66
- } )
67
- . option ( 'include-all-sources' , {
68
- flag : true ,
69
- help : 'instrument all unused sources after running tests'
70
- } )
71
-
72
- . callback ( opts => {
73
-
74
- let args = opts . _ ,
75
- files = [ ] ,
76
- cmdArgs = [ ] ;
77
-
78
- args . forEach ( arg => {
79
-
80
- let file = lookupFiles ( arg ) ;
81
- if ( file ) files = files . concat ( file ) ;
82
- } ) ;
83
-
84
- opts . include = opts . include . concat ( files ) ;
85
-
86
- coverCmd ( opts ) ;
87
- } ) ;
88
- ;
89
-
90
- nomnom . nom ( ) ;
91
-
92
- function lookupFiles ( path ) {
93
-
94
- if ( existsSync ( path ) ) {
95
- let stat = statSync ( path ) ;
96
- if ( stat . isFile ( ) ) return path ;
97
- }
98
- }
99
-
100
- function callback ( err ) {
101
- if ( err ) {
102
- console . error ( err ) ;
103
- process . exit ( 1 ) ;
104
- }
105
- process . exit ( 0 ) ;
106
- }
107
-
108
- function coverCmd ( opts ) {
109
-
18
+ function coverCmd ( opts ) {
110
19
let config = overrideConfigWith ( opts ) ;
111
20
let istanbulCoveragePath = path . resolve ( config . reporting . dir ( ) ) ;
112
21
let reporter = new Reporter ( config , istanbulCoveragePath ) ;
@@ -118,7 +27,7 @@ function coverCmd(opts) {
118
27
try {
119
28
cmd = which . sync ( cmd ) ;
120
29
} catch ( ex ) {
121
- return callback ( `Unable to resolve file [${ cmd } ]` ) ;
30
+ return processEnding ( `Unable to resolve file [${ cmd } ]` ) ;
122
31
}
123
32
} else {
124
33
cmd = path . resolve ( cmd ) ;
@@ -132,7 +41,7 @@ function coverCmd(opts) {
132
41
133
42
////
134
43
135
- function overrideConfigWith ( opts ) {
44
+ function overrideConfigWith ( opts ) {
136
45
let overrides = {
137
46
verbose : opts . verbose ,
138
47
instrumentation : {
@@ -159,7 +68,7 @@ function coverCmd(opts) {
159
68
return configuration . loadFile ( opts . config , overrides ) ;
160
69
}
161
70
162
- function enableHooks ( ) {
71
+ function enableHooks ( ) {
163
72
opts . reportingDir = path . resolve ( config . reporting . dir ( ) ) ;
164
73
mkdirp . sync ( opts . reportingDir ) ;
165
74
reporter . addAll ( config . reporting . reports ( ) ) ;
@@ -187,8 +96,8 @@ function coverCmd(opts) {
187
96
. map ( ( ext ) => '**/*' + ext ) ,
188
97
excludes : excludes
189
98
} , ( err , matchFn ) => {
190
- if ( err ) {
191
- return callback ( err ) ;
99
+ if ( err ) {
100
+ return processEnding ( err ) ;
192
101
}
193
102
194
103
prepareCoverage ( matchFn ) ;
@@ -197,12 +106,12 @@ function coverCmd(opts) {
197
106
}
198
107
199
108
200
- function prepareCoverage ( matchFn ) {
109
+ function prepareCoverage ( matchFn ) {
201
110
let coverageVar = `$$cov_${ Date . now ( ) } $$` ;
202
- let instrumenter = new Instrumenter ( { coverageVariable : coverageVar } ) ;
111
+ let instrumenter = new Instrumenter ( { coverageVariable : coverageVar } ) ;
203
112
let transformer = instrumenter . instrumentSync . bind ( instrumenter ) ;
204
113
205
- hook . hookRequire ( matchFn , transformer , assign ( { verbose : opts . verbose } , config . instrumentation . config ) ) ;
114
+ hook . hookRequire ( matchFn , transformer , assign ( { verbose : opts . verbose } , config . instrumentation . config ) ) ;
206
115
207
116
global [ coverageVar ] = { } ;
208
117
@@ -236,7 +145,7 @@ function coverCmd(opts) {
236
145
console . error ( `Writing coverage reports at [${ opts . reportingDir } ]` ) ;
237
146
console . error ( Array ( 80 + 1 ) . join ( '=' ) ) ;
238
147
}
239
- reporter . write ( collector , true , callback ) ;
148
+ reporter . write ( collector , true , processEnding ) ;
240
149
} ) ;
241
150
242
151
if ( config . instrumentation . includeAllSources ( ) ) {
@@ -255,12 +164,22 @@ function coverCmd(opts) {
255
164
256
165
}
257
166
258
- function runCommandFn ( ) {
167
+ function runCommandFn ( ) {
259
168
process . argv = [ "node" , cmd ] . concat ( cmdArgs ) ;
260
169
if ( opts . verbose ) {
261
170
console . log ( 'Running: ' + process . argv . join ( ' ' ) ) ;
262
171
}
263
- process . env . running_under_istanbul = 1 ;
172
+ process . env . running_under_istanbul = 1 ;
264
173
Module . runMain ( cmd , null , true ) ;
265
174
}
266
175
}
176
+
177
+ //
178
+
179
+ function processEnding ( err ) {
180
+ if ( err ) {
181
+ console . error ( err ) ;
182
+ process . exit ( 1 ) ;
183
+ }
184
+ process . exit ( 0 ) ;
185
+ }
0 commit comments