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

View File

@ -24,6 +24,10 @@ Usage
Both Asynchronous and Synchronous versions are provided. Both Asynchronous and Synchronous versions are provided.
```javascript
(function () {
"use strict";
var walk = require('walk') var walk = require('walk')
, fs = require('fs') , fs = require('fs')
, options , options
@ -31,7 +35,9 @@ var walk = require('walk')
; ;
options = { options = {
followLinks: false, followLinks: false
// directories with these keys will be skipped
, filters: ["Temp", "_Temp"]
}; };
walker = walk.walk("/tmp", options); walker = walk.walk("/tmp", options);
@ -40,7 +46,7 @@ walker = walk.walk("/tmp", options);
// walker = walk.walkSync("/tmp", options); // walker = walk.walkSync("/tmp", options);
walker.on("names", function (root, nodeNamesArray) { walker.on("names", function (root, nodeNamesArray) {
nodeNames.sort(function (a, b) { nodeNamesArray.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;
@ -70,12 +76,20 @@ walker.on("errors", function (root, nodeStatsArray, next) {
walker.on("end", function () { walker.on("end", function () {
console.log("all done"); 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
(function () {
"use strict";
var walk = require('walk') var walk = require('walk')
, fs = require('fs') , fs = require('fs')
, options , options
@ -114,6 +128,7 @@ options = {
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,11 +192,24 @@
, me = this , me = this
; ;
// Stop directories that contain filter keywords
// from continuing through the walk process
if (me._wfilters != undefined) {
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 { try {
files = fs.readdirSync(me._wcurpath); files = fs.readdirSync(me._wcurpath);
} catch(e) { } catch(e) {
err = 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);