diff --git a/index.js b/index.js index 19151be99..c6fe9c5fd 100755 --- a/index.js +++ b/index.js @@ -11,14 +11,8 @@ 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)) { @@ -26,39 +20,29 @@ if (/index\.covered\.js$/.test(__filename)) { } 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() } @@ -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 } }) @@ -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 () { @@ -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) { diff --git a/package.json b/package.json index 662c9aeee..9fcb1a37f 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",