merged
This commit is contained in:
commit
8d1fda4cbc
65
README.md
65
README.md
|
@ -24,65 +24,79 @@ Usage
|
||||||
|
|
||||||
Both Asynchronous and Synchronous versions are provided.
|
Both Asynchronous and Synchronous versions are provided.
|
||||||
|
|
||||||
var walk = require('walk')
|
```javascript
|
||||||
|
(function () {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var walk = require('walk')
|
||||||
, fs = require('fs')
|
, fs = require('fs')
|
||||||
, options
|
, options
|
||||||
, walker
|
, walker
|
||||||
;
|
;
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
// OR
|
// OR
|
||||||
// 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;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
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();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
walker.on("file", function (root, fileStats, next) {
|
walker.on("file", function (root, fileStats, next) {
|
||||||
fs.readFile(fileStats.name, function () {
|
fs.readFile(fileStats.name, function () {
|
||||||
// doStuff
|
// doStuff
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
walker.on("errors", function (root, nodeStatsArray, next) {
|
walker.on("errors", function (root, nodeStatsArray, next) {
|
||||||
next();
|
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
|
||||||
var walk = require('walk')
|
(function () {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var walk = require('walk')
|
||||||
, fs = require('fs')
|
, fs = require('fs')
|
||||||
, options
|
, options
|
||||||
, walker
|
, walker
|
||||||
;
|
;
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
listeners: {
|
listeners: {
|
||||||
names: function (root, nodeNamesArray) {
|
names: function (root, nodeNamesArray) {
|
||||||
nodeNames.sort(function (a, b) {
|
nodeNames.sort(function (a, b) {
|
||||||
|
@ -109,11 +123,12 @@ options = {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
walker = walk.walkSync("/tmp", options);
|
walker = walk.walkSync("/tmp", options);
|
||||||
|
|
||||||
console.log("all done");
|
console.log("all done");
|
||||||
|
}());
|
||||||
```
|
```
|
||||||
|
|
||||||
API
|
API
|
||||||
|
|
24
lib/walk.js
24
lib/walk.js
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue