Skip to content

Instantly share code, notes, and snippets.

@rouftom
Forked from VinGarcia/walksync.js
Created February 24, 2020 22:28
Show Gist options
  • Save rouftom/5efd83d9085e14375f018afd26b4a191 to your computer and use it in GitHub Desktop.
Save rouftom/5efd83d9085e14375f018afd26b4a191 to your computer and use it in GitHub Desktop.
// List all files in a directory in Node.js recursively in a synchronous fashion
var walkSync = function(dir, filelist) {
if( dir[dir.length-1] != '/') dir=dir.concat('/')
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(dir+file);
}
});
return filelist;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment