Skip to content

Commit

Permalink
Chore: Cache fs reads in ignored-paths (fixes eslint#8363)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom committed Jun 10, 2017
1 parent 3da7b5e commit c0db837
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/ignored-paths.js
Expand Up @@ -37,7 +37,6 @@ const DEFAULT_OPTIONS = {
cwd: process.cwd()
};


//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -80,6 +79,7 @@ class IgnoredPaths {
*/
constructor(options) {
options = mergeDefaultOptions(options);
this.cache = {};

/**
* add pattern to node-ignore instance
Expand All @@ -91,17 +91,29 @@ class IgnoredPaths {
return ig.addPattern(pattern);
}

/**
* read ignore filepath
* @param {string} filepath, file to add to ig
* @returns {array} raw ignore rules
*/
function readIgnoreFile(filepath) {
return fs.readFileSync(filepath, "utf8");
}

/**
* add ignore file to node-ignore instance
* @param {Object} ig, instance of node-ignore
* @param {string} filepath, file to add to ig
* @param {string} cache, file object to add to ig
* @returns {array} raw ignore rules
*/
function addIgnoreFile(ig, filepath) {
function addIgnoreFile(ig, filepath, cache) {
ig.ignoreFiles.push(filepath);
return ig.add(fs.readFileSync(filepath, "utf8"));
return ig.add(cache[filepath]);
}



this.defaultPatterns = [].concat(DEFAULT_IGNORE_DIRS, options.patterns || []);
this.baseDir = options.cwd;

Expand Down Expand Up @@ -155,8 +167,11 @@ class IgnoredPaths {
if (ignorePath) {
debug(`Adding ${ignorePath}`);
this.baseDir = path.dirname(path.resolve(options.cwd, ignorePath));
addIgnoreFile(this.ig.custom, ignorePath);
addIgnoreFile(this.ig.default, ignorePath);
if (!this.cache[ignorePath]) {
this.cache[ignorePath] = readIgnoreFile(ignorePath);
}
addIgnoreFile(this.ig.custom, ignorePath, this.cache);
addIgnoreFile(this.ig.default, ignorePath, this.cache);
}

if (options.ignorePattern) {
Expand Down

0 comments on commit c0db837

Please sign in to comment.