Skip to content

Instantly share code, notes, and snippets.

@kounelios13
Forked from kethinov/walksync.js
Last active October 6, 2017 20:19
Show Gist options
  • Save kounelios13/250bfbe8fdde3f85cc5bfceed667adeb to your computer and use it in GitHub Desktop.
Save kounelios13/250bfbe8fdde3f85cc5bfceed667adeb to your computer and use it in GitHub Desktop.
List all files in a directory in Node.js recursively in a synchronous fashion
const fs = require('fs');
// List all files in a directory in Node.js recursively in a synchronous fashion
const walkSync = function(dir, filelist,ignoreList) {
if(!dir.endsWith('/')){
dir+='/';
}
const files = fs.readdirSync(dir);
filelist = filelist || [];
ignoreList = ignoreList || [];
files.forEach(function(file) {
if (fs.statSync(dir + file).isDirectory()) {
filelist = walkSync(dir + file + '/', filelist,ignoreList);
}
else {
if(!ignoreList.includes(file)){
filelist.push(dir+file);
}
}
});
return filelist;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment