I made a function for recursively reading a directory and it just seems kind of long and complicated. If you could take a look and tell me what's good or bad about it, that would be great.
Reading the directory synchronously is not an option.
var dutils = require('./dutils');
module.exports = {
getIndex: function(ignore, cb) {
var root = this.root;
var cbs = 1;
var index = {};
// recursive function for walking directory
(function walk(dir) {
fs.readdir(dir, function(err, files) {
if (err) return cb(err);
files.forEach(function(fileName) {
// prepare
var filePath = path.join(dir, fileName); // filePath with root
var relFilePath = path.relative(root, filePath); // filePath without root
// if the file is on our ignore list don't index it
if (dutils.globmatch(filePath, ignore)) return;
var stats = fs.statSync(filePath);
var isDir = stats.isDirectory();
index[relFilePath] = {
isDir: isDir,
mtime: stats.mtime
};
// if the file is a directory, walk it next
if (isDir) {
// count the number of callbacks that are running concurrently
cbs++;
walk(filePath);
}
});
// our callback was completed
cbs--;
// if all callbacks were completed, we've completely walked our directory
if (cbs === 0) cb(null, index);
});
})(root);
}
}
cbs
counter is essentiallyasync.eachSeries(files, ...)
\$\endgroup\$