Added async readdir
This commit is contained in:
parent
b91834d796
commit
b8ae7be2f7
3 changed files with 121 additions and 16 deletions
|
|
@ -55,6 +55,70 @@ exports.readdirSyncRecursive = function(baseDir) {
|
|||
return fileList;
|
||||
};
|
||||
|
||||
/* wrench.readdirRecursive("directory_path", function(error, files) {});
|
||||
*
|
||||
* Recursively dives through directories and read the contents of all the
|
||||
* children directories.
|
||||
*
|
||||
* Asynchronous, so returns results/error in callback.
|
||||
* Callback receives the of files in currently recursed directory.
|
||||
* When no more directories are left, callback is called with null for all arguments.
|
||||
*
|
||||
*/
|
||||
exports.readdirRecursive = function(baseDir, fn) {
|
||||
baseDir = baseDir.replace(/\/$/, '');
|
||||
|
||||
var waitCount = 0;
|
||||
|
||||
function readdirRecursive(curDir) {
|
||||
var files = [],
|
||||
curFiles,
|
||||
nextDirs,
|
||||
prependcurDir = function(fname){
|
||||
return _path.join(curDir, fname);
|
||||
};
|
||||
|
||||
waitCount++;
|
||||
fs.readdir(curDir, function(e, curFiles) {
|
||||
waitCount--;
|
||||
|
||||
curFiles = curFiles.map(prependcurDir);
|
||||
|
||||
curFiles.forEach(function(it) {
|
||||
waitCount++;
|
||||
|
||||
fs.stat(it, function(e, stat) {
|
||||
waitCount--;
|
||||
|
||||
if (e) {
|
||||
fn(e);
|
||||
} else {
|
||||
if (stat.isDirectory()) {
|
||||
readdirRecursive(it);
|
||||
}
|
||||
}
|
||||
|
||||
if (waitCount == 0) {
|
||||
fn(null, null);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
fn(null, curFiles.map(function(val) {
|
||||
// convert absolute paths to relative
|
||||
return val.replace(baseDir + '/', '');
|
||||
}));
|
||||
|
||||
if (waitCount == 0) {
|
||||
fn(null, null);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
readdirRecursive(baseDir);
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent);
|
||||
*
|
||||
|
|
|
|||
Reference in a new issue