|
104 | 104 | var deferred = Q.defer();
|
105 | 105 |
|
106 | 106 | fs.readdir(dir, function (err, contents) {
|
107 |
| - if (err) deferred.reject(err); |
| 107 | + if (err) deferred_error(deferred, err, options); |
108 | 108 | else if (!contents.length) {
|
109 | 109 | deferred.resolve();
|
110 | 110 | }
|
|
116 | 116 | fs.stat(newPath, function (err, stat) {
|
117 | 117 | var isDirectory = stat && stat.isDirectory();
|
118 | 118 |
|
119 |
| - if (err) deferred.reject(err); |
| 119 | + if (err) deferred_error(deferred, err, options); |
120 | 120 | else if (isDirectory) {
|
121 | 121 | if(exports.INCLUDE_DIRECTORIES & options) {
|
122 | 122 | appendTo.push(newPath.substring(prefixLength) + '/');
|
|
141 | 141 | return deferred.promise;
|
142 | 142 | }
|
143 | 143 |
|
| 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 | + |
144 | 153 | /**
|
145 | 154 | * Changes the values in the supplied paths array to be absolute URIs
|
146 | 155 | *
|
|
206 | 215 | */
|
207 | 216 | exports.readSync = function(basePath, includeFilters, options) {
|
208 | 217 | 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 | + } |
210 | 224 | };
|
211 | 225 |
|
212 | 226 | /**
|
|
291 | 305 | */
|
292 | 306 | exports.NON_RECURSIVE = 32;
|
293 | 307 |
|
| 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 | + |
294 | 315 | }(typeof module == 'undefined' ? (window.ReadDir = {}) : module.exports));
|
0 commit comments