Skip to content

Commit 44e144b

Browse files
committed
Merge pull request #2 from steveukx/ignoreErrors
Don't throw errors when switched off
2 parents 5271bca + e673ac4 commit 44e144b

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

lib/readdir.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
var deferred = Q.defer();
105105

106106
fs.readdir(dir, function (err, contents) {
107-
if (err) deferred.reject(err);
107+
if (err) deferred_error(deferred, err, options);
108108
else if (!contents.length) {
109109
deferred.resolve();
110110
}
@@ -116,7 +116,7 @@
116116
fs.stat(newPath, function (err, stat) {
117117
var isDirectory = stat && stat.isDirectory();
118118

119-
if (err) deferred.reject(err);
119+
if (err) deferred_error(deferred, err, options);
120120
else if (isDirectory) {
121121
if(exports.INCLUDE_DIRECTORIES & options) {
122122
appendTo.push(newPath.substring(prefixLength) + '/');
@@ -141,6 +141,15 @@
141141
return deferred.promise;
142142
}
143143

144+
function deferred_error(deferred, error, options) {
145+
if (exports.IGNORE_ERRORS & options) {
146+
deferred.resolve();
147+
}
148+
else {
149+
deferred.reject(error);
150+
}
151+
}
152+
144153
/**
145154
* Changes the values in the supplied paths array to be absolute URIs
146155
*
@@ -206,7 +215,12 @@
206215
*/
207216
exports.readSync = function(basePath, includeFilters, options) {
208217
var rootDir = basePath.replace(/\/$/, '') + '/';
209-
return apply_filters(basePath, read_dir_sync(rootDir, [], rootDir.length, options), includeFilters, options);
218+
if (!fs.existsSync(rootDir)) {
219+
return [];
220+
}
221+
else {
222+
return apply_filters(basePath, read_dir_sync(rootDir, [], rootDir.length, options), includeFilters, options);
223+
}
210224
};
211225

212226
/**
@@ -291,4 +305,11 @@
291305
*/
292306
exports.NON_RECURSIVE = 32;
293307

308+
/**
309+
* Bitwise option for preventing errors reading directories from aborting the scan whenever possible - includes
310+
* incorrectly rooted relative symlinks and missing root directory.
311+
* @type {number}
312+
*/
313+
exports.IGNORE_ERRORS = 64;
314+
294315
}(typeof module == 'undefined' ? (window.ReadDir = {}) : module.exports));

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "readdir",
33
"description": "Reads a directory and return results with the ability to use Ant style file match patterns ",
4-
"version": "0.0.11",
4+
"version": "0.0.12",
55
"author": "Steve King <[email protected]>",
66
"contributors": [
77
{

test/test.js

+23
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,26 @@ var readdir = require('../lib/readdir');
167167
});
168168

169169
}());
170+
171+
(function() {
172+
var readdir = require('../lib/readdir.js'),
173+
read = readdir.read;
174+
175+
read('./example_dir/missing', readdir.IGNORE_ERRORS, function (error, everyFile) {
176+
process.nextTick(function () {
177+
Assert.equal(error, null, 'Should not have thrown an error while scanning non-existent directory');
178+
Assert.deepEqual(everyFile, [], 'No files given that the directory does not exist');
179+
});
180+
});
181+
182+
}());
183+
184+
(function() {
185+
var readdir = require('../lib/readdir.js'),
186+
read = readdir.readSync;
187+
188+
Assert.deepEqual(
189+
read('./example_dir/missing', null, readdir.IGNORE_ERRORS), [],
190+
'No files given that the directory does not exist');
191+
192+
}());

0 commit comments

Comments
 (0)