This commit is contained in:
AJ ONeal 2013-06-22 23:09:08 -07:00
commit 8d1fda4cbc
2 changed files with 113 additions and 74 deletions

155
README.md
View File

@ -24,96 +24,111 @@ Usage
Both Asynchronous and Synchronous versions are provided. Both Asynchronous and Synchronous versions are provided.
var walk = require('walk') ```javascript
, fs = require('fs') (function () {
, options "use strict";
, walker
;
options = { var walk = require('walk')
followLinks: false, , fs = require('fs')
}; , options
, walker
;
walker = walk.walk("/tmp", options); options = {
followLinks: false
// directories with these keys will be skipped
, filters: ["Temp", "_Temp"]
};
// OR walker = walk.walk("/tmp", options);
// walker = walk.walkSync("/tmp", options);
walker.on("names", function (root, nodeNamesArray) { // OR
nodeNames.sort(function (a, b) { // walker = walk.walkSync("/tmp", options);
if (a > b) return 1;
if (a < b) return -1; walker.on("names", function (root, nodeNamesArray) {
return 0; nodeNamesArray.sort(function (a, b) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
}); });
});
walker.on("directories", function (root, dirStatsArray, next) { walker.on("directories", function (root, dirStatsArray, next) {
// dirStatsArray is an array of `stat` objects with the additional attributes // dirStatsArray is an array of `stat` objects with the additional attributes
// * type // * type
// * error // * error
// * name // * name
next();
});
walker.on("file", function (root, fileStats, next) {
fs.readFile(fileStats.name, function () {
// doStuff
next(); next();
}); });
});
walker.on("errors", function (root, nodeStatsArray, next) { walker.on("file", function (root, fileStats, next) {
next(); fs.readFile(fileStats.name, function () {
}); // doStuff
next();
});
});
walker.on("end", function () { walker.on("errors", function (root, nodeStatsArray, next) {
console.log("all done"); next();
}); });
walker.on("end", function () {
console.log("all done");
});
}());
``` ```
### Async
The Synchronous version can operate without a callback if the event handlers are passed in ### Sync
Note: Due to changes in EventEmitter,
I don't think it's possible to create a truly synchronous walker,
but I believe it will still finish in a single event loop as-is
(due to changes in process.nextTick).
```javascript ```javascript
var walk = require('walk') (function () {
, fs = require('fs') "use strict";
, options
, walker
;
options = { var walk = require('walk')
, fs = require('fs')
, options
, walker
;
options = {
listeners: { listeners: {
names: function (root, nodeNamesArray) { names: function (root, nodeNamesArray) {
nodeNames.sort(function (a, b) { nodeNames.sort(function (a, b) {
if (a > b) return 1; if (a > b) return 1;
if (a < b) return -1; if (a < b) return -1;
return 0; return 0;
}); });
} }
, directories: function (root, dirStatsArray, next) { , directories: function (root, dirStatsArray, next) {
// dirStatsArray is an array of `stat` objects with the additional attributes // dirStatsArray is an array of `stat` objects with the additional attributes
// * type // * type
// * error // * error
// * name // * name
next();
}
, file: function (root, fileStats, next) {
fs.readFile(fileStats.name, function () {
// doStuff
next(); next();
} });
, file: function (root, fileStats, next) { }
fs.readFile(fileStats.name, function () { , errors: function (root, nodeStatsArray, next) {
// doStuff next();
next(); }
});
}
, errors: function (root, nodeStatsArray, next) {
next();
}
} }
}; };
walker = walk.walkSync("/tmp", options); walker = walk.walkSync("/tmp", options);
console.log("all done"); console.log("all done");
}());
``` ```
API API

View File

@ -37,6 +37,7 @@
me._wq = []; me._wq = [];
me._wqueue = [me._wq]; me._wqueue = [me._wq];
me._wcurpath = undefined; me._wcurpath = undefined;
me._wfilters = options.filters;
me._wfirstrun = true; me._wfirstrun = true;
me._wcurpath = pathname; me._wcurpath = pathname;
@ -191,10 +192,23 @@
, me = this , me = this
; ;
try { // Stop directories that contain filter keywords
files = fs.readdirSync(me._wcurpath); // from continuing through the walk process
} catch(e) { if (me._wfilters != undefined) {
err = e; var shouldExclude = false;
for (var iFilter=0; iFilter<me._wfilters.length; ++iFilter) {
if (me._wcurpath.indexOf(me._wfilters[iFilter]) != -1 ) {
me._wNext();
}
}
}
if( !shouldExclude ) {
try {
files = fs.readdirSync(me._wcurpath);
} catch(e) {
err = e;
}
} }
me._wReaddirHandler(err, files); me._wReaddirHandler(err, files);
@ -204,6 +218,16 @@
var me = this var me = this
; ;
// Stop directories that contain filter keywords
// from continuing through the walk process
if (me._wfilters != undefined) {
for (var iFilter=0; iFilter<me._wfilters.length; ++iFilter) {
if (me._wcurpath.indexOf(me._wfilters[iFilter]) != -1 ) {
me._wNext();
}
}
}
// TODO how to remove this anony? // TODO how to remove this anony?
fs.readdir(me._wcurpath, function (err, files) { fs.readdir(me._wcurpath, function (err, files) {
me._wReaddirHandler(err, files); me._wReaddirHandler(err, files);