Debian Codegraph @

nyc/bin/nyc.js

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/env node
2 'use strict'
3
4 const configUtil = require('../lib/config-util')
5 const { cliWrapper, suppressEPIPE } = require('../lib/commands/helpers')
•6 const { foregroundChild } = require('foreground-child')
7 const resolveFrom = require('resolve-from')
8 const NYC = require('../index.js')
9
10 // parse configuration and command-line arguments;
11 // we keep these values in a few different forms,
12 // used in the various execution contexts of nyc:
13 // reporting, instrumenting subprocesses, etc.
14
15 async function main () {
16 const { argv, childArgs, yargs } = await configUtil()
17
18 if (['check-coverage', 'report', 'instrument', 'merge'].includes(argv._[0])) {
19 // look in lib/commands for logic.
20 return
21 }
22
23 if (argv._.length === 0) {
24 // I don't have a clue what you're doing.
25 process.exitCode = 1
26 yargs.showHelp()
27 return
28 }
29
30 // if instrument is set to false,
31 // enable a noop instrumenter.
32 if (!argv.instrument) argv.instrumenter = './lib/instrumenters/noop'
33 else argv.instrumenter = './lib/instrumenters/istanbul'
34
35 var nyc = (new NYC(argv))
36 if (argv.clean) {
37 await nyc.reset()
38 } else {
39 await nyc.createTempDirectory()
40 }
41
42 const env = {
43 NYC_CONFIG: JSON.stringify(argv),
44 NYC_CWD: process.cwd()
45 }
46
47 /* istanbul ignore else */
48 if (argv['babel-cache'] === false) {
49 // babel's cache interferes with some configurations, so is
50 // disabled by default. opt in by setting babel-cache=true.
51 env.BABEL_DISABLE_CACHE = process.env.BABEL_DISABLE_CACHE = '1'
52 }
53
54 if (!argv.useSpawnWrap) {
55 const requireModules = [
56 require.resolve('../lib/register-env.js'),
57 ...nyc.require.map(mod => resolveFrom.silent(nyc.cwd, mod) || mod)
58 ]
59 const preloadList = require('node-preload')
60 preloadList.push(
61 ...requireModules,
62 require.resolve('../lib/wrap.js')
63 )
64
65 Object.assign(process.env, env)
66 requireModules.forEach(mod => {
67 require(mod)
68 })
69 }
70
71 if (argv.all) {
72 await nyc.addAllFiles()
73 }
74
75 if (argv.useSpawnWrap) {
76 const wrapper = require.resolve('./wrap.js')
77 // Support running nyc as a user without HOME (e.g. linux 'nobody'),
78 // https://github.com/istanbuljs/nyc/issues/951
79 env.SPAWN_WRAP_SHIM_ROOT = process.env.SPAWN_WRAP_SHIM_ROOT || process.env.XDG_CACHE_HOME || require('os').homedir()
80 const sw = require('spawn-wrap')
81
82 sw([wrapper], env)
83 }
84
85 // Both running the test script invocation and the check-coverage run may
86 // set process.exitCode. Keep track so that both children are run, but
87 // a non-zero exit codes in either one leads to an overall non-zero exit code.
88 process.exitCode = 0
•89 foregroundChild(childArgs, async () => {
90 const mainChildExitCode = process.exitCode
91
92 try {
93 await nyc.writeProcessIndex()
94
95 nyc.maybePurgeSourceMapCache()
96 if (argv.checkCoverage) {
97 await nyc.checkCoverage({
98 lines: argv.lines,
99 functions: argv.functions,
100 branches: argv.branches,
101 statements: argv.statements
102 }, argv['per-file']).catch(suppressEPIPE)
103 process.exitCode = process.exitCode || mainChildExitCode
104 }
105
106 if (!argv.silent) {
107 await nyc.report().catch(suppressEPIPE)
108 }
109 } catch (error) {
110 /* istanbul ignore next */
111 process.exitCode = process.exitCode || mainChildExitCode || 1
112 /* istanbul ignore next */
113 console.error(error.message)
114 }
115 })
116 }
117
118 cliWrapper(main)()