1 | const Exclude = require('test-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: Exclude.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 | .pkgConf('c8')
|
87 | .config(config)
|
88 | .demandCommand(1)
|
89 | .check((argv) => {
|
90 | if (!argv.tempDirectory) {
|
91 | argv.tempDirectory = resolve(argv.reportsDir, 'tmp')
|
92 | }
|
93 | return true
|
94 | })
|
95 | .epilog('visit https://git.io/vHysA for list of available reporters')
|
96 |
|
97 | const checkCoverage = require('./commands/check-coverage')
|
98 | const report = require('./commands/report')
|
99 | if (withCommands) {
|
100 | yargs.command(checkCoverage)
|
101 | yargs.command(report)
|
102 | } else {
|
103 | yargs.command(checkCoverage.command, checkCoverage.describe)
|
104 | yargs.command(report.command, report.describe)
|
105 | }
|
106 |
|
107 | return yargs
|
108 | }
|
109 |
|
110 | function hideInstrumenterArgs (yargv) {
|
111 | var argv = process.argv.slice(1)
|
112 | argv = argv.slice(argv.indexOf(yargv._[0]))
|
113 | if (argv[0][0] === '-') {
|
114 | argv.unshift(process.execPath)
|
115 | }
|
116 | return argv
|
117 | }
|
118 |
|
119 | function hideInstrumenteeArgs () {
|
120 | let argv = process.argv.slice(2)
|
121 | const yargv = parser(argv)
|
122 |
|
123 | if (!yargv._.length) return argv
|
124 |
|
125 |
|
126 |
|
127 | argv = argv.slice(0, argv.indexOf(yargv._[0]))
|
128 | argv.push(yargv._[0])
|
129 |
|
130 | return argv
|
131 | }
|
132 |
|
133 | module.exports = {
|
134 | buildYargs,
|
135 | hideInstrumenterArgs,
|
136 | hideInstrumenteeArgs
|
137 | }
|