Skip to content

Commit fd40d49

Browse files
authored
feat: Use @istanbuljs/schema for yargs setup (#1194)
BREAKING CHANGE: The `flow` and `jsx` parser plugins are no longer enabled by default.
1 parent 96b60b8 commit fd40d49

12 files changed

+97
-480
lines changed

lib/commands/check-coverage.js

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
const testExclude = require('test-exclude')
21
const NYC = require('../../index.js')
3-
const cliWrapper = require('./cli-wrapper.js')
2+
const { cliWrapper, setupOptions } = require('./helpers.js')
43

54
exports.command = 'check-coverage'
65

@@ -9,61 +8,9 @@ exports.describe = 'check whether coverage is within thresholds provided'
98
exports.builder = function (yargs) {
109
yargs
1110
.demandCommand(0, 0)
12-
.option('exclude', {
13-
alias: 'x',
14-
default: testExclude.defaultExclude,
15-
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded',
16-
global: false
17-
})
18-
.option('exclude-node-modules', {
19-
default: true,
20-
type: 'boolean',
21-
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
22-
global: false
23-
})
24-
.option('exclude-after-remap', {
25-
default: true,
26-
type: 'boolean',
27-
description: 'should exclude logic be performed after the source-map remaps filenames?',
28-
global: false
29-
})
30-
.option('include', {
31-
alias: 'n',
32-
default: [],
33-
describe: 'a list of specific files that should be covered, glob patterns are supported',
34-
global: false
35-
})
36-
.option('branches', {
37-
default: 0,
38-
description: 'what % of branches must be covered?'
39-
})
40-
.option('functions', {
41-
default: 0,
42-
description: 'what % of functions must be covered?'
43-
})
44-
.option('lines', {
45-
default: 90,
46-
description: 'what % of lines must be covered?'
47-
})
48-
.option('statements', {
49-
default: 0,
50-
description: 'what % of statements must be covered?'
51-
})
52-
.option('per-file', {
53-
default: false,
54-
description: 'check thresholds per file'
55-
})
56-
.option('temp-dir', {
57-
alias: 't',
58-
describe: 'directory to read raw coverage information from',
59-
default: './.nyc_output',
60-
global: false
61-
})
62-
.option('temp-directory', {
63-
hidden: true,
64-
global: false
65-
})
6611
.example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")
12+
13+
setupOptions(yargs, 'check-coverage')
6714
}
6815

6916
exports.handler = cliWrapper(async argv => {

lib/commands/cli-wrapper.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/commands/helpers.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
3+
const decamelize = require('decamelize')
4+
const schema = require('@istanbuljs/schema')
5+
6+
/* These options still need to be connected to the instrumenter
7+
* Disabling them for now also avoids the issue with OSX cutting
8+
* off the error help screen at 8192 characters.
9+
*/
10+
const blockOptions = [
11+
'coverageVariable',
12+
'coverageGlobalScope',
13+
'coverageGlobalScopeFunc'
14+
]
15+
16+
module.exports = {
17+
setupOptions (yargs, command, cwd) {
18+
Object.entries(schema.nyc.properties).forEach(([name, setup]) => {
19+
if (blockOptions.includes(name)) {
20+
return
21+
}
22+
23+
const option = {
24+
description: setup.description,
25+
default: setup.default,
26+
type: setup.type
27+
}
28+
29+
if (name === 'cwd') {
30+
if (command !== null) {
31+
return
32+
}
33+
34+
option.default = cwd
35+
option.global = true
36+
}
37+
38+
if (option.type === 'array') {
39+
option.type = 'string'
40+
}
41+
42+
if ('nycAlias' in setup) {
43+
option.alias = setup.nycAlias
44+
}
45+
46+
const optionName = decamelize(name, '-')
47+
yargs.option(optionName, option)
48+
if (!setup.nycCommands.includes(command)) {
49+
yargs.hide(optionName)
50+
}
51+
})
52+
},
53+
cliWrapper (execute) {
54+
return argv => {
55+
execute(argv).catch(error => {
56+
console.error(error.message)
57+
process.exit(1)
58+
})
59+
}
60+
}
61+
}

lib/commands/instrument.js

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,18 @@ const NYC = require('../../index.js')
22
const path = require('path')
33
const { promisify } = require('util')
44
const rimraf = promisify(require('rimraf'))
5-
const testExclude = require('test-exclude')
6-
const cliWrapper = require('./cli-wrapper.js')
5+
const { cliWrapper, setupOptions } = require('./helpers.js')
76

87
exports.command = 'instrument <input> [output]'
98

109
exports.describe = 'instruments a file or a directory tree and writes the instrumented code to the desired output location'
1110

1211
exports.builder = function (yargs) {
13-
return yargs
12+
yargs
1413
.demandCommand(0, 0)
15-
.positional('input', {
16-
describe: 'file or directory to instrument',
17-
type: 'text'
18-
})
19-
.positional('output', {
20-
describe: 'directory to output instrumented files',
21-
type: 'text'
22-
})
23-
.option('require', {
24-
alias: 'i',
25-
default: [],
26-
describe: 'a list of additional modules that nyc should attempt to require in its subprocess, e.g., @babel/register, @babel/polyfill.'
27-
})
28-
.option('extension', {
29-
alias: 'e',
30-
default: ['.cjs', '.mjs', '.ts', '.tsx', '.jsx'],
31-
describe: 'a list of extensions that nyc should handle in addition to .js'
32-
})
33-
.option('source-map', {
34-
default: true,
35-
type: 'boolean',
36-
describe: 'should nyc detect and handle source maps?'
37-
})
38-
.option('produce-source-map', {
39-
default: false,
40-
type: 'boolean',
41-
describe: "should nyc's instrumenter produce source maps?"
42-
})
43-
.option('compact', {
44-
default: true,
45-
type: 'boolean',
46-
describe: 'should the output be compacted?'
47-
})
48-
.option('preserve-comments', {
49-
default: true,
50-
type: 'boolean',
51-
describe: 'should comments be preserved in the output?'
52-
})
53-
.option('instrument', {
54-
default: true,
55-
type: 'boolean',
56-
describe: 'should nyc handle instrumentation?'
57-
})
58-
.option('in-place', {
59-
default: false,
60-
type: 'boolean',
61-
describe: 'should nyc run the instrumentation in place?'
62-
})
63-
.option('exit-on-error', {
64-
default: false,
65-
type: 'boolean',
66-
describe: 'should nyc exit when an instrumentation failure occurs?'
67-
})
68-
.option('include', {
69-
alias: 'n',
70-
default: [],
71-
describe: 'a list of specific files and directories that should be instrumented, glob patterns are supported'
72-
})
73-
.option('exclude', {
74-
alias: 'x',
75-
default: testExclude.defaultExclude,
76-
describe: 'a list of specific files and directories that should not be instrumented, glob patterns are supported'
77-
})
78-
.option('exclude-node-modules', {
79-
default: true,
80-
type: 'boolean',
81-
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
82-
global: false
83-
})
84-
.option('es-modules', {
85-
default: true,
86-
type: 'boolean',
87-
description: 'tell the instrumenter to treat files as ES Modules'
88-
})
89-
.option('delete', {
90-
describe: 'should the output folder be deleted before instrumenting files?',
91-
default: false,
92-
type: 'boolean'
93-
})
94-
.option('complete-copy', {
95-
describe: 'should nyc copy all files from input to output as well as instrumented files?',
96-
default: false,
97-
type: 'boolean'
98-
})
9914
.example('$0 instrument ./lib ./output', 'instrument all .js files in ./lib with coverage and output in ./output')
15+
16+
setupOptions(yargs, 'instrument')
10017
}
10118

10219
exports.handler = cliWrapper(async argv => {

lib/commands/merge.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const path = require('path')
33
const makeDir = require('make-dir')
44
const fs = require('../fs-promises')
5-
const cliWrapper = require('./cli-wrapper.js')
5+
const { cliWrapper, setupOptions } = require('./helpers.js')
66

77
const NYC = require('../../index.js')
88

@@ -11,8 +11,9 @@ exports.command = 'merge <input-directory> [output-file]'
1111
exports.describe = 'merge istanbul format coverage output in a given folder'
1212

1313
exports.builder = function (yargs) {
14-
return yargs
14+
yargs
1515
.demandCommand(0, 0)
16+
.example('$0 merge ./out coverage.json', 'merge together reports in ./out and output as coverage.json')
1617
.positional('input-directory', {
1718
describe: 'directory containing multiple istanbul coverage files',
1819
type: 'text',
@@ -23,15 +24,9 @@ exports.builder = function (yargs) {
2324
type: 'text',
2425
default: 'coverage.json'
2526
})
26-
.option('temp-dir', {
27-
alias: 't',
28-
describe: 'directory to read raw coverage information from',
29-
default: './.nyc_output'
30-
})
31-
.option('temp-directory', {
32-
hidden: true
33-
})
34-
.example('$0 merge ./out coverage.json', 'merge together reports in ./out and output as coverage.json')
27+
28+
setupOptions(yargs, 'merge')
29+
yargs.default('exclude-after-remap', false)
3530
}
3631

3732
exports.handler = cliWrapper(async argv => {

lib/commands/report.js

Lines changed: 4 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,16 @@
1-
const testExclude = require('test-exclude')
21
const NYC = require('../../index.js')
3-
const cliWrapper = require('./cli-wrapper.js')
2+
const { cliWrapper, setupOptions } = require('./helpers.js')
43

54
exports.command = 'report'
65

76
exports.describe = 'run coverage report for .nyc_output'
87

98
exports.builder = function (yargs) {
10-
return yargs
9+
yargs
1110
.demandCommand(0, 0)
12-
.option('reporter', {
13-
alias: 'r',
14-
describe: 'coverage reporter(s) to use',
15-
default: 'text'
16-
})
17-
.option('report-dir', {
18-
describe: 'directory to output coverage reports in',
19-
default: 'coverage'
20-
})
21-
.option('temp-dir', {
22-
alias: 't',
23-
describe: 'directory to read raw coverage information from',
24-
default: './.nyc_output'
25-
})
26-
.option('temp-directory', {
27-
hidden: true
28-
})
29-
.option('exclude', {
30-
alias: 'x',
31-
default: testExclude.defaultExclude,
32-
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded',
33-
global: false
34-
})
35-
.option('exclude-node-modules', {
36-
default: true,
37-
type: 'boolean',
38-
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
39-
global: false
40-
})
41-
.option('exclude-after-remap', {
42-
default: true,
43-
type: 'boolean',
44-
description: 'should exclude logic be performed after the source-map remaps filenames?',
45-
global: false
46-
})
47-
.option('include', {
48-
alias: 'n',
49-
default: [],
50-
describe: 'a list of specific files that should be covered, glob patterns are supported',
51-
global: false
52-
})
53-
.option('extension', {
54-
alias: 'e',
55-
default: ['.cjs', '.mjs', '.ts', '.tsx', '.jsx'],
56-
describe: 'a list of extensions that nyc should handle in addition to .js',
57-
global: false
58-
})
59-
.option('show-process-tree', {
60-
describe: 'display the tree of spawned processes',
61-
default: false,
62-
type: 'boolean'
63-
})
64-
.option('skip-empty', {
65-
describe: 'don\'t show empty files (no lines of code) in report',
66-
default: false,
67-
type: 'boolean',
68-
global: false
69-
})
70-
.option('check-coverage', {
71-
type: 'boolean',
72-
default: false,
73-
describe: 'check whether coverage is within thresholds provided',
74-
global: false
75-
})
76-
.option('branches', {
77-
default: 0,
78-
description: 'what % of branches must be covered?',
79-
global: false
80-
})
81-
.option('functions', {
82-
default: 0,
83-
description: 'what % of functions must be covered?',
84-
global: false
85-
})
86-
.option('lines', {
87-
default: 90,
88-
description: 'what % of lines must be covered?',
89-
global: false
90-
})
91-
.option('statements', {
92-
default: 0,
93-
description: 'what % of statements must be covered?',
94-
global: false
95-
})
96-
.option('per-file', {
97-
default: false,
98-
type: 'boolean',
99-
description: 'check thresholds per file',
100-
global: false
101-
})
10211
.example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
12+
13+
setupOptions(yargs, 'report')
10314
}
10415

10516
exports.handler = cliWrapper(async argv => {

0 commit comments

Comments
 (0)