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.
var walk = require('walk')
, fs = require('fs')
, options
, walker
;
```javascript
(function () {
"use strict";
options = {
followLinks: false,
};
var walk = require('walk')
, 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.walkSync("/tmp", options);
walker = walk.walk("/tmp", options);
walker.on("names", function (root, nodeNamesArray) {
nodeNames.sort(function (a, b) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
// OR
// walker = walk.walkSync("/tmp", options);
walker.on("names", function (root, nodeNamesArray) {
nodeNamesArray.sort(function (a, b) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
});
});
walker.on("directories", function (root, dirStatsArray, next) {
// dirStatsArray is an array of `stat` objects with the additional attributes
// * type
// * error
// * name
next();
});
walker.on("file", function (root, fileStats, next) {
fs.readFile(fileStats.name, function () {
// doStuff
walker.on("directories", function (root, dirStatsArray, next) {
// dirStatsArray is an array of `stat` objects with the additional attributes
// * type
// * error
// * name
next();
});
});
walker.on("errors", function (root, nodeStatsArray, next) {
next();
});
walker.on("file", function (root, fileStats, next) {
fs.readFile(fileStats.name, function () {
// doStuff
next();
});
});
walker.on("end", function () {
console.log("all done");
});
walker.on("errors", function (root, nodeStatsArray, next) {
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
var walk = require('walk')
, fs = require('fs')
, options
, walker
;
(function () {
"use strict";
options = {
var walk = require('walk')
, fs = require('fs')
, options
, walker
;
options = {
listeners: {
names: function (root, nodeNamesArray) {
nodeNames.sort(function (a, b) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
}
, directories: function (root, dirStatsArray, next) {
// dirStatsArray is an array of `stat` objects with the additional attributes
// * type
// * error
// * name
names: function (root, nodeNamesArray) {
nodeNames.sort(function (a, b) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
}
, directories: function (root, dirStatsArray, next) {
// dirStatsArray is an array of `stat` objects with the additional attributes
// * type
// * error
// * name
next();
}
, file: function (root, fileStats, next) {
fs.readFile(fileStats.name, function () {
// doStuff
next();
}
, file: function (root, fileStats, next) {
fs.readFile(fileStats.name, function () {
// doStuff
next();
});
}
, errors: function (root, nodeStatsArray, 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

View File

@ -37,6 +37,7 @@
me._wq = [];
me._wqueue = [me._wq];
me._wcurpath = undefined;
me._wfilters = options.filters;
me._wfirstrun = true;
me._wcurpath = pathname;
@ -191,10 +192,23 @@
, me = this
;
try {
files = fs.readdirSync(me._wcurpath);
} catch(e) {
err = e;
// 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 {
files = fs.readdirSync(me._wcurpath);
} catch(e) {
err = e;
}
}
me._wReaddirHandler(err, files);
@ -204,6 +218,16 @@
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?
fs.readdir(me._wcurpath, function (err, files) {
me._wReaddirHandler(err, files);