Debian Codegraph @

nyc/lib/commands/helpers.js

raw
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 /* istanbul ignore next: unsure how to test this */
54 suppressEPIPE (error) {
55 /* Prevent dumping error when `nyc npm t|head` causes stdout to
56 * be closed when reporting runs. */
57 if (error.code !== 'EPIPE') {
58 throw error
59 }
60 },
61 cliWrapper (execute) {
62 return argv => {
63 execute(argv).catch(error => {
64 try {
65 console.error(error.message)
66 } catch (_) {
67 /* We need to run process.exit(1) even if stderr is destroyed */
68 }
69
70 process.exit(1)
71 })
72 }
73 }
74 }