Skip to content

Commit 181117b

Browse files
committed
Merge pull request #98 from jamestalmage/fix-npm-link
Fix ability to self-test using `npm link`.
2 parents 8e78f50 + aa03d7c commit 181117b

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

index.js

+18-26
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var rimraf = require('rimraf')
1010
var onExit = require('signal-exit')
1111
var stripBom = require('strip-bom')
1212
var SourceMapCache = require('./lib/source-map-cache')
13+
var resolveFrom = require('resolve-from')
1314

1415
/* istanbul ignore next */
1516
if (/index\.covered\.js$/.test(__filename)) {
@@ -54,14 +55,15 @@ function NYC (opts) {
5455
NYC.prototype._loadAdditionalModules = function () {
5556
var _this = this
5657
this.require.forEach(function (r) {
57-
try {
58-
// first attempt to require the module relative to
59-
// the directory being instrumented.
60-
require(path.resolve(_this.cwd, './node_modules', r))
61-
} catch (e) {
62-
// now try other locations, .e.g, the nyc node_modules folder.
63-
require(r)
58+
// first attempt to require the module relative to
59+
// the directory being instrumented.
60+
var p = resolveFrom(_this.cwd, r)
61+
if (p) {
62+
require(p)
63+
return
6464
}
65+
// now try other locations, .e.g, the nyc node_modules folder.
66+
require(r)
6567
})
6668
}
6769

@@ -114,21 +116,6 @@ NYC.prototype.addFile = function (filename, returnImmediately) {
114116
}
115117
}
116118

117-
NYC.prototype.addContent = function (filename, content) {
118-
var relFile = path.relative(this.cwd, filename)
119-
var instrument = this.shouldInstrumentFile(filename, relFile)
120-
121-
if (instrument) {
122-
content = this.instrumenter.instrumentSync(content, './' + relFile)
123-
}
124-
125-
return {
126-
instrument: instrument,
127-
content: content,
128-
relFile: relFile
129-
}
130-
}
131-
132119
NYC.prototype.shouldInstrumentFile = function (filename, relFile) {
133120
relFile = relFile.replace(/^\.\//, '') // remove leading './'.
134121

@@ -156,13 +143,18 @@ NYC.prototype.addAllFiles = function () {
156143

157144
NYC.prototype._wrapRequire = function () {
158145
var _this = this
146+
appendTransform(function (code, filename) {
147+
var relFile = path.relative(_this.cwd, filename)
148+
var instrument = _this.shouldInstrumentFile(filename, relFile)
149+
150+
if (!instrument) {
151+
return code
152+
}
159153

160-
appendTransform(function (compiledSrc, filename) {
161-
_this.sourceMapCache.add(filename, compiledSrc)
154+
_this.sourceMapCache.add(filename, code)
162155

163156
// now instrument the compiled code.
164-
var obj = _this.addContent(filename, compiledSrc)
165-
return obj.content
157+
return _this.instrumenter.instrumentSync(code, './' + relFile)
166158
})
167159
}
168160

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"lodash": "^3.10.0",
7272
"micromatch": "~2.1.6",
7373
"mkdirp": "^0.5.0",
74+
"resolve-from": "^2.0.0",
7475
"rimraf": "^2.4.2",
7576
"signal-exit": "^2.1.1",
7677
"source-map": "^0.5.3",

0 commit comments

Comments
 (0)