Created
March 23, 2011 10:05
-
-
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
This file contains hidden or 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
/** | |
* 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