added followLinks option for realz

This commit is contained in:
AJ ONeal 2013-06-22 22:56:13 -07:00
parent a26de5a66c
commit ec07897228
2 changed files with 44 additions and 8 deletions

15
.jshintrc Normal file
View File

@ -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
}

View File

@ -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 // Adapted from work by jorge@jorgechamorro.com on 2010-11-25
(function () { (function () {
"use strict"; "use strict";
@ -31,6 +30,9 @@
var me = this var me = this
; ;
options = options || {};
me._wStat = options.followLinks && 'stat' || 'lstat';
me._wStatSync = me._wStat + 'Sync';
me._wsync = sync; me._wsync = sync;
me._wq = []; me._wq = [];
me._wqueue = [me._wq]; me._wqueue = [me._wq];
@ -39,15 +41,28 @@
me._wcurpath = pathname; me._wcurpath = pathname;
if (me._wsync) { if (me._wsync) {
console.log('_walkSync');
me._wWalk = me._wWalkSync; me._wWalk = me._wWalkSync;
} else { } else {
console.log('_walkASync');
me._wWalk = me._wWalkAsync; me._wWalk = me._wWalkAsync;
} }
// TODO just one little anony won't hurt... options.listeners = options.listeners || {};
process.nextTick(function () { Object.keys(options.listeners).forEach(function (event) {
me._wWalk(); 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 // Inherits must come before prototype additions
@ -86,14 +101,14 @@
if (!me._wsync) { if (!me._wsync) {
// TODO how to remove this anony? // TODO how to remove this anony?
fs.lstat(statPath, function (err, stat) { fs[me._wStat](statPath, function (err, stat) {
me._wLstatHandler(err, stat); me._wLstatHandler(err, stat);
}); });
return; return;
} }
try { try {
me._wLstatHandler(null, fs.lstatSync(statPath)); me._wLstatHandler(null, fs[me._wStatSync](statPath));
} catch(e) { } catch(e) {
me._wLstatHandler(e); me._wLstatHandler(e);
} }
@ -159,7 +174,7 @@
me._wfirstrun = false; me._wfirstrun = false;
// TODO how to remove this anony? // TODO how to remove this anony?
fs.lstat(me._wcurpath, function (e, stat) { fs[me._wStat](me._wcurpath, function (e, stat) {
if (stat) { if (stat) {
files = [me._wcurpath.replace(/.*\//, '')]; files = [me._wcurpath.replace(/.*\//, '')];
@ -170,6 +185,7 @@
}); });
}; };
Walker.prototype._wWalkSync = function () { Walker.prototype._wWalkSync = function () {
console.log('walkSync');
var err var err
, files , files
, me = this , me = this
@ -184,6 +200,7 @@
me._wReaddirHandler(err, files); me._wReaddirHandler(err, files);
}; };
Walker.prototype._wWalkAsync = function () { Walker.prototype._wWalkAsync = function () {
console.log('walkAsync');
var me = this var me = this
; ;
@ -209,7 +226,11 @@
me._wq = me._wqueue[me._wqueue.length - 1]; me._wq = me._wqueue[me._wqueue.length - 1];
return this._wNext(); return this._wNext();
} }
// To not break compatibility
//process.nextTick(function () {
me.emit('end'); me.emit('end');
//});
}; };
Walker.prototype._wJoinPath = function (v, i, o) { Walker.prototype._wJoinPath = function (v, i, o) {
var me = this var me = this