2010-12-12 08:58:43 +00:00
|
|
|
// TODO
|
|
|
|
// * add types by listener dynamically
|
|
|
|
// * unroll loops for better readability?
|
|
|
|
// * should emitted errors wait for `next()`?
|
|
|
|
(function (undefined) {
|
2010-11-21 05:02:53 +00:00
|
|
|
var fs = require('fs'),
|
2010-12-12 08:58:43 +00:00
|
|
|
upath = require('path'),
|
2010-11-21 05:02:53 +00:00
|
|
|
util = require('util'),
|
2010-12-12 08:58:43 +00:00
|
|
|
Futures = require('futures'),
|
|
|
|
events = require('events'),
|
|
|
|
noop = function () {},
|
|
|
|
// "FIFO" isn't easy to convert to camelCame and back reliably
|
|
|
|
isFnodeTypes = [
|
|
|
|
"isFile", "isDirectory", "isBlockDevice", "isCharacterDevice", "isSymbolicLink", "isFIFO", "isSocket"
|
2010-12-02 10:09:01 +00:00
|
|
|
],
|
2010-12-12 08:58:43 +00:00
|
|
|
fnodeTypes = [
|
|
|
|
"file", "directory", "blockDevice", "characterDevice", "symbolicLink", "FIFO", "socket"
|
2010-12-02 10:09:01 +00:00
|
|
|
],
|
2010-12-12 08:58:43 +00:00
|
|
|
fnodeTypesPlural = [
|
|
|
|
"files", "directories", "blockDevices", "characterDevices", "symbolicLinks", "FIFOs", "sockets"
|
|
|
|
];
|
|
|
|
|
2010-12-12 09:13:40 +00:00
|
|
|
function newVersion() {
|
|
|
|
throw new Error("New Version. Please see API on github.com/coolaj86/node-walk");
|
|
|
|
}
|
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
// Create a new walk instance
|
2010-12-12 09:13:40 +00:00
|
|
|
function create(path, options, cb) {
|
|
|
|
if (cb) {
|
|
|
|
newVersion();
|
|
|
|
}
|
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
var emitter = new events.EventEmitter(),
|
|
|
|
fstat = (options||{}).followLinks ? fs.stat : fs.lstat;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the current number of listeners (which may change)
|
|
|
|
// Emit events to each listener
|
|
|
|
// Wait for all listeners to `next()` before continueing
|
|
|
|
// (in theory this may avoid disk thrashing)
|
|
|
|
function emitSingleEvents(path, stats, next) {
|
|
|
|
var num = 1 + emitter.listeners(stats.type).length + emitter.listeners("node").length;
|
|
|
|
|
|
|
|
function nextWhenReady() {
|
|
|
|
num -= 1;
|
|
|
|
if (0 === num) { next(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
emitter.emit(stats.type, path, stats, nextWhenReady);
|
|
|
|
emitter.emit("node", path, stats, nextWhenReady);
|
|
|
|
nextWhenReady();
|
|
|
|
}
|
2010-12-02 10:09:01 +00:00
|
|
|
|
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
// Since the risk for disk thrashing among anything
|
|
|
|
// other than files is relatively low, all types are
|
|
|
|
// emitted at once, but all must complete before advancing
|
|
|
|
function emitPluralEvents(path, nodes, next) {
|
|
|
|
var num = 1;
|
2010-12-02 10:09:01 +00:00
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
function nextWhenReady() {
|
|
|
|
num -= 1;
|
|
|
|
if (0 === num) { next(); }
|
|
|
|
}
|
2010-12-02 10:09:01 +00:00
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
fnodeTypesPlural.concat(["nodes", "errors"]).forEach(function (fnodeType) {
|
|
|
|
if (0 === nodes[fnodeType].length) { return; }
|
|
|
|
num += emitter.listeners(fnodeType).length;
|
|
|
|
emitter.emit(fnodeType, path, nodes[fnodeType], nextWhenReady);
|
|
|
|
});
|
|
|
|
nextWhenReady();
|
|
|
|
}
|
2010-12-02 10:09:01 +00:00
|
|
|
|
2010-11-21 05:02:53 +00:00
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
// Determine each file node's type
|
|
|
|
//
|
|
|
|
function sortFnodesByType(path, stats, fnodes, nextFile) {
|
|
|
|
isFnodeTypes.forEach(function (isType, i) {
|
|
|
|
if (stats[isType]()) {
|
|
|
|
if (stats.type) { throw new Error("is_" + type + " and " + isType); }
|
|
|
|
stats.type = fnodeTypes[i];
|
|
|
|
fnodes[fnodeTypesPlural[i]].push(stats);
|
|
|
|
// TODO throw to break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!stats.type) { throw new Error(upath.join(path, stats.name) + ' isAnUndefinedType'); }
|
|
|
|
emitSingleEvents(path, stats, nextFile);
|
2010-11-21 05:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
// Asynchronously get the stats
|
|
|
|
//
|
|
|
|
function getStats(path, files, walkDirs) {
|
|
|
|
var nodeGroups = {};
|
2010-11-21 05:02:53 +00:00
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
fnodeTypesPlural.concat("nodes", "errors").forEach(function (fnodeTypePlural) {
|
|
|
|
nodeGroups[fnodeTypePlural] = [];
|
|
|
|
});
|
2010-11-21 05:02:53 +00:00
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
function nextFile() {
|
|
|
|
var file = files.pop(), dirs = [], fnames = [];
|
2010-11-21 05:02:53 +00:00
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
if (undefined === file) {
|
|
|
|
emitPluralEvents(path, nodeGroups, function () {
|
|
|
|
nodeGroups.directories.forEach(function (dir) {
|
|
|
|
dirs.push(dir.name);
|
2010-11-21 05:02:53 +00:00
|
|
|
});
|
2010-12-12 08:58:43 +00:00
|
|
|
walkDirs(dirs);
|
2010-11-21 05:02:53 +00:00
|
|
|
});
|
2010-12-12 08:58:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-11-21 05:02:53 +00:00
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
fstat(upath.join(path, file), function (err, stats) {
|
|
|
|
stats = stats || {};
|
|
|
|
stats.name = file;
|
|
|
|
nodeGroups.nodes.push(stats);
|
|
|
|
if (err) {
|
|
|
|
stats.error = err;
|
|
|
|
stats.type = 'error';
|
|
|
|
nodeGroups.errors.push(stats);
|
|
|
|
//emitter.emit('fileError', path, stats, noop);
|
|
|
|
return nextFile();
|
2010-12-02 10:09:01 +00:00
|
|
|
}
|
2010-12-12 08:58:43 +00:00
|
|
|
sortFnodesByType(path, stats, nodeGroups, nextFile);
|
2010-11-21 05:02:53 +00:00
|
|
|
});
|
2010-12-12 08:58:43 +00:00
|
|
|
}
|
|
|
|
nextFile();
|
|
|
|
}
|
2010-11-21 05:02:53 +00:00
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
function walk(path, next) {
|
|
|
|
fs.readdir(path, function (err, nodes) {
|
|
|
|
if (err) {
|
|
|
|
emitter.emit('directoryError', path, { error: err, name: path }, noop);
|
|
|
|
return next(); /*TODO*/ throw err;
|
|
|
|
}
|
|
|
|
getStats(path, nodes, function (dirs) {
|
|
|
|
walkDirs(path, dirs, next);
|
|
|
|
});
|
2010-11-21 05:02:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
function walkDirs(path, dirs, cb) {
|
|
|
|
function nextDir() {
|
|
|
|
var dir = dirs.pop();
|
|
|
|
if (undefined === dir) {
|
|
|
|
delete dirs;
|
|
|
|
return cb();
|
|
|
|
}
|
|
|
|
walk(upath.join(path, dir), nextDir);
|
|
|
|
}
|
|
|
|
nextDir();
|
|
|
|
}
|
2010-11-21 05:02:53 +00:00
|
|
|
|
2010-12-12 08:58:43 +00:00
|
|
|
walk(upath.normalize(path), function () {
|
2010-12-12 09:13:40 +00:00
|
|
|
emitter.emit('end');
|
2010-12-12 08:58:43 +00:00
|
|
|
});
|
2010-12-12 09:13:40 +00:00
|
|
|
emitter.walk = newVersion;
|
|
|
|
emitter.whenever = newVersion;
|
2010-12-02 10:09:01 +00:00
|
|
|
return emitter;
|
2010-11-21 05:02:53 +00:00
|
|
|
}
|
2010-12-12 08:58:43 +00:00
|
|
|
module.exports = create;
|
|
|
|
module.exports.isFnodeTypes = isFnodeTypes;
|
|
|
|
module.exports.fnodeTypes = fnodeTypes;
|
|
|
|
module.exports.fnodeTypesPlural = fnodeTypesPlural;
|
2010-11-21 05:02:53 +00:00
|
|
|
}());
|