Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use @istanbuljs/schema for yargs setup #1194

Merged
merged 1 commit into from Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 3 additions & 56 deletions lib/commands/check-coverage.js
@@ -1,6 +1,5 @@
const testExclude = require('test-exclude')
const NYC = require('../../index.js')
const cliWrapper = require('./cli-wrapper.js')
const { cliWrapper, setupOptions } = require('./helpers.js')

exports.command = 'check-coverage'

Expand All @@ -9,61 +8,9 @@ exports.describe = 'check whether coverage is within thresholds provided'
exports.builder = function (yargs) {
yargs
.demandCommand(0, 0)
.option('exclude', {
alias: 'x',
default: testExclude.defaultExclude,
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded',
global: false
})
.option('exclude-node-modules', {
default: true,
type: 'boolean',
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
global: false
})
.option('exclude-after-remap', {
default: true,
type: 'boolean',
description: 'should exclude logic be performed after the source-map remaps filenames?',
global: false
})
.option('include', {
alias: 'n',
default: [],
describe: 'a list of specific files that should be covered, glob patterns are supported',
global: false
})
.option('branches', {
default: 0,
description: 'what % of branches must be covered?'
})
.option('functions', {
default: 0,
description: 'what % of functions must be covered?'
})
.option('lines', {
default: 90,
description: 'what % of lines must be covered?'
})
.option('statements', {
default: 0,
description: 'what % of statements must be covered?'
})
.option('per-file', {
default: false,
description: 'check thresholds per file'
})
.option('temp-dir', {
alias: 't',
describe: 'directory to read raw coverage information from',
default: './.nyc_output',
global: false
})
.option('temp-directory', {
hidden: true,
global: false
})
.example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")

setupOptions(yargs, 'check-coverage')
}

