Skip to content

Commit

Permalink
Merge pull request #9 from ivan/master
Browse files Browse the repository at this point in the history
Re-resolve path if require fails; don't break on corrupt JSON
  • Loading branch information
bahmutov committed May 2, 2016
2 parents cf0324e + 8675417 commit a06004f
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ var SAVE_FILENAME =
process.env.CACHE_REQUIRE_PATHS_FILE ?
process.env.CACHE_REQUIRE_PATHS_FILE :
'./.cache-require-paths.json';
var nameCache = exists(SAVE_FILENAME) ? JSON.parse(fs.readFileSync(SAVE_FILENAME, 'utf-8')) : {};
var nameCache;
try {
nameCache = exists(SAVE_FILENAME) ? JSON.parse(fs.readFileSync(SAVE_FILENAME, 'utf-8')) : {};
} catch (err) {
nameCache = {};
}

var currentModuleCache;
var pathToLoad;
Expand All @@ -30,7 +35,14 @@ Module.prototype.require = function cachePathsRequire(name) {
currentModuleCache[name] = pathToLoad;
}

return _require.call(this, pathToLoad);
try {
return _require.call(this, pathToLoad);
} catch (err) {
// Cache may be outdated; resolve and try again
pathToLoad = Module._resolveFilename(name, this);
currentModuleCache[name] = pathToLoad;
return _require.call(this, pathToLoad);
}
};

function printCache() {
Expand Down

0 comments on commit a06004f

Please sign in to comment.