Skip to content

Commit

Permalink
ignore errors while walking; allow stopping walk.sync
Browse files Browse the repository at this point in the history
  • Loading branch information
popeindustries committed Apr 28, 2015
1 parent 37a9e65 commit 9cd4bc1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/hunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module.exports.sync = function huntSync (directory, matcher, stopOnFirstMatch) {
if (isMatch) {
matches.push(resource);
finished = stopOnFirstMatch;
if (finished) return true;
}
}
});
Expand Down
49 changes: 22 additions & 27 deletions lib/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@ module.exports = function walk (dir, visitor, fn) {
files.forEach(function (file) {
file = path.join(dir, file);
fs.stat(file, function (err, stat) {
if (err) {
// Skip if not found, otherwise exit
if (err.code === 'ENOENT') {
return (!--outstanding) ? next(finished) : null;
} else {
return fn(err);
}
}

if (!finished) {
// Skip on error
if (err) return (!--outstanding) ? next(finished) : null;

visitor(file, stat, function (stop) {
if (stop === true) finished = true;

Expand All @@ -61,41 +55,42 @@ module.exports = function walk (dir, visitor, fn) {

/**
* Synchronously walk directory tree from 'directory', passing all resources to 'visitor'.
* Stops walking when root directory reached.
* Stops walking when root directory reached or `true` is returned from 'visitor'.
* @param {String} dir
* @param {Function} visitor(resource)
*/
module.exports.sync = function walkSync (directory, visitor) {
directory = path.resolve(directory);

function visit (dir) {
var files = fs.readdirSync(dir)
, outstanding = files.length
, next = function () {
var parent = path.resolve(dir, '..');
function next () {
var parent = path.resolve(dir, '..');

// Stop if we can no longer go up a level
if (parent.toLowerCase() === dir.toLowerCase()) return;
// Stop if we can no longer go up a level
if (parent.toLowerCase() === dir.toLowerCase()) return;

// Up one level
visit(parent);
};
// Up one level
visit(parent);
}

var files = fs.readdirSync(dir)
, outstanding = files.length
, finished = false;

files.forEach(function (file) {
file = path.join(dir, file);
try {
var stat = fs.statSync(file);
} catch (err) {
// Skip if file not found, otherwise throw
if (err.code === 'ENOENT') {
return (!--outstanding) ? next() : null;
} else {
throw err;
}
// Skip if error
return (!--outstanding) ? next() : null;
}

visitor(file, stat);
if (!--outstanding) return next();
if (!finished) {
var stop = visitor(file, stat);
if (stop === true) finished = true;
if (!--outstanding && !finished) return next();
}
});
}

Expand Down
8 changes: 8 additions & 0 deletions test/fs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ describe('recur-fs', function () {
});
visits.should.equal(4);
});
it('should allow for early termination', function () {
var visits = 0;
walk.sync(path.resolve('readdir-nested/src/package'), function (resource, stat) {
if (~resource.indexOf('.js')) visits++;
return true;
});
visits.should.equal(1);
});
});
});

Expand Down

0 comments on commit 9cd4bc1

Please sign in to comment.