Created
May 21, 2020 21:18
-
-
Save jagsbyteinception/fa99743af2e934d9452fd5829c8b8c91 to your computer and use it in GitHub Desktop.
nodejs - recursively search directory for file type or extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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