| 1 |
'use strict' |
| 2 |
|
| 3 |
const path = require('path') |
| 4 |
const findUp = require('find-up') |
| 5 |
const Yargs = require('yargs/yargs') |
| 6 |
|
| 7 |
const { setupOptions } = require('./commands/helpers') |
| 8 |
const processArgs = require('./process-args') |
| 9 |
const { loadNycConfig } = require('@istanbuljs/load-nyc-config') |
| 10 |
|
| 11 |
async function guessCWD (cwd) { |
| 12 |
cwd = cwd || process.env.NYC_CWD || process.cwd() |
| 13 |
const pkgPath = await findUp('package.json', { cwd }) |
| 14 |
if (pkgPath) { |
| 15 |
cwd = path.dirname(pkgPath) |
| 16 |
} |
| 17 |
|
| 18 |
return cwd |
| 19 |
} |
| 20 |
|
| 21 |
async function processConfig (cwd) { |
| 22 |
cwd = await guessCWD(cwd) |
| 23 |
const yargs = Yargs([]) |
| 24 |
.usage('$0 [command] [options]') |
| 25 |
.usage('$0 [options] [bin-to-instrument]') |
| 26 |
.showHidden(false) |
| 27 |
|
| 28 |
setupOptions(yargs, null, cwd) |
| 29 |
|
| 30 |
yargs |
| 31 |
.example('$0 npm test', 'instrument your tests with coverage') |
| 32 |
.example('$0 --require @babel/register npm test', 'instrument your tests with coverage and transpile with Babel') |
| 33 |
.example('$0 report --reporter=text-lcov', 'output lcov report after running your tests') |
| 34 |
.epilog('visit https://git.io/vHysA for list of available reporters') |
| 35 |
.boolean('h') |
| 36 |
.boolean('version') |
| 37 |
.help(false) |
| 38 |
.version(false) |
| 39 |
|
| 40 |
const instrumenterArgs = processArgs.hideInstrumenteeArgs() |
| 41 |
|
| 42 |
// This yargs.parse must come before any options that exit post-hoc |
| 43 |
const childArgs = processArgs.hideInstrumenterArgs(yargs.parse(process.argv.slice(2))) |
| 44 |
const config = await loadNycConfig(yargs.parse(instrumenterArgs)) |
| 45 |
|
| 46 |
yargs |
| 47 |
.config(config) |
| 48 |
.help('h') |
| 49 |
.alias('h', 'help') |
| 50 |
.version() |
| 51 |
.command(require('./commands/check-coverage')) |
| 52 |
.command(require('./commands/instrument')) |
| 53 |
.command(require('./commands/report')) |
| 54 |
.command(require('./commands/merge')) |
| 55 |
|
| 56 |
return { |
| 57 |
get argv () { |
| 58 |
return yargs.parse(instrumenterArgs) |
| 59 |
}, |
| 60 |
childArgs, |
| 61 |
yargs |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
module.exports = processConfig |