Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: avoid unnecessary filesystem accesses during config search #10359

Merged
merged 1 commit into from May 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/config/config-file.js
Expand Up @@ -567,6 +567,22 @@ function load(filePath, configContext, relativeTo) {
return config;
}

/**
* Checks whether the given filename points to a file
* @param {string} filename A path to a file
* @returns {boolean} `true` if a file exists at the given location
*/
function isExistingFile(filename) {
try {
return fs.statSync(filename).isFile();
} catch (err) {
if (err.code === "ENOENT") {
return false;
}
throw err;
}
}


//------------------------------------------------------------------------------
// Public Interface
Expand All @@ -591,14 +607,6 @@ module.exports = {
* or null if there is no configuration file in the directory.
*/
getFilenameForDirectory(directory) {
for (let i = 0, len = CONFIG_FILES.length; i < len; i++) {
const filename = path.join(directory, CONFIG_FILES[i]);

if (fs.existsSync(filename) && fs.statSync(filename).isFile()) {
return filename;
}
}

return null;
return CONFIG_FILES.map(filename => path.join(directory, filename)).find(isExistingFile) || null;
}
};