forked from istanbuljs/nyc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnyc.js
executable file
·139 lines (129 loc) · 4.12 KB
/
nyc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
var foreground = require('foreground-child')
var NYC
try {
NYC = require('../index.covered.js')
} catch (e) {
NYC = require('../index.js')
}
var path = require('path')
var sw = require('spawn-wrap')
if (process.env.NYC_CWD) {
;(new NYC({
require: process.env.NYC_REQUIRE ? process.env.NYC_REQUIRE.split(',') : []
})).wrap()
sw.runMain()
} else {
var yargs = require('yargs')
.usage('$0 [command] [options]\n\nrun your tests with the nyc bin to instrument them with coverage')
.command('report', 'run coverage report for .nyc_output', function (yargs) {
yargs
.usage('$0 report [options]')
.option('r', {
alias: 'reporter',
describe: 'coverage reporter(s) to use',
default: 'text'
})
.help('h')
.alias('h', 'help')
.example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
})
.command('check-coverage', 'check whether coverage is within thresholds provided', function (yargs) {
yargs
.usage('$0 check-coverage [options]')
.option('b', {
alias: 'branches',
default: 0,
description: 'what % of branches must be covered?'
})
.option('f', {
alias: 'functions',
default: 0,
description: 'what % of functions must be covered?'
})
.option('l', {
alias: 'lines',
default: 90,
description: 'what % of lines must be covered?'
})
.option('s', {
alias: 'statements',
default: 0,
description: 'what % of statements must be covered?'
})
.help('h')
.alias('h', 'help')
.example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")
})
.option('r', {
alias: 'reporter',
describe: 'coverage reporter(s) to use',
default: 'text'
})
.option('s', {
alias: 'silent',
default: false,
type: 'boolean',
describe: "don't output a report after tests finish running"
})
.option('a', {
alias: 'all',
default: false,
type: 'boolean',
describe: 'whether or not to instrument all files of the project (not just the ones touched by your test suite)'
})
.option('i', {
alias: 'require',
default: [],
describe: 'a list of additional modules that nyc should attempt to require in its subprocess, e.g., babel-register, babel-polyfill.'
})
.help('h')
.alias('h', 'help')
.version(require('../package.json').version)
.example('$0 npm test', 'instrument your tests with coverage')
.example('$0 --require babel-core/polyfill --require babel-core/register npm test', 'instrument your tests with coverage and babel')
.example('$0 report --reporter=text-lcov', 'output lcov report after running your tests')
.epilog('visit http://git.io/vTJJB for list of available reporters')
var argv = yargs.argv
if (~argv._.indexOf('report')) {
// run a report.
process.env.NYC_CWD = process.cwd()
report(argv)
} else if (~argv._.indexOf('check-coverage')) {
foreground(
process.execPath,
[
require.resolve('istanbul/lib/cli'),
'check-coverage',
'--lines=' + argv.lines,
'--functions=' + argv.functions,
'--branches=' + argv.branches,
'--statements=' + argv.statements,
path.resolve(process.cwd(), './.nyc_output/*.json')
]
)
} else if (argv._.length) {
// wrap subprocesses and execute argv[1]
var nyc = (new NYC())
nyc.cleanup()
if (argv.all) nyc.addAllFiles()
if (!Array.isArray(argv.require)) argv.require = [argv.require]
sw([__filename], {
NYC_CWD: process.cwd(),
NYC_REQUIRE: argv.require.join(',')
})
foreground(nyc.mungeArgs(argv), function (done) {
if (!argv.silent) report(argv)
return done()
})
} else {
// I don't have a clue what you're doing.
yargs.showHelp()
}
}
function report (argv) {
process.env.NYC_CWD = process.cwd()
;(new NYC({
reporter: argv.reporter
})).report()
}