Skip to content

Instantly share code, notes, and snippets.

@olostan
Created August 22, 2011 13:03
Show Gist options
  • Save olostan/1162325 to your computer and use it in GitHub Desktop.
Save olostan/1162325 to your computer and use it in GitHub Desktop.
Recursive list of files
var fs = require('fs');
function dir(path, callback) {
var promises = 0;
var all = [];
function _dir(path) {
promises++;
fs.readdir(path, function(err,files) {
promises--;
if (err) throw err;
for(var f in files) {
var file = path+"/"+files[f];
//console.log(file);
all.push(file);
function recurse(file) {
promises++;
this.callback = function(err, stats) {
promises--;
if (stats.isDirectory()) _dir(file);
if (promises==0) callback(all);
}
}
fs.stat(file,(new recurse(file)).callback);
}
});
};
_dir(path);
}
dir('.', function(list) { console.log(list.join("\n"));} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment