diff --git a/index.js b/index.js index af290c26b..b5a487b47 100755 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ var arrify = require('arrify') var SourceMapCache = require('./lib/source-map-cache') var convertSourceMap = require('convert-source-map') var md5hex = require('md5-hex') +var findCacheDir = require('find-cache-dir') /* istanbul ignore next */ if (/index\.covered\.js$/.test(__filename)) { @@ -26,7 +27,6 @@ function NYC (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 || './node_modules/.cache/nyc' this.cwd = opts.cwd || process.env.NYC_CWD || process.cwd() this.reporter = arrify(opts.reporter || 'text') @@ -46,6 +46,11 @@ function NYC (opts) { this.enableCache = opts.enableCache === true || process.env.NYC_CACHE === 'enable' + if (this.enableCache) { + this.cacheDirectory = findCacheDir({name: 'nyc', cwd: this.cwd}) + this.enableCache = Boolean(this.cacheDirectory) + } + // require extensions can be provided as config in package.json. this.require = arrify(config.require || opts.require) @@ -70,7 +75,7 @@ NYC.prototype._createTransform = function () { return hash }, factory: this._transformFactory.bind(this), - cacheDir: this.cacheDirectory(), + cacheDir: this.cacheDirectory, disableCache: !this.enableCache, ext: '.js' }) @@ -215,7 +220,9 @@ NYC.prototype.cleanup = function () { } NYC.prototype.clearCache = function () { - rimraf.sync(this.cacheDirectory()) + if (this.enableCache) { + rimraf.sync(this.cacheDirectory) + } } NYC.prototype.createTempDirectory = function () { @@ -292,7 +299,7 @@ NYC.prototype._loadReports = function () { var _this = this var files = fs.readdirSync(this.tempDirectory()) - var cacheDir = _this.cacheDirectory() + var cacheDir = _this.cacheDirectory var loadedMaps = this.loadedMaps || (this.loadedMaps = {}) @@ -334,10 +341,6 @@ NYC.prototype.tempDirectory = function () { return path.resolve(this.cwd, './', this._tempDirectory) } -NYC.prototype.cacheDirectory = function () { - return path.resolve(this.cwd, './', this._cacheDirectory) -} - NYC.prototype.mungeArgs = function (yargv) { var argv = process.argv.slice(1) argv = argv.slice(argv.indexOf(yargv._[0])) diff --git a/package.json b/package.json index 55e0cd4b9..527b60b9d 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "arrify": "^1.0.1", "caching-transform": "^1.0.0", "convert-source-map": "^1.1.2", + "find-cache-dir": "^0.1.1", "foreground-child": "^1.3.0", "glob": "^6.0.2", "istanbul": "^0.4.1",