From 1ca2e7ace35a6d9049f30d4996752a17c0cc3483 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 12 Dec 2010 02:13:40 -0700 Subject: [PATCH] fixed path type, added 'end', tested chronologability --- README.md | 6 ++++++ examples/find-slowdown-test.js | 33 +++++++++++++++++++++++++++++++++ examples/find-test.js | 5 ++++- examples/find.js | 2 +- lib/walk.js | 16 ++++++++++++---- 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 examples/find-slowdown-test.js diff --git a/README.md b/README.md index 93a6446..8316f6b 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ Usage next(); }); + walker.on("end", function () { + console.log("all done"); + }); + API ==== @@ -66,6 +70,8 @@ Emitted Values Single Events - fired immediately + * `end` - No files, dirs, etc left to inspect + * `directoryError` - Error when `fstat` succeeded, but reading path failed (Probably due to permissions). * `node` - a `stats` object for a node of any type * `file` - includes links when `followLinks` is `true` diff --git a/examples/find-slowdown-test.js b/examples/find-slowdown-test.js new file mode 100644 index 0000000..8bc18d8 --- /dev/null +++ b/examples/find-slowdown-test.js @@ -0,0 +1,33 @@ +(function () { + var walk = require("../lib/walk.js"), + emit = walk(process.argv[2] || "/tmp"), + util = require('util'), + path = require('path'); + + // nor the root, nor the node should ever be empty + walk.fnodeTypesPlural.forEach(function (fnodeType) { + emit.on(fnodeType, function (root, nodes, next) { + if (!nodes || !nodes.length || !root) { + console.log(fnodeType, "empty set", root, nodes.length); //JSON.stringify(nodes)); + } + next(); + }); + }); + walk.fnodeTypes.forEach(function (fnodeType) { + emit.on(fnodeType, function (root, node, next) { + if (!node || !node.name || !root) { + console.log(fnodeType, "empty item", root, node.name); //JSON.stringify(node)); + } + next(); + }); + }); + emit.on('directory', function (root, dir, next) { + console.log(path.join(root, dir.name)); + setTimeout(next, 100); + }); + emit.on('file', function (root, file, next) { + console.log(path.join(root, file.name)); + setTimeout(next, 100); + }); +}()); + diff --git a/examples/find-test.js b/examples/find-test.js index 3eaeb8b..e0132b8 100644 --- a/examples/find-test.js +++ b/examples/find-test.js @@ -1,5 +1,5 @@ (function () { - var walk = require("../lib/walk3.js"), + var walk = require("../lib/walk.js"), emit = walk(process.argv[2] || "/tmp"), util = require('util'), path = require('path'); @@ -31,5 +31,8 @@ next(); }); */ + emit.on('end', function () { + console.log("All Done!"); + }); }()); diff --git a/examples/find.js b/examples/find.js index 39dab76..d44ceb8 100644 --- a/examples/find.js +++ b/examples/find.js @@ -1,5 +1,5 @@ (function () { - var walk = require("../lib/walk3.js"), + var walk = require("../lib/walk.js"), emit = walk(process.argv[2] || "/tmp"); //icount = 0; diff --git a/lib/walk.js b/lib/walk.js index e6e3743..be3bd5f 100644 --- a/lib/walk.js +++ b/lib/walk.js @@ -20,8 +20,16 @@ "files", "directories", "blockDevices", "characterDevices", "symbolicLinks", "FIFOs", "sockets" ]; + function newVersion() { + throw new Error("New Version. Please see API on github.com/coolaj86/node-walk"); + } + // Create a new walk instance - function create(path, options) { + function create(path, options, cb) { + if (cb) { + newVersion(); + } + var emitter = new events.EventEmitter(), fstat = (options||{}).followLinks ? fs.stat : fs.lstat; @@ -103,7 +111,6 @@ } fstat(upath.join(path, file), function (err, stats) { - //console.log("`stat`ed file " + file); stats = stats || {}; stats.name = file; nodeGroups.nodes.push(stats); @@ -145,9 +152,10 @@ } walk(upath.normalize(path), function () { - //throw Error("hey"); - //console.log("Utterly Done!"); + emitter.emit('end'); }); + emitter.walk = newVersion; + emitter.whenever = newVersion; return emitter; } module.exports = create;