[Pkg-javascript-commits] [node-findit] 01/03: Imported Upstream version 2.0.0

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Tue Jul 1 15:51:51 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to branch master
in repository node-findit.

commit 16ac946b4c1531c439d07fe96c918bdf14466414
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Tue Jul 1 15:49:53 2014 +0000

    Imported Upstream version 2.0.0
---
 index.js         | 91 ++++++++++++++++++++++++++++++++++++++++++++------------
 package.json     |  2 +-
 readme.markdown  |  7 +++++
 test/err.js      | 12 ++++++++
 test/symlinks.js | 17 +++++++++--
 5 files changed, 107 insertions(+), 22 deletions(-)

diff --git a/index.js b/index.js
index 33f4468..d5254d2 100644
--- a/index.js
+++ b/index.js
@@ -17,7 +17,6 @@ module.exports = function walk (dir, opts, emitter, dstat) {
         emitter._pending = 0;
         emitter._seen = {};
     }
-    emitter._pending ++;
     
     if (dstat) {
         var stopped = false;
@@ -25,28 +24,56 @@ module.exports = function walk (dir, opts, emitter, dstat) {
             stopped = true;
         });
         emitter.emit('path', fdir, dstat);
-        if (!stopped) fs.readdir(dir, onreaddir);
+        if (!stopped) {
+            emitter._pending ++;
+            fs.readdir(dir, function (err, files) {
+                emitter._pending --;
+                onreaddir(dir, err, files);
+            });
+        }
         else check()
     }
-    else fs.lstat(dir, function onstat (err, stat) {
+    else {
+        emitter._pending ++;
+        fs.lstat(dir, function (err, stat) {
+            emitter._pending --;
+            onlstat(err, stat);
+        });
+    }
+    function onlstat (err, stat) {
         if (emitter._stopped) return;
-        if (err) return finish();
+        
+        if (err) {
+            if (!err.path) err.path = dir;
+            emitter.emit('error', err);
+            return check();
+        }
         emitter._seen[stat.ino || dir] = true;
         
         if (stat.isSymbolicLink() && opts.followSymlinks) {
             emitter.emit('link', fdir, stat);
+            emitter._pending ++;
             fs.readlink(dir, function (err, rfile) {
                 if (emitter._stopped) return;
-                if (err) return finish();
+                emitter._pending --;
+                if (err) {
+                    if (!err.path) err.path = dir;
+                    emitter.emit('error', err);
+                    return check();
+                }
                 var file_ = path.resolve(dir, rfile);
                 emitter.emit('readlink', fdir, file_);
-                fs.lstat(file_, onstat);
+                emitter._pending ++;
+                fs.lstat(file_, function (err, stat) {
+                    emitter._pending --;
+                    onstat(err, stat);
+                });
             });
         }
         else if (stat.isSymbolicLink()) {
             emitter.emit('link', fdir, stat);
             emitter.emit('path', fdir, stat);
-            finish();
+            check();
         }
         else if (stat.isDirectory()) {
             var stopped = false;
@@ -54,38 +81,55 @@ module.exports = function walk (dir, opts, emitter, dstat) {
                 stopped = true;
             });
             emitter.emit('path', fdir, stat);
-            if (!stopped) fs.readdir(dir, onreaddir);
+            if (!stopped) {
+                emitter._pending ++;
+                fs.readdir(dir, function (err, files) {
+                    emitter._pending --;
+                    onreaddir(dir, err, files);
+                });
+            }
             else check()
         }
         else {
             emitter.emit('file', fdir, stat);
             emitter.emit('path', fdir, stat);
-            finish();
+            check();
         }
-    });
+    }
     
     return emitter;
     
     function check () {
-        if (-- emitter._pending === 0) finish();
+        if (emitter._pending === 0) finish();
     }
     
     function finish () {
         emitter.emit('end');
-        emitter._seen = null;
+        emitter._stopped = true;
     }
     
