Skip to content

Instantly share code, notes, and snippets.

@caolan
Created March 23, 2011 10:05
Show Gist options
  • Save caolan/882892 to your computer and use it in GitHub Desktop.
Save caolan/882892 to your computer and use it in GitHub Desktop.
List all files below a given path, recursing through subdirectories using node.js and the Async module
/**
* List all files below a given path, recursing through subdirectories.
*
* @param {String} p
* @param {Function} callback
* @api public
*/
exports.descendants = function (p, callback) {
fs.stat(p, function (err, stats) {
if (err) {
return callback(err);
}
if (stats.isDirectory()) {
fs.readdir(p, function (err, files) {
if (err) {
return callback(err);
}
var paths = files.map(function (f) {
return path.join(p, f);
});
async.concat(paths, exports.descendants, function (err, files) {
if (err) {
callback(err);
}
else {
callback(err, files);
}
});
});
}
else if (stats.isFile()) {
callback(null, p);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment