Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #73 from kss-node/pr-63-rebased

Updated walk.js to be re-entrant.
  • Loading branch information...
commit 36793d33beb15340597dbc34abd9374d7288ab96 2 parents 0000e2b + 684e968
@JohnAlbin JohnAlbin authored
Showing with 53 additions and 44 deletions.
  1. +53 −44 lib/walk.js
View
97 lib/walk.js
@@ -3,54 +3,63 @@ var fs = require("fs");
var path = require("path");
module.exports = (function() {
- var counter = 0;
- var walk = function(dirname, options, callbacks) {
- var callbacks = callbacks || {};
-
- options = options || {};
- if (typeof callbacks.finished !== 'function') { callbacks.finished = function(){}; }
- if (typeof callbacks.file !== 'function') { callbacks.file = function(){}; }
-
- counter += 1;
- fs.readdir(dirname, function(err, relnames) {
- if(err) {
- callbacks.finished(err);
- return;
- }
-
- if (!relnames.length) {
- counter -= 1;
- if (!counter) callbacks.finished(null);
- return;
- }
-
- relnames.forEach(function(relname, index, relnames) {
- var name = path.join(dirname, relname);
- counter += 1;
-
- fs.stat(name, function(err, stat) {
- if(err) {
- callbacks.finished(err);
- return;
- }
-
- if(stat.isDirectory()) {
- if (name !== '.svn' && name !== '.git') walk(name, options, callbacks);
- } else {
- if (!options.mask || name.match(options.mask)) {
- callbacks.file(name);
- }
- }
+
+ function walk(dirname, options, callbacks) {
+
+ var counter = 0;
+
+ var walkInternal = function(dirname, options, callbacks) {
+ var callbacks = callbacks || {};
+
+ options = options || {};
+ if (typeof callbacks.finished !== 'function') { callbacks.finished = function(){}; }
+ if (typeof callbacks.file !== 'function') { callbacks.file = function(){}; }
+
+ counter += 1;
+ fs.readdir(dirname, function(err, relnames) {
+ if(err) {
+ callbacks.finished(err);
+ return;
+ }
+
+ if (!relnames.length) {
counter -= 1;
- if(index === relnames.length - 1) counter -= 1;
+ if (!counter) callbacks.finished(null);
+ return;
+ }
+
+ relnames.forEach(function(relname, index, relnames) {
+ var name = path.join(dirname, relname);
+ counter += 1;
- if(counter === 0) {
- callbacks.finished(null);
- }
+ fs.stat(name, function(err, stat) {
+ if(err) {
+ callbacks.finished(err);
+ return;
+ }
+
+ if(stat.isDirectory()) {
+ if (name !== '.svn' && name !== '.git') walkInternal(name, options, callbacks);
+ } else {
+ if (!options.mask || name.match(options.mask)) {
+ callbacks.file(name);
+ }
+ }
+
+ counter -= 1;
+ if(index === relnames.length - 1) counter -= 1;
+
+ if(counter === 0) {
+ callbacks.finished(null);
+ }
+ });
});
});
- });
- };
+ };
+
+ walkInternal(dirname, options, callbacks);
+ }
+
return walk;
})();
Please sign in to comment.
Something went wrong with that request. Please try again.