diff --git a/lib/walk.js b/lib/walk.js index 4903c7f..04fbf91 100644 --- a/lib/walk.js +++ b/lib/walk.js @@ -4,6 +4,8 @@ // Array.prototype.forEachAsync(next, item, i, collection) require('futures/forEachAsync'); + function noop() {} + var fs = require('fs'), EventEmitter = require('events').EventEmitter, TypeEmitter = require('./node-type-emitter'); @@ -18,14 +20,15 @@ function walk() { fs.readdir(curpath, function(err, files) { if (err) { - emitter.emit('error', curpath, err); + emitter.emit('directoryError', curpath, { error: err }, noop); + //emitter.emit('error', curpath, { error: err }); } // XXX bug was here. next() was omitted if (!files || 0 == files.length) { return next(); } - var stats = [], - fnodeGroups = TypeEmitter.createNodeGroups(); + + var fnodeGroups = TypeEmitter.createNodeGroups(); // TODO could allow user to selectively stat // and don't stat if there are no stat listeners @@ -33,22 +36,25 @@ files.forEachAsync(function (cont, file) { emitter.emit('name', curpath, file); fs.lstat(curpath + '/' + file, function (err, stat) { - if (err) { - emitter.emit('error', curpath, err); - } - if (!stat) { - cont(); - } + stat = stat || {}; stat.name = file; - stats.push(stat); - //emitter.emit('stat', curpath, file, stat); - TypeEmitter.sortFnodesByType(stat, fnodeGroups); - TypeEmitter.emitNodeType(emitter, curpath, stat, cont); + if (err) { + stat.error = err; + //emitter.emit('error', curpath, stat); + emitter.emit('nodeError', curpath, stat, noop); + fnodeGroups.errors.push(stat); + cont(); + } else { + TypeEmitter.sortFnodesByType(stat, fnodeGroups); + TypeEmitter.emitNodeType(emitter, curpath, stat, cont); + } }); }).then(function () { - var dirs = [] - //emitter.emit('stats', curpath, files, stats); + if (fnodeGroups.errors.length) { + emitter.emit('errors', curpath, fnodeGroups.errors); + } TypeEmitter.emitNodeTypeGroups(emitter, curpath, fnodeGroups, function () { + var dirs = []; fnodeGroups.directories.forEach(function (stat) { dirs.push(stat.name); }); diff --git a/profile/walk-test.js b/profile/walk-test.js new file mode 100755 index 0000000..bb6dfb0 --- /dev/null +++ b/profile/walk-test.js @@ -0,0 +1,127 @@ +#!/usr/bin/env node +(function () { + "use strict"; + + var walk = require('../lib/walk'), + count = 0, + saneCount = 0; + + function sort(a,b) { + a= a.toLowerCase(); + b= b.toLowerCase(); + if (a > b) return -1; + if (a < b) return 1; + else return 0; + } + + process.argv.forEach(function(startpath, index) { + if (index > 1) { + emitter = walk(startpath); + + // Non-`stat`ed Nodes + emitter.on('name', function (path, file, stat) { + saneCount += 1; + //console.log( ["[", count, "] ", path, '/', file].join('') ) + console.log( [path, '/', file].join('') ) + }); + /* + emitter.on('names', function (path, files, stats) { + files.sort(sort); + //console.log('sort: ' + files.join(' ; ')); + }); + */ + + + // Single `stat`ed Nodes + emitter.on('error', function (path, err, next) { + // ignore + }); + emitter.on('directoryError', function (path, stats, next) { + next(); + }); + emitter.on('nodeError', function (path, stats, next) { + next(); + }); + /* + emitter.on('node', function (path, stat, next) { + count += 1; + console.log( [path, '/', stat.name].join('') ) + //console.log( ["[", count, "] ", path, '/', stat.name].join('') ) + next(); + }); + */ + emitter.on('file', function (path, stat, next) { + count += 1; + console.log( [path, '/', stat.name].join('') ) + //console.log( ["[", count, "] ", path, '/', stat.name].join('') ) + next(); + }); + emitter.on('directory', function (path, stat, next) { + count += 1; + console.log( [path, '/', stat.name].join('') ) + next(); + }); + emitter.on('symbolicLink', function (path, stat, next) { + count += 1; + console.log( [path, '/', stat.name].join('') ) + next(); + }); + /* + */ + /* + emitter.on('blockDevice', function (path, stat, next) { + next(); + }); + emitter.on('characterDevice', function (path, stat, next) { + next(); + }); + emitter.on('FIFO', function (path, stat, next) { + next(); + }); + emitter.on('socket', function (path, stat, next) { + next(); + }); + */ + + // Grouped `stat`ed Nodes + emitter.on('errors', function (path, stats, next) { + next(); + }); + /* + emitter.on('nodes', function (path, stats, next) { + next(); + }); + */ + emitter.on('files', function (path, stats, next) { + next(); + }); + emitter.on('directories', function (path, stats, next) { + //delete stats[1]; + next(); + }); + emitter.on('symbolicLinks', function (path, stats, next) { + next(); + }); + /* + emitter.on('blockDevices', function (path, stats, next) { + next(); + }); + emitter.on('characterDevices', function (path, stats, next) { + next(); + }); + emitter.on('FIFOs', function (path, stats, next) { + next(); + }); + emitter.on('sockets', function (path, stats, next) { + next(); + }); + */ + + // The end of all things + emitter.on('end', function () { + console.log("The eagle has landed. [" + count + " == " + saneCount + "]"); + }); + } + }); + +}());