Skip to content

Instantly share code, notes, and snippets.

@MichaelPaulukonis
Forked from VinGarcia/walksync.js
Created May 28, 2016 02:54
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 MichaelPaulukonis/576f620634a85d5ae4a75297a1c7febe to your computer and use it in GitHub Desktop.
Save MichaelPaulukonis/576f620634a85d5ae4a75297a1c7febe 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;
};
@MichaelPaulukonis
Copy link
Author

There's a small npm library if you want to do this idiomatically (asynchronously).

But if you're not, there's this. Which is also small. But not an npm library, AFAICT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment