Skip to content

Commit

Permalink
Merge pull request #2 from steveukx/ignoreErrors
Browse files Browse the repository at this point in the history
Don't throw errors when switched off
  • Loading branch information
steveukx committed Jun 1, 2014
2 parents 5271bca + e673ac4 commit 44e144b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
27 changes: 24 additions & 3 deletions lib/readdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
var deferred = Q.defer();

fs.readdir(dir, function (err, contents) {
if (err) deferred.reject(err);
if (err) deferred_error(deferred, err, options);
else if (!contents.length) {
deferred.resolve();
}
Expand All @@ -116,7 +116,7 @@
fs.stat(newPath, function (err, stat) {
var isDirectory = stat && stat.isDirectory();

if (err) deferred.reject(err);
if (err) deferred_error(deferred, err, options);
else if (isDirectory) {
if(exports.INCLUDE_DIRECTORIES & options) {
appendTo.push(newPath.substring(prefixLength) + '/');
Expand All @@ -141,6 +141,15 @@
return deferred.promise;
}

function deferred_error(deferred, error, options) {
if (exports.IGNORE_ERRORS & options) {
deferred.resolve();
}
else {
deferred.reject(error);
}
}

/**
* Changes the values in the supplied paths array to be absolute URIs
*
Expand Down Expand Up @@ -206,7 +215,12 @@
*/
exports.readSync = function(basePath, includeFilters, options) {
var rootDir = basePath.replace(/\/$/, '') + '/';
return apply_filters(basePath, read_dir_sync(rootDir, [], rootDir.length, options), includeFilters, options);
if (!fs.existsSync(rootDir)) {
return [];
}
else {
return apply_filters(basePath, read_dir_sync(rootDir, [], rootDir.length, options), includeFilters, options);
}
};

/**
Expand Down Expand Up @@ -291,4 +305,11 @@
*/
exports.NON_RECURSIVE = 32;

/**
* Bitwise option for preventing errors reading directories from aborting the scan whenever possible - includes
* incorrectly rooted relative symlinks and missing root directory.
* @type {number}
*/
exports.IGNORE_ERRORS = 64;

}(typeof module == 'undefined' ? (window.ReadDir = {}) : module.exports));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "readdir",
"description": "Reads a directory and return results with the ability to use Ant style file match patterns ",
"version": "0.0.11",
"version": "0.0.12",
"author": "Steve King <[email protected]>",
"contributors": [
{
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,26 @@ var readdir = require('../lib/readdir');
});

}());

(function() {
var readdir = require('../lib/readdir.js'),
read = readdir.read;

read('./example_dir/missing', readdir.IGNORE_ERRORS, function (error, everyFile) {
process.nextTick(function () {
Assert.equal(error, null, 'Should not have thrown an error while scanning non-existent directory');
Assert.deepEqual(everyFile, [], 'No files given that the directory does not exist');
});
});

}());

(function() {
var readdir = require('../lib/readdir.js'),
read = readdir.readSync;

Assert.deepEqual(
read('./example_dir/missing', null, readdir.IGNORE_ERRORS), [],
'No files given that the directory does not exist');

}());

0 comments on commit 44e144b

Please sign in to comment.