Skip to content

Instantly share code, notes, and snippets.

@vanleantking
Forked from kethinov/walksync.js
Created May 18, 2021 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanleantking/ec15a5e0969e500c4fd3e69c39a89a59 to your computer and use it in GitHub Desktop.
Save vanleantking/ec15a5e0969e500c4fd3e69c39a89a59 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;
};