From ec07897228f26390add0b577a6855158999dd54f Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 22 Jun 2013 22:56:13 -0700 Subject: [PATCH] added followLinks option for realz --- .jshintrc | 15 +++++++++++++++ lib/walk.js | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 .jshintrc diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..81d5568 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,15 @@ +{ "es5": true +, "node": true +, "browser": true +, "jquery": true + +, "onevar": true +, "laxcomma": true +, "laxbreak": true + +, "eqeqeq": true +, "immed": true +, "undef": true +, "unused": true +, "latedef": true +} diff --git a/lib/walk.js b/lib/walk.js index 5209198..8348e51 100644 --- a/lib/walk.js +++ b/lib/walk.js @@ -1,4 +1,3 @@ -/*jshint strict:true node:true es5:true onevar:true laxcomma:true laxbreak:true*/ // Adapted from work by jorge@jorgechamorro.com on 2010-11-25 (function () { "use strict"; @@ -31,6 +30,9 @@ var me = this ; + options = options || {}; + me._wStat = options.followLinks && 'stat' || 'lstat'; + me._wStatSync = me._wStat + 'Sync'; me._wsync = sync; me._wq = []; me._wqueue = [me._wq]; @@ -39,15 +41,28 @@ me._wcurpath = pathname; if (me._wsync) { + console.log('_walkSync'); me._wWalk = me._wWalkSync; } else { + console.log('_walkASync'); me._wWalk = me._wWalkAsync; } - // TODO just one little anony won't hurt... - process.nextTick(function () { - me._wWalk(); + options.listeners = options.listeners || {}; + Object.keys(options.listeners).forEach(function (event) { + var callbacks = options.listeners[event] + ; + + if ('function' === typeof callbacks) { + callbacks = [callbacks]; + } + + callbacks.forEach(function (callback) { + me.on(event, callback); + }); }); + + me._wWalk(); } // Inherits must come before prototype additions @@ -86,14 +101,14 @@ if (!me._wsync) { // TODO how to remove this anony? - fs.lstat(statPath, function (err, stat) { + fs[me._wStat](statPath, function (err, stat) { me._wLstatHandler(err, stat); }); return; } try { - me._wLstatHandler(null, fs.lstatSync(statPath)); + me._wLstatHandler(null, fs[me._wStatSync](statPath)); } catch(e) { me._wLstatHandler(e); } @@ -159,7 +174,7 @@ me._wfirstrun = false; // TODO how to remove this anony? - fs.lstat(me._wcurpath, function (e, stat) { + fs[me._wStat](me._wcurpath, function (e, stat) { if (stat) { files = [me._wcurpath.replace(/.*\//, '')]; @@ -170,6 +185,7 @@ }); }; Walker.prototype._wWalkSync = function () { + console.log('walkSync'); var err , files , me = this @@ -184,6 +200,7 @@ me._wReaddirHandler(err, files); }; Walker.prototype._wWalkAsync = function () { + console.log('walkAsync'); var me = this ; @@ -209,7 +226,11 @@ me._wq = me._wqueue[me._wqueue.length - 1]; return this._wNext(); } - me.emit('end'); + + // To not break compatibility + //process.nextTick(function () { + me.emit('end'); + //}); }; Walker.prototype._wJoinPath = function (v, i, o) { var me = this