diff --git a/.gitignore b/.gitignore index fd4f2b0..10d9d84 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules .DS_Store +.idea/ diff --git a/index.js b/index.js index 23478d3..35de9a3 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,25 @@ function toMatcherFunction(ignoreEntry) { } } +function _getFileStat(filePath, callback) { + fs.stat(filePath, function (err, stats) { + if (err) { + if (err.code === 'ENOENT') { + fs.lstat(filePath, function (_err, _stats) { + if (_err) { + return callback(_err); + } + return callback(null, _stats); + }); + } else { + return callback(err); + } + } + + return callback(null, stats); + }); +} + function readdir(path, ignores, callback) { if (typeof ignores == "function") { callback = ignores; @@ -52,7 +71,7 @@ function readdir(path, ignores, callback) { files.forEach(function(file) { var filePath = p.join(path, file); - fs.stat(filePath, function(_err, stats) { + _getFileStat(filePath, function(_err, stats) { if (_err) { return callback(_err); } @@ -69,7 +88,7 @@ function readdir(path, ignores, callback) { return null; } - if (stats.isDirectory()) { + if (stats && stats.isDirectory && stats.isDirectory()) { readdir(filePath, ignores, function(__err, res) { if (__err) { return callback(__err); diff --git a/test/recursive-readdir-test.js b/test/recursive-readdir-test.js index 6fd79ee..fba58a1 100644 --- a/test/recursive-readdir-test.js +++ b/test/recursive-readdir-test.js @@ -361,6 +361,22 @@ describe("readdir", function() { }); }); + it("does not fail if the symlink is broken and returns the symlink in file list", function(done) { + var expectedFiles = getAbsolutePaths([ + "/testsymlinks/testbroken/file.dat.alias" + ]); + + readdir(p.join(__dirname, "testsymlinks", "testbroken"), function(err, list) { + assert.ifError(err); + assert.deepEqual( + list.sort(), + expectedFiles, + "Failed to find expected files." + ); + done(); + }); + }); + if (!global.Promise) { console.log("Native Promise not supported - skipping tests"); } else { diff --git a/test/testsymlinks/testbroken/file.dat.alias b/test/testsymlinks/testbroken/file.dat.alias new file mode 100644 index 0000000..3c51ce4 Binary files /dev/null and b/test/testsymlinks/testbroken/file.dat.alias differ