Skip to content

Commit

Permalink
Chore: Cache fs reads in ignored-paths (fixes #8363) (#8706)
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom authored and not-an-aardvark committed Jun 13, 2017
1 parent c61194f commit 26a2daa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
40 changes: 26 additions & 14 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,6 @@ class IgnoredPaths {
return ig.addPattern(pattern);
}

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

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

Expand Down Expand Up @@ -155,8 +144,8 @@ 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);
this.addIgnoreFile(this.ig.custom, ignorePath);
this.addIgnoreFile(this.ig.default, ignorePath);
}

if (options.ignorePattern) {
Expand All @@ -168,6 +157,29 @@ class IgnoredPaths {
this.options = options;
}

/**
* read ignore filepath
* @param {string} filePath, file to add to ig
* @returns {array} raw ignore rules
*/
readIgnoreFile(filePath) {
if (typeof this.cache[filePath] === "undefined") {
this.cache[filePath] = fs.readFileSync(filePath, "utf8");
}
return this.cache[filePath];
}

/**
* add ignore file to node-ignore instance
* @param {Object} ig, instance of node-ignore
* @param {string} filePath, file to add to ig
* @returns {array} raw ignore rules
*/
addIgnoreFile(ig, filePath) {
ig.ignoreFiles.push(filePath);
return ig.add(this.readIgnoreFile(filePath));
}

/**
* Determine whether a file path is included in the default or custom ignore patterns
* @param {string} filepath Path to check
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/ignored-paths.js
Expand Up @@ -164,6 +164,30 @@ describe("IgnoredPaths", () => {
)
);
});


});

describe("caching file reads", () => {

let readFileSyncCount;

before(() => {
readFileSyncCount = sinon.spy(fs, "readFileSync");
});

after(() => {
readFileSyncCount.restore();
});

it("should cache readFileSync on same file paths", () => {
const ignoreFilePath = getFixturePath(".eslintignore");
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });

ignoredPaths.readIgnoreFile(ignoreFilePath);
assert.isTrue(ignoredPaths.contains(ignoreFilePath));
sinon.assert.calledOnce(readFileSyncCount);
});
});

describe("initialization with ignorePattern", () => {
Expand Down

0 comments on commit 26a2daa

Please sign in to comment.