|
| 1 | +var arrify = require('arrify') |
| 2 | +var path = require('path') |
| 3 | +var pkgUp = require('pkg-up') |
| 4 | +var testExclude = require('test-exclude') |
| 5 | +var Yargs = require('yargs/yargs') |
| 6 | + |
| 7 | +// load config from a cascade of sources: |
| 8 | +// * command line arguments. |
| 9 | +// * package.json. |
| 10 | +// * .nycrc (coming soon) |
| 11 | +function Config (argv, cwd) { |
| 12 | + cwd = cwd || process.env.NYC_CWD || process.cwd() |
| 13 | + var pkgPath = pkgUp.sync(cwd) |
| 14 | + |
| 15 | + if (pkgPath) { |
| 16 | + cwd = path.dirname(pkgPath) |
| 17 | + } |
| 18 | + |
| 19 | + var config = Config.buildYargs(cwd) |
| 20 | + .default({ |
| 21 | + cwd: cwd |
| 22 | + }) |
| 23 | + .parse(argv || []) |
| 24 | + |
| 25 | + // post-hoc, we convert several of the |
| 26 | + // configuration settings to arrays, providing |
| 27 | + // a consistent contract to index.js. |
| 28 | + config.require = arrify(config.require) |
| 29 | + config.extension = arrify(config.extension) |
| 30 | + config.exclude = arrify(config.exclude) |
| 31 | + config.include = arrify(config.include) |
| 32 | + config.cwd = cwd |
| 33 | + |
| 34 | + return config |
| 35 | +} |
| 36 | + |
| 37 | +// build a yargs object, omitting any settings |
| 38 | +// that would cause the application to exit early. |
| 39 | +Config.buildYargs = function (cwd) { |
| 40 | + return Yargs([]) |
| 41 | + .usage('$0 [command] [options]\n\nrun your tests with the nyc bin to instrument them with coverage') |
| 42 | + .command('report', 'run coverage report for .nyc_output', function (yargs) { |
| 43 | + return yargs |
| 44 | + .usage('$0 report [options]') |
| 45 | + .option('reporter', { |
| 46 | + alias: 'r', |
| 47 | + describe: 'coverage reporter(s) to use', |
| 48 | + default: 'text' |
| 49 | + }) |
| 50 | + .option('report-dir', { |
| 51 | + describe: 'directory to output coverage reports in', |
| 52 | + default: 'coverage' |
| 53 | + }) |
| 54 | + .option('temp-directory', { |
| 55 | + describe: 'directory from which coverage JSON files are read', |
| 56 | + default: './.nyc_output' |
| 57 | + }) |
| 58 | + .option('show-process-tree', { |
| 59 | + describe: 'display the tree of spawned processes', |
| 60 | + default: false, |
| 61 | + type: 'boolean' |
| 62 | + }) |
| 63 | + .example('$0 report --reporter=lcov', 'output an HTML lcov report to ./coverage') |
| 64 | + }) |
| 65 | + .command('check-coverage', 'check whether coverage is within thresholds provided', function (yargs) { |
| 66 | + return yargs |
| 67 | + .usage('$0 check-coverage [options]') |
| 68 | + .option('branches', { |
| 69 | + default: 0, |
| 70 | + description: 'what % of branches must be covered?' |
| 71 | + }) |
| 72 | + .option('functions', { |
| 73 | + default: 0, |
| 74 | + description: 'what % of functions must be covered?' |
| 75 | + }) |
| 76 | + .option('lines', { |
| 77 | + default: 90, |
| 78 | + description: 'what % of lines must be covered?' |
| 79 | + }) |
| 80 | + .option('statements', { |
| 81 | + default: 0, |
| 82 | + description: 'what % of statements must be covered?' |
| 83 | + }) |
| 84 | + .example('$0 check-coverage --lines 95', "check whether the JSON in nyc's output folder meets the thresholds provided") |
| 85 | + }) |
| 86 | + .option('reporter', { |
| 87 | + alias: 'r', |
| 88 | + describe: 'coverage reporter(s) to use', |
| 89 | + default: 'text' |
| 90 | + }) |
| 91 | + .option('report-dir', { |
| 92 | + describe: 'directory to output coverage reports in', |
| 93 | + default: 'coverage' |
| 94 | + }) |
| 95 | + .option('silent', { |
| 96 | + alias: 's', |
| 97 | + default: false, |
| 98 | + type: 'boolean', |
| 99 | + describe: "don't output a report after tests finish running" |
| 100 | + }) |
| 101 | + .option('all', { |
| 102 | + alias: 'a', |
| 103 | + default: false, |
| 104 | + type: 'boolean', |
| 105 | + describe: 'whether or not to instrument all files of the project (not just the ones touched by your test suite)' |
| 106 | + }) |
| 107 | + .option('exclude', { |
| 108 | + alias: 'x', |
| 109 | + default: testExclude.defaultExclude, |
| 110 | + describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported, node_modules is always excluded' |
| 111 | + }) |
| 112 | + .option('include', { |
| 113 | + alias: 'n', |
| 114 | + default: [], |
| 115 | + describe: 'a list of specific files that should be covered, glob patterns are supported' |
| 116 | + }) |
| 117 | + .option('require', { |
| 118 | + alias: 'i', |
| 119 | + default: [], |
| 120 | + describe: 'a list of additional modules that nyc should attempt to require in its subprocess, e.g., babel-register, babel-polyfill.' |
| 121 | + }) |
| 122 | + .option('cache', { |
| 123 | + alias: 'c', |
| 124 | + default: false, |
| 125 | + type: 'boolean', |
| 126 | + describe: 'cache instrumentation results for improved performance' |
| 127 | + }) |
| 128 | + .option('extension', { |
| 129 | + alias: 'e', |
| 130 | + default: [], |
| 131 | + describe: 'a list of extensions that nyc should handle in addition to .js' |
| 132 | + }) |
| 133 | + .option('check-coverage', { |
| 134 | + type: 'boolean', |
| 135 | + default: false, |
| 136 | + describe: 'check whether coverage is within thresholds provided' |
| 137 | + }) |
| 138 | + .option('branches', { |
| 139 | + default: 0, |
| 140 | + description: 'what % of branches must be covered?' |
| 141 | + }) |
| 142 | + .option('functions', { |
| 143 | + default: 0, |
| 144 | + description: 'what % of functions must be covered?' |
| 145 | + }) |
| 146 | + .option('lines', { |
| 147 | + default: 90, |
| 148 | + description: 'what % of lines must be covered?' |
| 149 | + }) |
| 150 | + .option('statements', { |
| 151 | + default: 0, |
| 152 | + description: 'what % of statements must be covered?' |
| 153 | + }) |
| 154 | + .option('source-map', { |
| 155 | + default: true, |
| 156 | + type: 'boolean', |
| 157 | + description: 'should nyc detect and handle source maps?' |
| 158 | + }) |
| 159 | + .option('instrument', { |
| 160 | + default: true, |
| 161 | + type: 'boolean', |
| 162 | + description: 'should nyc handle instrumentation?' |
| 163 | + }) |
| 164 | + .option('hook-run-in-context', { |
| 165 | + default: true, |
| 166 | + type: 'boolean', |
| 167 | + description: 'should nyc wrap vm.runInThisContext?' |
| 168 | + }) |
| 169 | + .option('show-process-tree', { |
| 170 | + describe: 'display the tree of spawned processes', |
| 171 | + default: false, |
| 172 | + type: 'boolean' |
| 173 | + }) |
| 174 | + .pkgConf('nyc', cwd || process.cwd()) |
| 175 | + .example('$0 npm test', 'instrument your tests with coverage') |
| 176 | + .example('$0 --require babel-core/register npm test', 'instrument your tests with coverage and babel') |
| 177 | + .example('$0 report --reporter=text-lcov', 'output lcov report after running your tests') |
| 178 | + .epilog('visit https://git.io/voHar for list of available reporters') |
| 179 | + .boolean('help') |
| 180 | + .boolean('h') |
| 181 | + .boolean('version') |
| 182 | +} |
| 183 | + |
| 184 | +// decorate yargs with all the actions |
| 185 | +// that would make it exit: help, version, command. |
| 186 | +Config.decorateYargs = function (yargs) { |
| 187 | + return yargs |
| 188 | + .help('h') |
| 189 | + .alias('h', 'help') |
| 190 | + .version() |
| 191 | + .command(require('../lib/commands/instrument')) |
| 192 | +} |
| 193 | + |
| 194 | +module.exports = Config |
0 commit comments