@@ -6,11 +6,12 @@ var fs = require("fs")
6
6
var findCacheDir = require ( "find-cache-dir" )
7
7
var objectHash = require ( "object-hash" )
8
8
var os = require ( "os" )
9
+ var path = require ( "path" )
10
+ var mkdirp = require ( "mkdirp" )
9
11
10
12
var engines = { }
11
13
var rules = { }
12
14
var cache = null
13
- var cachePath = null
14
15
15
16
/**
16
17
* linter
@@ -65,7 +66,9 @@ function lint(input, config, webpack) {
65
66
rules : rulesHash ,
66
67
res : res ,
67
68
}
68
- fs . writeFileSync ( cachePath , JSON . stringify ( cache ) )
69
+ var cacheJson = JSON . stringify ( cache )
70
+ var cachePath = getCachePath ( )
71
+ writeCache ( cachePath , cacheJson )
69
72
}
70
73
}
71
74
@@ -176,24 +179,40 @@ module.exports = function(input, map) {
176
179
// Read the cached information only once and if enable
177
180
if ( cache === null ) {
178
181
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
182
+ cache = readCache ( getCachePath ( ) )
194
183
}
195
184
}
196
185
197
186
lint ( input , config , this )
198
187
this . callback ( null , input , map )
199
188
}
189
+
190
+ function writeCache ( cachePath , cacheJson ) {
191
+ mkdirp . sync ( path . dirname ( cachePath ) )
192
+ fs . writeFileSync ( cachePath , cacheJson )
193
+ }
194
+
195
+ function readCache ( cachePath ) {
196
+ try {
197
+ return require ( cachePath )
198
+ }
199
+ catch ( e ) {
200
+ return { }
201
+ }
202
+ }
203
+
204
+ function getCachePath ( ) {
205
+ var cachePath
206
+ try {
207
+ var thunk = findCacheDir ( {
208
+ name : "eslint-loader" ,
209
+ thunk : true ,
210
+ create : true ,
211
+ } )
212
+ cachePath = thunk ( "data.json" )
213
+ }
214
+ catch ( e ) {
215
+ cachePath = path . join ( os . tmpdir ( ) , "eslint-loader" , "cache.json" )
216
+ }
217
+ return cachePath
218
+ }
0 commit comments