UNPKG

4.16 kBJavaScriptView Raw
1const defaultExclude = require('@istanbuljs/schema/default-exclude')
2const findUp = require('find-up')
3const { readFileSync } = require('fs')
4const Yargs = require('yargs/yargs')
5const parser = require('yargs-parser')
6const { resolve } = require('path')
7
8const configPath = findUp.sync(['.c8rc', '.c8rc.json', '.nycrc', '.nycrc.json'])
9const config = configPath ? JSON.parse(readFileSync(configPath)) : {}
10
11function buildYargs (withCommands = false) {
12 const yargs = Yargs([])
13 .usage('$0 [opts] [script] [opts]')
14 .option('reporter', {
15 alias: 'r',
16 describe: 'coverage reporter(s) to use',
17 default: 'text'
18 })
19 .option('reports-dir', {
20 alias: ['o', 'report-dir'],
21 describe: 'directory where coverage reports will be output to',
22 default: './coverage'
23 })
24 .option('exclude', {
25 alias: 'x',
26 default: defaultExclude,
27 describe: 'a list of specific files and directories that should be excluded from coverage (glob patterns are supported)'
28 })
29 .option('include', {
30 alias: 'n',
31 default: [],
32 describe: 'a list of specific files that should be covered (glob patterns are supported)'
33 })
34 .option('check-coverage', {
35 default: false,
36 type: 'boolean',
37 description: 'check whether coverage is within thresholds provided'
38 })
39 .option('branches', {
40 default: 0,
41 description: 'what % of branches must be covered?',
42 type: 'number'
43 })
44 .option('functions', {
45 default: 0,
46 description: 'what % of functions must be covered?',
47 type: 'number'
48 })
49 .option('lines', {
50 default: 90,
51 description: 'what % of lines must be covered?',
52 type: 'number'
53 })
54 .option('statements', {
55 default: 0,
56 description: 'what % of statements must be covered?',
57 type: 'number'
58 })
59 .option('per-file', {
60 default: false,
61 description: 'check thresholds per file',
62 type: 'boolean'
63 })
64 .option('temp-directory', {
65 describe: 'directory V8 coverage data is written to and read from',
66 default: process.env.NODE_V8_COVERAGE
67 })
68 .option('resolve', {
69 default: '',
70 describe: 'resolve paths to alternate base directory'
71 })
72 .option('wrapper-length', {
73 describe: 'how many bytes is the wrapper prefix on executed JavaScript',
74 type: 'number'
75 })
76 .option('omit-relative', {
77 default: true,
78 type: 'boolean',
79 describe: 'omit any paths that are not absolute, e.g., internal/net.js'
80 })
81 .option('clean', {
82 default: true,
83 type: 'boolean',
84 describe: 'should temp files be deleted before script execution'
85 })
86 .options('all', {
87 default: false,
88 type: 'boolean',
89 describe: 'supplying --all will cause c8 to consider all src files in the current working directory ' +
90 'when the determining coverage. Respects include/exclude.'
91 })
92 .pkgConf('c8')
93 .config(config)
94 .demandCommand(1)
95 .check((argv) => {
96 if (!argv.tempDirectory) {
97 argv.tempDirectory = resolve(argv.reportsDir, 'tmp')
98 }
99 return true
100 })
101 .epilog('visit https://git.io/vHysA for list of available reporters')
102
103 const checkCoverage = require('./commands/check-coverage')
104 const report = require('./commands/report')
105 if (withCommands) {
106 yargs.command(checkCoverage)
107 yargs.command(report)
108 } else {
109 yargs.command(checkCoverage.command, checkCoverage.describe)
110 yargs.command(report.command, report.describe)
111 }
112
113 return yargs
114}
115
116function hideInstrumenterArgs (yargv) {
117 var argv = process.argv.slice(1)
118 argv = argv.slice(argv.indexOf(yargv._[0]))
119 if (argv[0][0] === '-') {
120 argv.unshift(process.execPath)
121 }
122 return argv
123}
124
125function hideInstrumenteeArgs () {
126 let argv = process.argv.slice(2)
127 const yargv = parser(argv)
128
129 if (!yargv._.length) return argv
130
131 // drop all the arguments after the bin being
132 // instrumented by c8.
133 argv = argv.slice(0, argv.indexOf(yargv._[0]))
134 argv.push(yargv._[0])
135
136 return argv
137}
138
139module.exports = {
140 buildYargs,
141 hideInstrumenterArgs,
142 hideInstrumenteeArgs
143}