@@ -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,25 @@ 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 ( cache )
71
+ try {
72
+ safeWriteCache ( cachePath , cacheJson )
73
+ }
74
+ catch ( e ) {
75
+ try {
76
+ cachePath = cacheFallback
77
+ safeWriteCache ( cachePath , cacheJson )
78
+ }
79
+ catch ( e ) {
80
+ cache = false
81
+ }
82
+ }
69
83
}
70
84
}
71
85
@@ -176,13 +190,13 @@ module.exports = function(input, map) {
176
190
// Read the cached information only once and if enable
177
191
if ( cache === null ) {
178
192
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
193
try {
194
+ var thunk = findCacheDir ( {
195
+ name : "eslint-loader" ,
196
+ thunk : true ,
197
+ create : true ,
198
+ } )
199
+ cachePath = thunk ( "data.json" )
186
200
cache = require ( cachePath )
187
201
}
188
202
catch ( e ) {
@@ -197,3 +211,13 @@ module.exports = function(input, map) {
197
211
lint ( input , config , this )
198
212
this . callback ( null , input , map )
199
213
}
214
+
215
+ function safeWriteCache ( cachePath , cacheJson ) {
216
+ if ( fs . existsSync ( cachePath ) ) {
217
+ fs . writeFileSync ( cachePath , cacheJson )
218
+ }
219
+ else {
220
+ fs . mkdirSync ( path . dirname ( cachePath ) )
221
+ fs . writeFileSync ( cachePath , cacheJson )
222
+ }
223
+ }
0 commit comments