forked from bcoe/c8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-args.js
137 lines (127 loc) · 3.79 KB
/
parse-args.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
const Exclude = require('test-exclude')
const findUp = require('find-up')
const { readFileSync } = require('fs')
const Yargs = require('yargs/yargs')
const parser = require('yargs-parser')
const { resolve } = require('path')
const configPath = findUp.sync(['.c8rc', '.c8rc.json', '.nycrc', '.nycrc.json'])
const config = configPath ? JSON.parse(readFileSync(configPath)) : {}
function buildYargs (withCommands = false) {
const yargs = Yargs([])
.usage('$0 [opts] [script] [opts]')
.option('reporter', {
alias: 'r',
describe: 'coverage reporter(s) to use',
default: 'text'
})
.option('reports-dir', {
alias: ['o', 'report-dir'],
describe: 'directory where coverage reports will be output to',
default: './coverage'
})
.option('exclude', {
alias: 'x',
default: Exclude.defaultExclude,
describe: 'a list of specific files and directories that should be excluded from coverage (glob patterns are supported)'
})
.option('include', {
alias: 'n',
default: [],
describe: 'a list of specific files that should be covered (glob patterns are supported)'
})
.option('check-coverage', {
default: false,
type: 'boolean',
description: 'check whether coverage is within thresholds provided'
})
.option('branches', {
default: 0,
description: 'what % of branches must be covered?',
type: 'number'
})
.option('functions', {
default: 0,
description: 'what % of functions must be covered?',
type: 'number'
})
.option('lines', {
default: 90,
description: 'what % of lines must be covered?',
type: 'number'
})
.option('statements', {
default: 0,
description: 'what % of statements must be covered?',
type: 'number'
})
.option('per-file', {
default: false,
description: 'check thresholds per file',
type: 'boolean'
})
.option('temp-directory', {
describe: 'directory V8 coverage data is written to and read from',
default: process.env.NODE_V8_COVERAGE
})
.option('resolve', {
default: '',
describe: 'resolve paths to alternate base directory'
})
.option('wrapper-length', {
describe: 'how many bytes is the wrapper prefix on executed JavaScript',
type: 'number'
})
.option('omit-relative', {
default: true,
type: 'boolean',
describe: 'omit any paths that are not absolute, e.g., internal/net.js'
})
.option('clean', {
default: true,
type: 'boolean',
describe: 'should temp files be deleted before script execution'
})
.pkgConf('c8')
.config(config)
.demandCommand(1)
.check((argv) => {
if (!argv.tempDirectory) {
argv.tempDirectory = resolve(argv.reportsDir, 'tmp')
}
return true
})
.epilog('visit https://git.io/vHysA for list of available reporters')
const checkCoverage = require('./commands/check-coverage')
const report = require('./commands/report')
if (withCommands) {
yargs.command(checkCoverage)
yargs.command(report)
} else {
yargs.command(checkCoverage.command, checkCoverage.describe)
yargs.command(report.command, report.describe)
}
return yargs
}
function hideInstrumenterArgs (yargv) {
var argv = process.argv.slice(1)
argv = argv.slice(argv.indexOf(yargv._[0]))
if (argv[0][0] === '-') {
argv.unshift(process.execPath)
}
return argv
}
function hideInstrumenteeArgs () {
let argv = process.argv.slice(2)
const yargv = parser(argv)
if (!yargv._.length) return argv
// drop all the arguments after the bin being
// instrumented by c8.
argv = argv.slice(0, argv.indexOf(yargv._[0]))
argv.push(yargv._[0])
return argv
}
module.exports = {
buildYargs,
hideInstrumenterArgs,
hideInstrumenteeArgs
}