Created
June 15, 2012 01:39
-
-
Save nfeldman/2934140 to your computer and use it in GitHub Desktop.
recursive readdir for node (via stackoverflow)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/q/5827612/ | |
function walk(dir, done) { | |
var results = []; | |
fs.readdir(dir, function (err, list) { | |
if (err) { | |
return done(err); | |
} | |
var i = 0; | |
(function next() { | |
var file = list[i++]; | |
if (!file) { | |
return done(null, results); | |
} | |
file = dir + '/' + file; | |
fs.stat(file, function (err, stat) { | |
if (stat && stat.isDirectory()) { | |
walk(file, function (err, res) { | |
results = results.concat(res); | |
next(); | |
}); | |
} else { | |
results.push(file); | |
next(); | |
} | |
}); | |
}()); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment