Skip to content

Commit

Permalink
rework options processing
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Dec 18, 2015
1 parent ed1a9ee commit 84fce89
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 39 deletions.
60 changes: 22 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,38 @@ var onExit = require('signal-exit')
var stripBom = require('strip-bom')
var SourceMapCache = require('./lib/source-map-cache')
var resolveFrom = require('resolve-from')
var crypto = require('crypto')

function md5 (str) {
return crypto
.createHash('md5')
.update(str, 'utf8')
.digest('hex')
}
var md5 = require('md5-hex')
var arrify = require('arrify')

/* istanbul ignore next */
if (/index\.covered\.js$/.test(__filename)) {
require('./lib/self-coverage-helper')
}

function NYC (opts) {
if (opts && opts.istanbul) {
opts._istanbul = opts.istanbul
delete opts.istanbul
}
_.extend(this, {
subprocessBin: path.resolve(
__dirname,
'./bin/nyc.js'
),
tempDirectory: './.nyc_output',
cacheDirectory: './.nyc_cache',
cwd: process.env.NYC_CWD || process.cwd(),
reporter: 'text',
sourceMapCache: new SourceMapCache(),
require: []
}, opts)

if (!Array.isArray(this.reporter)) this.reporter = [this.reporter]
opts = opts || {}

this._istanbul = opts.istanbul
this.subprocessBin = opts.subprocessBin || path.resolve(__dirname, './bin/nyc.js')
this._tempDirectory = opts.tempDirectory || './.nyc_output'
this._cacheDirectory = opts.cacheDirectory || './.nyc_cache'
this.cwd = opts.cwd || process.env.NYC_CWD || process.cwd()
this.reporter = arrify(opts.reporter || 'text')
this.sourceMapCache = opts.sourceMapCache || new SourceMapCache()

// you can specify config in the nyc stanza of package.json.
var config = require(path.resolve(this.cwd, './package.json')).config || {}
config = config.nyc || {}

// load exclude stanza from config.
this.include = config.include || ['**']
this.include = arrify(config.include || ['**'])
this.include = this._prepGlobPatterns(this.include)

this.exclude = ['**/node_modules/**'].concat(config.exclude || ['test/**', 'test{,-*}.js'])
if (!Array.isArray(this.exclude)) this.exclude = [this.exclude]
this.exclude = ['**/node_modules/**'].concat(arrify(config.exclude || ['test/**', 'test{,-*}.js']))
this.exclude = this._prepGlobPatterns(this.exclude)

// require extensions can be provided as config in package.json.
this.require = config.require ? config.require : this.require
this.require = arrify(config.require || opts.require)

this._createOutputDirectory()
}
Expand Down Expand Up @@ -169,13 +153,13 @@ NYC.prototype._wrapRequire = function () {
_this.sourceMapCache.add(filename, code)

var hash = md5(code)
var cacheFilePath = path.join(_this._cacheDirectory(), hash + '.js')
var cacheFilePath = path.join(_this.cacheDirectory(), hash + '.js')

try {
return fs.readFileSync(cacheFilePath, 'utf8')
} catch (e) {
var instrumented = _this.instrumenter().instrumentSync(code, './' + relFile)
fs.writeFile(cacheFilePath, instrumented)
fs.writeFileSync(cacheFilePath, instrumented)
return instrumented
}
})
Expand All @@ -186,8 +170,8 @@ NYC.prototype.cleanup = function () {
}

NYC.prototype._createOutputDirectory = function () {
mkdirp.sync(this.tmpDirectory())
mkdirp.sync(this._cacheDirectory())
mkdirp.sync(this.tempDirectory())
mkdirp.sync(this.cacheDirectory())
}

NYC.prototype._wrapExit = function () {
Expand Down Expand Up @@ -256,12 +240,12 @@ NYC.prototype._loadReports = function () {
})
}

NYC.prototype.tmpDirectory = function () {
return path.resolve(this.cwd, './', this.tempDirectory)
NYC.prototype.tmpDirectory = NYC.prototype.tempDirectory = function () {
return path.resolve(this.cwd, './', this._tempDirectory)
}

NYC.prototype._cacheDirectory = function () {
return path.resolve(this.cwd, './', this.cacheDirectory)
NYC.prototype.cacheDirectory = function () {
return path.resolve(this.cwd, './', this._cacheDirectory)
}

NYC.prototype.mungeArgs = function (yargv) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"pretest": "standard",
"test": "npm run cover",
"clean": "rm -rf ./.nyc_output ./.self_coverage ./test/fixtures/.nyc_output ./test/build && rm -f *covered.js ./lib/*covered.js",
"clean": "rm -rf ./.nyc_output ./.nyc_cache ./.self_coverage ./test/fixtures/.nyc_output ./test/fixtures/.nyc_cache ./test/build && rm -f *covered.js ./lib/*covered.js",
"build": "node ./build-tests",
"instrument": "node ./build-self-coverage.js",
"run-tests": "tap --no-cov -b ./test/build/*.js ./test/src/source-map-cache.js",
Expand Down Expand Up @@ -64,11 +64,13 @@
"license": "ISC",
"dependencies": {
"append-transform": "^0.2.0",
"arrify": "^1.0.1",
"convert-source-map": "^1.1.2",
"foreground-child": "^1.3.0",
"glob": "^5.0.14",
"istanbul": "^0.4.1",
"lodash": "^3.10.0",
"md5-hex": "^1.1.0",
"micromatch": "~2.1.6",
"mkdirp": "^0.5.0",
"resolve-from": "^2.0.0",
Expand Down

0 comments on commit 84fce89

Please sign in to comment.