1 | const defaultExclude = require('@istanbuljs/schema/default-exclude')
|
2 | const findUp = require('find-up')
|
3 | const { readFileSync } = require('fs')
|
4 | const Yargs = require('yargs/yargs')
|
5 | const parser = require('yargs-parser')
|
6 | const { resolve } = require('path')
|
7 |
|
8 | function buildYargs (withCommands = false) {
|
9 | const yargs = Yargs([])
|
10 | .usage('$0 [opts] [script] [opts]')
|
11 | .options('config', {
|
12 | alias: 'c',
|
13 | config: true,
|
14 | describe: 'path to JSON configuration file',
|
15 | configParser: (path) => JSON.parse(readFileSync(path)),
|
16 | default: () => findUp.sync(['.c8rc', '.c8rc.json', '.nycrc', '.nycrc.json'])
|
17 | })
|
18 | .option('reporter', {
|
19 | alias: 'r',
|
20 | group: 'Reporting options',
|
21 | describe: 'coverage reporter(s) to use',
|
22 | default: 'text'
|
23 | })
|
24 | .option('reports-dir', {
|
25 | alias: ['o', 'report-dir'],
|
26 | group: 'Reporting options',
|
27 | describe: 'directory where coverage reports will be output to',
|
28 | default: './coverage'
|
29 | })
|
30 | .options('all', {
|
31 | default: false,
|
32 | type: 'boolean',
|
33 | group: 'Reporting options',
|
34 | describe: 'supplying --all will cause c8 to consider all src files in the current working directory ' +
|
35 | 'when the determining coverage. Respects include/exclude.'
|
36 | })
|
37 | .options('src', {
|
38 | default: undefined,
|
39 | type: 'string',
|
40 | group: 'Reporting options',
|
41 | describe: 'supplying --src will override cwd as the default location where --all looks for src files. --src can be ' +
|
42 | 'supplied multiple times and each directory will be included. This allows for workspaces spanning multiple projects'
|
43 | })
|
44 | .option('include', {
|
45 | alias: 'n',
|
46 | default: [],
|
47 | group: 'Reporting options',
|
48 | describe: 'a list of specific files that should be covered (glob patterns are supported)'
|
49 | })
|
50 | .option('exclude', {
|
51 | alias: 'x',
|
52 | default: defaultExclude,
|
53 | group: 'Reporting options',
|
54 | describe: 'a list of specific files and directories that should be excluded from coverage (glob patterns are supported)'
|
55 | })
|
56 | .option('exclude-after-remap', {
|
57 | alias: 'a',
|
58 | type: 'boolean',
|
59 | default: false,
|
60 | group: 'Reporting options',
|
61 | describe: 'apply exclude logic to files after they are remapped by a source-map'
|
62 | })
|
63 | .options('skip-full', {
|
64 | default: false,
|
65 | type: 'boolean',
|
66 | group: 'Reporting options',
|
67 | describe: 'do not show files with 100% statement, branch, and function coverage'
|
68 | })
|
69 | .option('check-coverage', {
|
70 | default: false,
|
71 | type: 'boolean',
|
72 | group: 'Coverage thresholds',
|
73 | description: 'check whether coverage is within thresholds provided'
|
74 | })
|
75 | .option('branches', {
|
76 | default: 0,
|
77 | group: 'Coverage thresholds',
|
78 | description: 'what % of branches must be covered?',
|
79 | type: 'number'
|
80 | })
|
81 | .option('functions', {
|
82 | default: 0,
|
83 | group: 'Coverage thresholds',
|
84 | description: 'what % of functions must be covered?',
|
85 | type: 'number'
|
86 | })
|
87 | .option('lines', {
|
88 | default: 90,
|
89 | group: 'Coverage thresholds',
|
90 | description: 'what % of lines must be covered?',
|
91 | type: 'number'
|
92 | })
|
93 | .option('statements', {
|
94 | default: 0,
|
95 | group: 'Coverage thresholds',
|
96 | description: 'what % of statements must be covered?',
|
97 | type: 'number'
|
98 | })
|
99 | .option('per-file', {
|
100 | default: false,
|
101 | group: 'Coverage thresholds',
|
102 | description: 'check thresholds per file',
|
103 | type: 'boolean'
|
104 | })
|
105 | .option('temp-directory', {
|
106 | describe: 'directory V8 coverage data is written to and read from',
|
107 | default: process.env.NODE_V8_COVERAGE
|
108 | })
|
109 | .option('clean', {
|
110 | default: true,
|
111 | type: 'boolean',
|
112 | describe: 'should temp files be deleted before script execution'
|
113 | })
|
114 | .option('resolve', {
|
115 | default: '',
|
116 | describe: 'resolve paths to alternate base directory'
|
117 | })
|
118 | .option('wrapper-length', {
|
119 | describe: 'how many bytes is the wrapper prefix on executed JavaScript',
|
120 | type: 'number'
|
121 | })
|
122 | .option('omit-relative', {
|
123 | default: true,
|
124 | type: 'boolean',
|
125 | describe: 'omit any paths that are not absolute, e.g., internal/net.js'
|
126 | })
|
127 | .options('allowExternal', {
|
128 | default: false,
|
129 | type: 'boolean',
|
130 | describe: 'supplying --allowExternal will cause c8 to allow files from outside of your cwd. This applies both to ' +
|
131 | 'files discovered in coverage temp files and also src files discovered if using the --all flag.'
|
132 | })
|
133 | .pkgConf('c8')
|
134 | .demandCommand(1)
|
135 | .check((argv) => {
|
136 | if (!argv.tempDirectory) {
|
137 | argv.tempDirectory = resolve(argv.reportsDir, 'tmp')
|
138 | }
|
139 | return true
|
140 | })
|
141 | .epilog('visit https://git.io/vHysA for list of available reporters')
|
142 |
|
143 | const checkCoverage = require('./commands/check-coverage')
|
144 | const report = require('./commands/report')
|
145 | if (withCommands) {
|
146 | yargs.command(checkCoverage)
|
147 | yargs.command(report)
|
148 | } else {
|
149 | yargs.command(checkCoverage.command, checkCoverage.describe)
|
150 | yargs.command(report.command, report.describe)
|
151 | }
|
152 |
|
153 | return yargs
|
154 | }
|
155 |
|
156 | function hideInstrumenterArgs (yargv) {
|
157 | let argv = process.argv.slice(1)
|
158 | argv = argv.slice(argv.indexOf(yargv._[0]))
|
159 | if (argv[0][0] === '-') {
|
160 | argv.unshift(process.execPath)
|
161 | }
|
162 | return argv
|
163 | }
|
164 |
|
165 | function hideInstrumenteeArgs () {
|
166 | let argv = process.argv.slice(2)
|
167 | const yargv = parser(argv)
|
168 |
|
169 | if (!yargv._.length) return argv
|
170 |
|
171 |
|
172 |
|
173 | argv = argv.slice(0, argv.indexOf(yargv._[0]))
|
174 | argv.push(yargv._[0])
|
175 |
|
176 | return argv
|
177 | }
|
178 |
|
179 | module.exports = {
|
180 | buildYargs,
|
181 | hideInstrumenterArgs,
|
182 | hideInstrumenteeArgs
|
183 | }
|