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

Commit 6ab09b5

Browse files
committed
add checks for writing and reading cache
1 parent d7f003d commit 6ab09b5

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

index.js

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ var fs = require("fs")
66
var findCacheDir = require("find-cache-dir")
77
var objectHash = require("object-hash")
88
var os = require("os")
9+
var path = require("path")
10+
var mkdirp = require("mkdirp")
911

1012
var engines = {}
1113
var rules = {}
1214
var cache = null
13-
var cachePath = null
1415

1516
/**
1617
* linter
@@ -65,7 +66,8 @@ function lint(input, config, webpack) {
6566
rules: rulesHash,
6667
res: res,
6768
}
68-
fs.writeFileSync(cachePath, JSON.stringify(cache))
69+
writeCache(cache)
70+
// let this function handle the writing of the cache
6971
}
7072
}
7173

@@ -176,24 +178,54 @@ module.exports = function(input, map) {
176178
// Read the cached information only once and if enable
177179
if (cache === null) {
178180
if (config.cache) {
179-
var thunk = findCacheDir({
180-
name: "eslint-loader",
181-
thunk: true,
182-
create: true,
183-
})
184-
cachePath = thunk("data.json") || os.tmpdir() + "/data.json"
185-
try {
186-
cache = require(cachePath)
187-
}
188-
catch (e) {
189-
cache = {}
190-
}
191-
}
192-
else {
193-
cache = false
181+
cache = readCache()
194182
}
195183
}
196184

197185
lint(input, config, this)
198186
this.callback(null, input, map)
199187
}
188+
189+
function writeCache(cache) {
190+
var cachePath = getCachePath()
191+
// here we should already get the safe path to write
192+
try { // just in case
193+
mkdirp.sync(path.dirname(cachePath)) // Create folders if not exists
194+
fs.writeFileSync(cachePath, JSON.stringify(cache)) // Write it now
195+
}
196+
catch (e) {
197+
// Maybe permission denied?
198+
// Don't log errors, try it again in the next lint...
199+
}
200+
}
201+
202+
function readCache() {
203+
try {
204+
return require(getCachePath())
205+
} // if we cannot read the safe path, just return {}
206+
catch (e) {
207+
return {}
208+
}
209+
}
210+
211+
function getCachePath() {
212+
var cachePath
213+
try {
214+
// findCacheDir could throw an error
215+
var thunk = findCacheDir({
216+
name: "eslint-loader",
217+
thunk: true,
218+
create: true,
219+
})
220+
cachePath = thunk("data.json")
221+
}
222+
catch (e) {
223+
// Just check if cachePath is truthy for fallbacks, findCacheDir
224+
// thunks could return a null value
225+
}
226+
if (!cachePath) {
227+
cachePath = path.join(os.tmpdir(), "eslint-loader", "cache.json")
228+
// tmpdir should be safe enough to write
229+
}
230+
return cachePath
231+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"dependencies": {
2222
"find-cache-dir": "^0.1.1",
2323
"loader-utils": "^1.0.2",
24+
"mkdirp": "^0.5.1",
2425
"object-assign": "^4.0.1",
2526
"object-hash": "^1.1.4"
2627
},

0 commit comments

Comments
 (0)