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('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 | .options('allowExternal', {
|
93 | default: false,
|
94 | type: 'boolean',
|
95 | describe: 'supplying --allowExternal will cause c8 to allow files from outside of your cwd. This applies both to ' +
|
96 | 'files discovered in coverage temp files and also src files discovered if using the --all flag.'
|
97 | })
|
98 | .options('src', {
|
99 | default: undefined,
|
100 | type: 'string',
|
101 | describe: 'supplying --src will override cwd as the default location where --all looks for src files. --src can be ' +
|
102 | 'supplied multiple times and each directory will be included. This allows for workspaces spanning multiple projects'
|
103 | })
|
104 | .pkgConf('c8')
|
105 | .config(config)
|
106 | .demandCommand(1)
|
107 | .check((argv) => {
|
108 | if (!argv.tempDirectory) {
|
109 | argv.tempDirectory = resolve(argv.reportsDir, 'tmp')
|
110 | }
|
111 | return true
|
112 | })
|
113 | .epilog('visit https://git.io/vHysA for list of available reporters')
|
114 |
|
115 | const checkCoverage = require('./commands/check-coverage')
|
116 | const report = require('./commands/report')
|
117 | if (withCommands) {
|
118 | yargs.command(checkCoverage)
|
119 | yargs.command(report)
|
120 | } else {
|
121 | yargs.command(checkCoverage.command, checkCoverage.describe)
|
122 | yargs.command(report.command, report.describe)
|
123 | }
|
124 |
|
125 | return yargs
|
126 | }
|
127 |
|
128 | function hideInstrumenterArgs (yargv) {
|
129 | var argv = process.argv.slice(1)
|
130 | argv = argv.slice(argv.indexOf(yargv._[0]))
|
131 | if (argv[0][0] === '-') {
|
132 | argv.unshift(process.execPath)
|
133 | }
|
134 | return argv
|
135 | }
|
136 |
|
137 | function hideInstrumenteeArgs () {
|
138 | let argv = process.argv.slice(2)
|
139 | const yargv = parser(argv)
|
140 |
|
141 | if (!yargv._.length) return argv
|
142 |
|
143 |
|
144 |
|
145 | argv = argv.slice(0, argv.indexOf(yargv._[0]))
|
146 | argv.push(yargv._[0])
|
147 |
|
148 | return argv
|
149 | }
|
150 |
|
151 | module.exports = {
|
152 | buildYargs,
|
153 | hideInstrumenterArgs,
|
154 | hideInstrumenteeArgs
|
155 | }
|