Skip to content

Instantly share code, notes, and snippets.

@thuan1412
Forked from kethinov/walksync.js
Created August 21, 2021 11:17
Show Gist options
  • Save thuan1412/cb1ec664d5d22bb77130915e1bcae6a1 to your computer and use it in GitHub Desktop.
Save thuan1412/cb1ec664d5d22bb77130915e1bcae6a1 to your computer and use it in GitHub Desktop.
List all files in a directory in Node.js recursively in a synchronous fashion
// List all files in a directory in Node.js recursively in a synchronous fashion
var walkSync = function(dir, filelist) {
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
if (fs.statSync(dir + file).isDirectory()) {
filelist = walkSync(dir + file + '/', filelist);
}
else {
filelist.push(file);
}
});
return filelist;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment