[ISSUE #6] initial 'path' may, in fact, be a file

This commit is contained in:
AJ ONeal 2012-01-15 19:21:11 -07:00
parent 2dd4eee860
commit f1c2c4d5e1
1 changed files with 39 additions and 23 deletions

View File

@ -17,7 +17,9 @@
var emitter = new EventEmitter() var emitter = new EventEmitter()
, q = [] , q = []
, queue = [q] , queue = [q]
, curpath; , curpath
, firstRun = true
;
function readdirHandler(err, files) { function readdirHandler(err, files) {
var fnodeGroups = TypeEmitter.createNodeGroups(); var fnodeGroups = TypeEmitter.createNodeGroups();
@ -69,27 +71,45 @@
}); });
} }
function readFiles() {
if (!files || 0 == files.length) {
return next();
}
// TODO could allow user to selectively stat
// and don't stat if there are no stat listeners
emitter.emit('names', curpath, files, noop);
if (sync) {
files.forEach(function (items) {
filesHandler(noop, items);
});
postFilesHandler();
} else {
forEachAsync(files, filesHandler).then(postFilesHandler);
}
}
if (err) { if (err) {
emitter.emit('directoryError', curpath, { error: err }, noop); if (!firstRun) {
emitter.emit('directoryError', curpath, { error: err }, noop);
readFiles();
return;
}
firstRun = false;
fs.lstat(curpath, function (e, stat) {
if (stat) {
files = [curpath.replace(/.*\//, '')];
curpath = curpath.replace(files[0], '');
}
readFiles();
})
//emitter.emit('error', curpath, { error: err }); //emitter.emit('error', curpath, { error: err });
return;
} }
if (!files || 0 == files.length) { readFiles();
return next();
}
// TODO could allow user to selectively stat
// and don't stat if there are no stat listeners
emitter.emit('names', curpath, files, noop);
if (sync) {
files.forEach(function (items) {
filesHandler(noop, items);
});
postFilesHandler();
} else {
forEachAsync(files, filesHandler).then(postFilesHandler);
}
} }
function walkSync() { function walkSync() {
@ -105,11 +125,6 @@
} }
function walk() { function walk() {
if (sync) {
walkSync();
return;
}
fs.readdir(curpath, readdirHandler); fs.readdir(curpath, readdirHandler);
} }
@ -132,6 +147,7 @@
curpath = pathname; curpath = pathname;
if (sync) { if (sync) {
walk = walkSync;
process.nextTick(walk); process.nextTick(walk);
} else { } else {
walk(); walk();