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