[react-packager] Recover and warn from corrupted cache file

This commit is contained in:
Amjad Masad 2015-03-03 17:22:21 -08:00
parent 37cab152b5
commit 41743e5987
1 changed files with 13 additions and 3 deletions

View File

@ -112,13 +112,23 @@ Cache.prototype._persistCache = function() {
return this._persisting; return this._persisting;
}; };
function loadCacheSync(cacheFilepath) { function loadCacheSync(cachePath) {
var ret = Object.create(null); var ret = Object.create(null);
if (!fs.existsSync(cacheFilepath)) { if (!fs.existsSync(cachePath)) {
return ret; return ret;
} }
var cacheOnDisk = JSON.parse(fs.readFileSync(cacheFilepath)); var cacheOnDisk;
try {
cacheOnDisk = JSON.parse(fs.readFileSync(cachePath));
} catch (e) {
if (e instanceof SyntaxError) {
console.warn('Unable to parse cache file. Will clear and continue.');
fs.unlinkSync(cachePath);
return ret;
}
throw e;
}
// Filter outdated cache and convert to promises. // Filter outdated cache and convert to promises.
Object.keys(cacheOnDisk).forEach(function(key) { Object.keys(cacheOnDisk).forEach(function(key) {