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

Commit

Permalink
add check before writing file
Browse files Browse the repository at this point in the history
  • Loading branch information
viankakrisna committed Feb 27, 2017
1 parent d7f003d commit 4636c39
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
50 changes: 36 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ var fs = require("fs")
var findCacheDir = require("find-cache-dir")
var objectHash = require("object-hash")
var os = require("os")
var path = require("path")
var mkdirp = require("mkdirp")

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

/**
* linter
Expand Down Expand Up @@ -65,7 +66,9 @@ function lint(input, config, webpack) {
rules: rulesHash,
res: res,
}
fs.writeFileSync(cachePath, JSON.stringify(cache))
var cacheJson = JSON.stringify(cache)
var cachePath = getCachePath()
writeCache(cachePath, cacheJson)
}
}

Expand Down Expand Up @@ -176,18 +179,7 @@ module.exports = function(input, map) {
// Read the cached information only once and if enable
if (cache === null) {
if (config.cache) {
var thunk = findCacheDir({
name: "eslint-loader",
thunk: true,
create: true,
})
cachePath = thunk("data.json") || os.tmpdir() + "/data.json"
try {
cache = require(cachePath)
}
catch (e) {
cache = {}
}
cache = readCache(getCachePath())
}
else {
cache = false
Expand All @@ -197,3 +189,33 @@ module.exports = function(input, map) {
lint(input, config, this)
this.callback(null, input, map)
}

function writeCache(cachePath, cacheJson) {
mkdirp.sync(path.dirname(cachePath))
fs.writeFileSync(cachePath, cacheJson)
}

function readCache(cachePath) {
try {
return require(cachePath)
}
catch (e) {
return {}
}
}

function getCachePath() {
var cachePath
try {
var thunk = findCacheDir({
name: "eslint-loader",
thunk: true,
create: true,
})
cachePath = thunk("data.json")
}
catch (e) {
cachePath = path.join(os.tmpdir(), "eslint-loader", "cache.json")
}
return cachePath
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"find-cache-dir": "^0.1.1",
"loader-utils": "^1.0.2",
"mkdirp": "^0.5.1",
"object-assign": "^4.0.1",
"object-hash": "^1.1.4"
},
Expand Down

0 comments on commit 4636c39

Please sign in to comment.