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