Skip to content

Commit

Permalink
Use find-cache-dir to determine cache directory.
Browse files Browse the repository at this point in the history
Uses `find-cache-dir` for consistent cache locating with AVA, etc. It takes the extra step of ensuring a `package.json` file exists (and hunting up the directory tree for it), otherwise it returns null. If it can't find a suitable cache directory, caching is disabled.

I also dropped the `cacheDirectory()` method in favor of just setting a property.
  • Loading branch information
jamestalmage committed Jan 1, 2016
1 parent 706e2ed commit 718a319
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
19 changes: 11 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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')

Expand All @@ -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)

Expand All @@ -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'
})
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 = {})

Expand Down Expand Up @@ -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]))
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 718a319

Please sign in to comment.