forked from istanbuljs/nyc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·161 lines (130 loc) · 4.15 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* global __coverage__ */
var _ = require('lodash')
var fs = require('fs')
var mkdirp = require('mkdirp')
var path = require('path')
var rimraf = require('rimraf')
var onExit = require('signal-exit')
var stripBom = require('strip-bom')
function NYC (opts) {
_.extend(this, {
subprocessBin: path.resolve(
__dirname,
'./bin/nyc.js'
),
tempDirectory: './.nyc_output',
cwd: process.env.NYC_CWD || process.cwd(),
reporter: 'text',
istanbul: require('istanbul')
}, opts)
if (!Array.isArray(this.reporter)) this.reporter = [this.reporter]
var config = require(path.resolve(this.cwd, './package.json')).config || {}
config = config.nyc || {}
this.exclude = config.exclude || ['node_modules\/', 'test\/', 'test\\.js']
if (!Array.isArray(this.exclude)) this.exclude = [this.exclude]
this.exclude = _.map(this.exclude, function (p) {
return new RegExp(p)
})
this.instrumenter = this._createInstrumenter()
mkdirp.sync(this.tmpDirectory())
}
NYC.prototype._createInstrumenter = function () {
var configFile = path.resolve(this.cwd, './.istanbul.yml')
if (!fs.existsSync(configFile)) configFile = undefined
var instrumenterConfig = this.istanbul.config.loadFile(configFile).instrumentation.config
return new this.istanbul.Instrumenter({
coverageVariable: '__coverage__',
embedSource: instrumenterConfig['embed-source'],
noCompact: !instrumenterConfig.compact,
preserveComments: instrumenterConfig['preserve-comments']
})
}
NYC.prototype.cleanup = function () {
if (!process.env.NYC_CWD) rimraf.sync(this.tmpDirectory())
}
NYC.prototype._wrapRequire = function () {
var _this = this
// any JS you require should get coverage added.
require.extensions['.js'] = function (module, filename) {
var instrument = true
var content = fs.readFileSync(filename, 'utf8')
// only instrument a file if it's not on the exclude list.
var relFile = path.relative(_this.cwd, filename)
for (var i = 0, exclude; (exclude = _this.exclude[i]) !== undefined; i++) {
if (exclude.test(relFile)) instrument = false
}
if (instrument) {
content = _this.instrumenter.instrumentSync(
content,
'./' + relFile
)
}
module._compile(stripBom(content), filename)
}
}
NYC.prototype._wrapExit = function () {
var _this = this
var outputCoverage = function () {
var coverage = global.__coverage__
if (typeof __coverage__ === 'object') coverage = __coverage__
if (!coverage) return
fs.writeFileSync(
path.resolve(_this.tmpDirectory(), './', process.pid + '.json'),
JSON.stringify(coverage),
'utf-8'
)
}
// we always want to write coverage
// regardless of how the process exits.
onExit(function () {
outputCoverage()
}, {alwaysLast: true})
}
NYC.prototype.wrap = function (bin) {
this._wrapRequire()
this._wrapExit()
return this
}
NYC.prototype.report = function (cb, _collector, _reporter) {
cb = cb || function () {}
var collector = _collector || new this.istanbul.Collector()
var reporter = _reporter || new this.istanbul.Reporter()
this._loadReports().forEach(function (report) {
collector.add(report)
})
this.reporter.forEach(function (_reporter) {
reporter.add(_reporter)
})
reporter.write(collector, true, cb)
}
NYC.prototype._loadReports = function () {
var _this = this
var files = fs.readdirSync(this.tmpDirectory())
return _.map(files, function (f) {
try {
return JSON.parse(fs.readFileSync(
path.resolve(_this.tmpDirectory(), './', f),
'utf-8'
))
} catch (e) { // handle corrupt JSON output.
return {}
}
})
}
NYC.prototype.tmpDirectory = function () {
return path.resolve(this.cwd, './', this.tempDirectory)
}
NYC.prototype.mungeArgs = function (yargv) {
var argv = process.argv.slice(1)
argv = argv.slice(argv.indexOf(yargv._[0]))
if (!/^(node|iojs)$/.test(argv[0]) &&
process.platform === 'win32' &&
(/\.js$/.test(argv[0]) ||
(!/\.(cmd|exe)$/.test(argv[0]) &&
!fs.existsSync(argv[0] + '.cmd') &&
!fs.existsSync(argv[0] + '.exe')))) {
argv.unshift(process.execPath)
}
return argv
}
module.exports = NYC