@@ -6,11 +6,13 @@ 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' )
9
10
10
11
var engines = { }
11
12
var rules = { }
12
13
var cache = null
13
14
var cachePath = null
15
+ var cacheFallback = path . join ( os . tmpdir ( ) , 'eslint-loader' , 'cache.json' )
14
16
15
17
/**
16
18
* linter
@@ -59,13 +61,23 @@ function lint(input, config, webpack) {
59
61
res = engine . executeOnText ( input , resourcePath , true )
60
62
61
63
// Save new results in the cache
62
- if ( config . cache ) {
64
+ if ( config . cache && cachePath ) {
63
65
cache [ resourcePath ] = {
64
66
hash : inputMD5 ,
65
67
rules : rulesHash ,
66
68
res : res ,
67
69
}
68
- fs . writeFileSync ( cachePath , JSON . stringify ( cache ) )
70
+ var cacheJson = JSON . stringify ( cacheJson )
71
+ try {
72
+ safeWriteCache ( cachePath )
73
+ } catch ( e ) {
74
+ try {
75
+ cachePath = cacheFallback
76
+ safeWriteCache ( cachePath )
77
+ } catch ( e ) {
78
+ cache = false
79
+ }
80
+ }
69
81
}
70
82
}
71
83
@@ -181,7 +193,7 @@ module.exports = function(input, map) {
181
193
thunk : true ,
182
194
create : true ,
183
195
} )
184
- cachePath = thunk ( "data.json" ) || os . tmpdir ( ) + "/data.json"
196
+ cachePath = thunk ( "data.json" ) || cacheFallback
185
197
try {
186
198
cache = require ( cachePath )
187
199
}
@@ -197,3 +209,13 @@ module.exports = function(input, map) {
197
209
lint ( input , config , this )
198
210
this . callback ( null , input , map )
199
211
}
212
+
213
+
214
+ function safeWriteCache ( cacheJson ) {
215
+ if ( fs . existsSync ( cachePath ) ) {
216
+ fs . writeFileSync ( cachePath , cacheJson )
217
+ } else {
218
+ fs . mkdirSync ( path . dirname ( cachePath ) )
219
+ fs . writeFileSync ( cachePath , cacheJson )
220
+ }
221
+ }
0 commit comments