exports.handler = cliWrapper(async argv => {
Expand Down
10 changes: 0 additions & 10 deletions lib/commands/cli-wrapper.js

This file was deleted.

61 changes: 61 additions & 0 deletions lib/commands/helpers.js
@@ -0,0 +1,61 @@
'use strict'

const decamelize = require('decamelize')
const schema = require('@istanbuljs/schema')

/* These options still need to be connected to the instrumenter
* Disabling them for now also avoids the issue with OSX cutting
* off the error help screen at 8192 characters.
*/
const blockOptions = [
'coverageVariable',
'coverageGlobalScope',
'coverageGlobalScopeFunc'
]

module.exports = {
setupOptions (yargs, command, cwd) {
Object.entries(schema.nyc.properties).forEach(([name, setup]) => {
if (blockOptions.includes(name)) {
return
}

const option = {
description: setup.description,
default: setup.default,
type: setup.type
}

if (name === 'cwd') {
if (command !== null) {
return
}

option.default = cwd
option.global = true
}

if (option.type === 'array') {
option.type = 'string'
}

if ('nycAlias' in setup) {
option.alias = setup.nycAlias
}

const optionName = decamelize(name, '-')
yargs.option(optionName, option)
if (!setup.nycCommands.includes(command)) {
yargs.hide(optionName)
}
})
},
cliWrapper (execute) {
return argv => {
execute(argv).catch(error => {
console.error(error.message)
process.exit(1)
})
}
}
}
91 changes: 4 additions & 87 deletions lib/commands/instrument.js
Expand Up @@ -2,101 +2,18 @@ const NYC = require('../../index.js')
const path = require('path')
const { promisify } = require('util')
const rimraf = promisify(require('rimraf'))
const testExclude = require('test-exclude')
const cliWrapper = require('./cli-wrapper.js')
const { cliWrapper, setupOptions } = require('./helpers.js')

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

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

exports.builder = function (yargs) {
return yargs
yargs
.demandCommand(0, 0)
.positional('input', {
describe: 'file or directory to instrument',
type: 'text'
})
.positional('output', {
describe: 'directory to output instrumented files',
type: 'text'
})
.option('require', {
alias: 'i',
default: [],
describe: 'a list of additional modules that nyc should attempt to require in its subprocess, e.g., @babel/register, @babel/polyfill.'
})
.option('extension', {
alias: 'e',
default: ['.cjs', '.mjs', '.ts', '.tsx', '.jsx'],
describe: 'a list of extensions that nyc should handle in addition to .js'
})
.option('source-map', {
default: true,
type: 'boolean',
describe: 'should nyc detect and handle source maps?'
})
.option('produce-source-map', {
default: false,
type: 'boolean',
describe: "should nyc's instrumenter produce source maps?"
})
.option('compact', {
default: true,
type: 'boolean',
describe: 'should the output be compacted?'
})
.option('preserve-comments', {
default: true,
type: 'boolean',
describe: 'should comments be preserved in the output?'
})
.option('instrument', {
default: true,
type: 'boolean',
describe: 'should nyc handle instrumentation?'
})
.option('in-place', {
default: false,
type: 'boolean',
describe: 'should nyc run the instrumentation in place?'
})
.option('exit-on-error', {
default: false,
type: 'boolean',
describe: 'should nyc exit when an instrumentation failure occurs?'
})
.option('include', {
alias: 'n',
default: [],
describe: 'a list of specific files and directories that should be instrumented, glob patterns are supported'
})
.option('exclude', {
alias: 'x',
default: testExclude.defaultExclude,
describe: 'a list of specific files and directories that should not be instrumented, glob patterns are supported'
})
.option('exclude-node-modules', {
default: true,
type: 'boolean',
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
global: false
})
.option('es-modules', {
default: true,
type: 'boolean',
description: 'tell the instrumenter to treat files as ES Modules'
})
.option('delete', {
describe: 'should the output folder be deleted before instrumenting files?',
default: false,
type: 'boolean'
})
.option('complete-copy', {
describe: 'should nyc copy all files from input to output as well as instrumented files?',
default: false,
type: 'boolean'
})
.example('$0 instrument ./lib ./output', 'instrument all .js files in ./lib with coverage and output in ./output')

setupOptions(yargs, 'instrument')
}

exports.handler = cliWrapper(async argv => {
Expand Down
17 changes: 6 additions & 11 deletions lib/commands/merge.js
Expand Up @@ -2,7 +2,7 @@
const path = require('path')
const makeDir = require('make-dir')
const fs = require('../fs-promises')
const cliWrapper = require('./cli-wrapper.js')
const { cliWrapper, setupOptions } = require('./helpers.js')

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

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

exports.builder = function (yargs) {
return yargs
yargs
.demandCommand(0, 0)
.example('$0 merge ./out coverage.json', 'merge together reports in ./out and output as coverage.json')
.positional('input-directory', {
describe: 'directory containing multiple istanbul coverage files',
type: 'text',
Expand All @@ -23,15 +24,9 @@ exports.builder = function (yargs) {
type: 'text',
default: 'coverage.json'
})
.option('temp-dir', {
alias: 't',
describe: 'directory to read raw coverage information from',
default: './.nyc_output'
})
.option('temp-directory', {
hidden: true
})
.example('$0 merge ./out coverage.json', 'merge together reports in ./out and output as coverage.json')

setupOptions(yargs, 'merge')
yargs.default('exclude-after-remap', false)
}

exports.handler = cliWrapper(async argv => {
Expand Down
97 changes: 4 additions & 93 deletions lib/commands/report.js
@@ -1,105 +1,16 @@
const testExclude = require('test-exclude')
const NYC = require('../../index.js')
const cliWrapper = require('./cli-wrapper.js')
const { cliWrapper, setupOptions } = require('./helpers.js')

exports.command = 'report'

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

exports.builder = function (yargs) {
return yargs
yargs
.demandCommand(0, 0)
.option('reporter', {
alias: 'r',
describe: 'coverage reporter(s) to use',
default: 'text'
})
.option('report-dir', {
describe: 'directory to output coverage reports in',
default: 'coverage'
})
.option('temp-dir', {
alias: 't',
describe: 'directory to read raw coverage information from',
default: './.nyc_output'
})
.option('temp-directory', {
hidden: true
})
.option('exclude', {
alias: 'x',
default: testExclude.defaultExclude,
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded',
global: false
})
.option('exclude-node-modules', {
default: true,
type: 'boolean',
describe: 'whether or not to exclude all node_module folders (i.e. **/node_modules/**) by default',
global: false
})
.option('exclude-after-remap', {
default: true,
type: 'boolean',
description: 'should exclude logic be performed after the source-map remaps filenames?',
global: false
})
.option('include', {
alias: 'n',
default: [],
describe: 'a list of specific files that should be covered, glob patterns are supported',
global: false
})
.option('extension', {
alias: 'e',
default: ['.cjs', '.mjs', '.ts', '.tsx', '.jsx'],
describe: 'a list of extensions that nyc should handle in addition to .js',
global: false
})
.option('show-process-tree', {
describe: 'display the tree of spawned processes',
default: false,
type: 'boolean'
})
.option('skip-empty', {
describe: 'don\'t show empty files (no lines of code) in report',
default: false,
type: 'boolean',
global: false
})
.option('check-coverage', {
type: 'boolean',
default: false,
describe: 'check whether coverage is within thresholds provided',
global: false
})
.option('branches', {
default: 0,
description: 'what % of branches must be covered?',
global: false
})
.option('functions', {
default: 0,
description: 'what % of functions must be covered?',
global: false
})
.option('lines', {
default: 90,
description: 'what % of lines must be covered?',
global: false
})
.option('statements', {
default: 0,
description: 'what % of statements must be covered?',
global: false
})
.option('per-file', {
default: false,
type: 'boolean',
description: 'check thresholds per file',
global: false
})
.example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')

setupOptions(yargs, 'report')
}

exports.handler = cliWrapper(async argv => {
Expand Down