-    function onreaddir (err, files) {
+    function onreaddir (dir, err, files) {
         if (emitter._stopped) return;
-        if (err) return check();
+        
+        if (err) {
+            if (!err.path) err.path = dir;
+            emitter.emit('error', err);
+            return check();
+        }
         
         files.forEach(function (rfile) {
-            emitter._pending ++;
             var file = path.join(fdir, rfile);
             
+            emitter._pending ++;
             fs.lstat(file, function (err, stat) {
+                emitter._pending --;
+                
                 if (emitter._stopped) return;
-                if (err) check()
+                if (err) {
+                    if (!err.path) err.path = file;
+                    emitter.emit('error', err);
+                    check()
+                }
                 else onstat(file, stat)
             });
         });
@@ -106,15 +150,24 @@ module.exports = function walk (dir, opts, emitter, dstat) {
             
             fs.readlink(file, function (err, rfile) {
                 if (emitter._stopped) return;
-                if (err) return check();
+                if (err) {
+                    if (!err.path) err.path = file;
+                    emitter.emit('error', err);
+                    return check();
+                }
                 var file_ = path.resolve(path.dirname(file), rfile);
                 
                 emitter.emit('readlink', file, file_);
+                emitter._pending ++;
                 fs.lstat(file_, function (err, stat_) {
+                    emitter._pending --;
                     if (emitter._stopped) return;
-                    if (err) return check();
+                    if (err) {
+                        if (!err.path) err.path = file_;
+                        emitter.emit('error', err);
+                        return check();
+                    }
                     
-                    emitter._pending ++;
                     onstat(file_, stat_, file);
                     check();
                 });
diff --git a/package.json b/package.json
index fd0d623..c1d9d75 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "findit",
-  "version": "1.2.0",
+  "version": "2.0.0",
   "description": "walk a directory tree recursively with events",
   "main": "index.js",
   "devDependencies": {
diff --git a/readme.markdown b/readme.markdown
index 62a1ec1..c31588b 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -90,6 +90,13 @@ event fires.
 
 When `finder.stop()` is called, this event fires.
 
+## finder.on('error', function (err) {})
+
+Whenever there is an error, this event fires. You can choose to ignore errors or
+stop the traversal using `finder.stop()`.
+
+You can always get the source of the error by checking `err.path`.
+
 # install
 
 With [npm](https://npmjs.org) do:
diff --git a/test/err.js b/test/err.js
new file mode 100644
index 0000000..57ee575
--- /dev/null
+++ b/test/err.js
@@ -0,0 +1,12 @@
+var find = require('../');
+var test = require('tap').test;
+var path = require('path');
+
+test('error', function (t) {
+    t.plan(1);
+    
+    var finder = find(__dirname + '/does/not/exist');
+    finder.on('error', function (err) {
+        t.equal(err.path, __dirname + '/does/not/exist');
+    });
+});
diff --git a/test/symlinks.js b/test/symlinks.js
index ec393b4..1f980d5 100644
--- a/test/symlinks.js
+++ b/test/symlinks.js
@@ -6,6 +6,7 @@ function helper(t, dir, options, callback) {
     var symlinks = [];
     var files = [];
     var dirs = [];
+    var errors = [];
     
     var finder = findit(dir, options);
     
@@ -25,7 +26,7 @@ function helper(t, dir, options, callback) {
     });
     
     finder.on('error', function (err) {
-        t.fail(err);
+        errors.push(err);
     });
     
     finder.on('end', function () {
@@ -33,13 +34,19 @@ function helper(t, dir, options, callback) {
         files.sort();
         dirs.sort();
         
-        callback({ symlinks: symlinks, files: files, dirs: dirs });
+        callback({
+            errors: errors,
+            symlinks: symlinks,
+            files: files,
+            dirs: dirs
+        });
     });
 }
 
 test('links', function (t) {
     helper(t, __dirname + '/symlinks/dir1', { followSymlinks: false }, done);
     function done (data) {
+        t.deepEqual(data.errors, []);
         t.deepEqual(data.symlinks, [
             'dangling-symlink', 'link-to-dir2', 'link-to-file'
         ]);
@@ -53,6 +60,12 @@ test('follow links', function (t) {
     helper(t, __dirname + '/symlinks/dir1', { followSymlinks: true }, done);
     
     function done (data) {
+        t.equal(data.errors.length, 1);
+        t.equal(
+            data.errors[0].path, __dirname
+            + '/symlinks/dir1/does-not-exist'
+        );
+        
         t.deepEqual(data.symlinks, [
             'cyclic-link-to-dir1', 'dangling-symlink', 'link-to-dir2',
             'link-to-file'

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-findit.git



More information about the Pkg-javascript-commits mailing list