Skip to content

Instantly share code, notes, and snippets.

@jagsbyteinception
Created May 21, 2020 21:18
Show Gist options
  • Save jagsbyteinception/fa99743af2e934d9452fd5829c8b8c91 to your computer and use it in GitHub Desktop.
Save jagsbyteinception/fa99743af2e934d9452fd5829c8b8c91 to your computer and use it in GitHub Desktop.
nodejs - recursively search directory for file type or extension
var searchRecursive = function(dir, pattern) {
var results = [];
fs.readdirSync(dir).forEach(function (dirInner) {
// Obtain absolute path
dirInner = path.resolve(dir, dirInner);
var stat = fs.statSync(dirInner);
// If path is a directory, scan it and combine results
if (stat.isDirectory()) {
results = results.concat(searchRecursive(dirInner, pattern));
}
if (stat.isFile() && dirInner.endsWith(pattern)) {
results.push(dirInner);
}
});
return results;
};
var files = searchRecursive(config.app.publicPath, '.scss');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment