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

chore: upgrade to yargs 10 #694

Merged
merged 3 commits into from
Oct 22, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions bin/nyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,38 @@ var wrapper = require.resolve('./wrap.js')
// we keep these values in a few different forms,
// used in the various execution contexts of nyc:
// reporting, instrumenting subprocesses, etc.
var yargs = configUtil.decorateYargs(configUtil.buildYargs())
var yargs = configUtil.addCommandsAndHelp(configUtil.buildYargs())
var instrumenterArgs = processArgs.hideInstrumenteeArgs()
var argv = yargs.parse(instrumenterArgs)
var config = configUtil.loadConfig(instrumenterArgs)

if (argv._[0] === 'report') {
// run a report.
process.env.NYC_CWD = process.cwd()

report(config)
// look in lib/commands/report.js for logic.
} else if (argv._[0] === 'check-coverage') {
checkCoverage(config)
// look in lib/commands/check-coverage.js for logic.
} else if (argv._[0] === 'instrument') {
// look in lib/commands/instrument.js for logic.
} else if (argv._.length) {
// if instrument is set to false,
// enable a noop instrumenter.
if (!config.instrument) config.instrumenter = './lib/instrumenters/noop'
else config.instrumenter = './lib/instrumenters/istanbul'
if (!argv.instrument) argv.instrumenter = './lib/instrumenters/noop'
else argv.instrumenter = './lib/instrumenters/istanbul'

var nyc = (new NYC(config))
if (config.clean) {
var nyc = (new NYC(argv))
if (argv.clean) {
nyc.reset()
} else {
nyc.createTempDirectory()
}
if (config.all) nyc.addAllFiles()
if (argv.all) nyc.addAllFiles()

var env = {
NYC_CONFIG: JSON.stringify(config),
NYC_CONFIG: JSON.stringify(argv),
NYC_CWD: process.cwd(),
NYC_ROOT_ID: nyc.rootId,
NYC_INSTRUMENTER: config.instrumenter
NYC_INSTRUMENTER: argv.instrumenter
}

if (config['babel-cache'] === false) {
if (argv['babel-cache'] === false) {
// babel's cache interferes with some configurations, so is
// disabled by default. opt in by setting babel-cache=true.
env.BABEL_DISABLE_CACHE = process.env.BABEL_DISABLE_CACHE = '1'
Expand All @@ -71,13 +67,13 @@ if (argv._[0] === 'report') {
), function (done) {
var mainChildExitCode = process.exitCode

if (config.checkCoverage) {
checkCoverage(config)
if (argv.checkCoverage) {
checkCoverage(argv)
process.exitCode = process.exitCode || mainChildExitCode
if (!config.silent) report(config)
if (!argv.silent) report(argv)
return done()
} else {
if (!config.silent) report(config)
if (!argv.silent) report(argv)
return done()
}
})
Expand Down
46 changes: 46 additions & 0 deletions lib/commands/check-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var NYC
try {
NYC = require('../../index.covered.js')
} catch (e) {
NYC = require('../../index.js')
}

exports.command = 'check-coverage'

exports.describe = 'check whether coverage is within thresholds provided'

exports.builder = function (yargs) {
yargs
.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'
})
.example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")
}

exports.handler = function (argv) {
process.env.NYC_CWD = process.cwd()

;(new NYC(argv)).checkCoverage({
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file'])
}
1 change: 0 additions & 1 deletion lib/commands/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ exports.describe = 'instruments a file or a directory tree and writes the instru

exports.builder = function (yargs) {
return yargs
.usage('$0 instrument <input> [output]')
.option('require', {
alias: 'i',
default: [],
Expand Down
39 changes: 39 additions & 0 deletions lib/commands/report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var NYC
try {
NYC = require('../../index.covered.js')
} catch (e) {
NYC = require('../../index.js')
}

exports.command = 'report'

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

exports.builder = function (yargs) {
return yargs
.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-directory', {
describe: 'directory to read raw coverage information from',
default: './.nyc_output'
})
.option('show-process-tree', {
describe: 'display the tree of spawned processes',
default: false,
type: 'boolean'
})
.example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
}

exports.handler = function (argv) {
process.env.NYC_CWD = process.cwd()
var nyc = new NYC(argv)
nyc.report()
}
114 changes: 34 additions & 80 deletions lib/config-util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const arrify = require('arrify')
const fs = require('fs')
const path = require('path')
Expand All @@ -7,94 +9,41 @@ const Yargs = require('yargs/yargs')

var Config = {}

// load config from a cascade of sources:
// * command line arguments
// * package.json
// * .nycrc
Config.loadConfig = function (argv, cwd) {
function guessCWD (cwd) {
cwd = cwd || process.env.NYC_CWD || process.cwd()
var pkgPath = findUp.sync('package.json', {cwd: cwd})
var rcPath = findUp.sync(['.nycrc', '.nycrc.json'], {cwd: cwd})
var rcConfig = null
const pkgPath = findUp.sync('package.json', {cwd: cwd})
if (pkgPath) {
cwd = path.dirname(pkgPath)
}
return cwd
}

function loadConfig (argv, cwd) {
const rcPath = findUp.sync(['.nycrc', '.nycrc.json'], {cwd: cwd})
let config = {}

if (rcPath) {
rcConfig = JSON.parse(
config = JSON.parse(
fs.readFileSync(rcPath, 'utf-8')
)
}

if (pkgPath) {
cwd = path.dirname(pkgPath)
}

var config = Config.buildYargs(cwd)
if (rcConfig) config.config(rcConfig)
config = config.parse(argv || [])

// post-hoc, we convert several of the
// configuration settings to arrays, providing
// a consistent contract to index.js.
config.require = arrify(config.require)
config.extension = arrify(config.extension)
config.exclude = arrify(config.exclude)
config.include = arrify(config.include)
if (config.require) config.require = arrify(config.require)
if (config.extension) config.extension = arrify(config.extension)
if (config.exclude) config.exclude = arrify(config.exclude)
if (config.include) config.include = arrify(config.include)

return config
}

// build a yargs object, omitting any settings
// that would cause the application to exit early.
Config.buildYargs = function (cwd) {
cwd = guessCWD(cwd)
const config = loadConfig()
return Yargs([])
.usage('$0 [command] [options]\n\nrun your tests with the nyc bin to instrument them with coverage')
.command('report', 'run coverage report for .nyc_output', function (yargs) {
return yargs
.usage('$0 report [options]')
.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-directory', {
describe: 'directory to read raw coverage information from',
default: './.nyc_output'
})
.option('show-process-tree', {
describe: 'display the tree of spawned processes',
default: false,
type: 'boolean'
})
.example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage')
})
.command('check-coverage', 'check whether coverage is within thresholds provided', function (yargs) {
return yargs
.usage('$0 check-coverage [options]')
.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'
})
.example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided")
})
.usage('$0 [command] [options]')
.usage('$0 [options] [bin-to-instrument]')
.option('reporter', {
alias: 'r',
describe: 'coverage reporter(s) to use',
Expand Down Expand Up @@ -139,7 +88,7 @@ Config.buildYargs = function (cwd) {
.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.',
describe: 'a list of additional modules that nyc should attempt to require in its subprocess, e.g., babel-register, babel-polyfill',
global: false
})
.option('eager', {
Expand Down Expand Up @@ -240,24 +189,29 @@ Config.buildYargs = function (cwd) {
default: './.nyc_output',
global: false
})
.pkgConf('nyc', cwd || process.cwd())
.pkgConf('nyc', cwd)
.example('$0 npm test', 'instrument your tests with coverage')
.example('$0 --require babel-core/register npm test', 'instrument your tests with coverage and babel')
.example('$0 --require babel-core/register npm test', 'instrument your tests with coverage and transpile with Babel')
.example('$0 report --reporter=text-lcov', 'output lcov report after running your tests')
.epilog('visit https://git.io/vHysA for list of available reporters')
.boolean('help')
.boolean('h')
.boolean('version')
.config(config)
.help(false)
.version(false)
}

// decorate yargs with all the actions
// that would make it exit: help, version, command.
Config.decorateYargs = function (yargs) {
// we add operations that would make yargs
// exit post-hoc, allowing for a multi-pass
// parsing step.
Config.addCommandsAndHelp = function (yargs) {
return yargs
.help('h')
.alias('h', 'help')
.version()
.command(require('../lib/commands/check-coverage'))
.command(require('../lib/commands/instrument'))
.command(require('../lib/commands/report'))
}

module.exports = Config
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"bundle": "bundle-dependencies update",
"pretest": "standard",
"posttest": "standard",
"test": "npm run clean && npm run build && npm run instrument && npm run test-integration && npm run test-mocha && npm run report",
"clean": "rimraf ./.nyc_output ./node_modules/.cache ./.self_coverage ./test/fixtures/.nyc_output ./test/fixtures/node_modules/.cache *covered.js ./lib/*covered.js",
"build": "node ./build-tests",
Expand Down Expand Up @@ -98,8 +98,8 @@
"signal-exit": "^3.0.1",
"spawn-wrap": "^1.3.8",
"test-exclude": "^4.1.1",
"yargs": "^8.0.1",
"yargs-parser": "^5.0.0"
"yargs": "^10.0.3",
"yargs-parser": "^8.0.0"
},
"devDependencies": {
"any-path": "^1.3.0",
Expand Down
Loading