Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

add checks before writing and reading cache #158

Closed
wants to merge 4 commits into from

Conversation

viankakrisna
Copy link
Contributor

index.js Outdated
fs.writeFileSync(cachePath, JSON.stringify(cache))
var cacheJson = JSON.stringify(cacheJson)
try {
safeWriteCache(cachePath)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function definition says it accepts cacheJson but you're passing cachePath.
This is a bit confusing—it's hard to tell what is local and what is global, and why.
Can you avoid shared mutable state wherever possible?

index.js Outdated
@@ -181,7 +193,7 @@ module.exports = function(input, map) {
thunk: true,
create: true,
})
cachePath = thunk("data.json") || os.tmpdir() + "/data.json"
cachePath = thunk("data.json") || cacheFallback
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both this line and the previous line (where you declare thunk) can throw.
You need to move them inside a try/catch and apply the same strategy of fallback.

Right now || cacheFallback is not doing anything because if something's bad, the function will throw rather than return null.

index.js Outdated
safeWriteCache(cacheJson)
}
catch (e) {
cache = false
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using false here? Does it have a special meaning compared to null elsewhere?

index.js Outdated
thunk: true,
create: true,
})
cachePath = thunk("data.json")
cache = require(cachePath)
}
catch (e) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume that we want to read from fallback below? Maybe extract it to safeReadCache?

index.js Outdated
@@ -6,11 +6,13 @@ var fs = require("fs")
var findCacheDir = require("find-cache-dir")
var objectHash = require("object-hash")
var os = require("os")
var path = require("path")

var engines = {}
var rules = {}
var cache = null
var cachePath = null
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be a global variable at all?

index.js Outdated
fs.writeFileSync(cachePath, cacheJson)
}
else {
fs.mkdirSync(path.dirname(cachePath))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is odd that we're using two different ways to create a directory: mkdirSync and findCacheDir (which internally uses mkdirp).

Can you use mkdirp here too? You also don't need these existsSync checks because mkdirp already handles all of that.

@viankakrisna viankakrisna changed the title add check before writing file add check before writing and reading cache Feb 27, 2017
@viankakrisna viankakrisna changed the title add check before writing and reading cache add checks before writing file Feb 27, 2017
@viankakrisna viankakrisna changed the title add checks before writing file add checks before writing and reading cache Feb 27, 2017
So the behavior would be similar to findCacheDir
index.js Outdated
}
catch (e) {
// Maybe permission denied?
// Don't log errors, try it again in the next lint...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if it always fail, people will never have the cache and no message?

@gaearon
Copy link

gaearon commented Feb 27, 2017

I think we should use the Babel version, it already handles edge cases well and IMO is written more consistently

@MoOx
Copy link
Contributor

MoOx commented Feb 28, 2017

So we should close in favor of #159?

@MoOx
Copy link
Contributor

MoOx commented Feb 28, 2017

Or maybe you want this merged/released asap?

@gaearon
Copy link

gaearon commented Feb 28, 2017

Let's do #159 instead.

@MoOx MoOx closed this Feb 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants