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);
// if global filelist doesn't exist, create it.
filelist = filelist || [];
// loop through current files
files.forEach(function(file) {
// if it is a directory, lets recurse!
if (fs.statSync(dir + file).isDirectory()) {
filelist = walkSync(dir + file + '/', filelist);
}
else {
// not a directory, will add the file to file list
filelist.push(file);
}
});
return filelist;
};