Permalink
Browse files

new/bc: done emits error count as first param

  • Loading branch information...
1 parent a254f62 commit 924ac3b7835c43dc5cc391227382dad0d15f860e @isao committed May 2, 2013
Showing with 16 additions and 13 deletions.
  1. +1 −1 examples/copydemo.js
  2. +1 −1 examples/finddemo.js
  3. +4 −2 index.js
  4. +4 −4 tests/counts.js
  5. +3 −3 tests/errors.js
  6. +2 −1 tests/files.js
  7. +1 −1 tests/typer.js
@@ -26,7 +26,7 @@ scan.on('file', function (err, pathname, stat) {
scan.on('error', console.error);
-scan.on('done', function (count) {
+scan.on('done', function (err, count) {
console.log('done. %d things scanned. files copied to %s', count, to);
});
@@ -33,7 +33,7 @@ scan.on('file', function (err, pathname, stat) {
scan.on('error', console.error);
// results
-scan.on('done', function (count) {
+scan.on('done', function (err, count) {
console.log('\nscanned %d things', count);
console.log('found these js files:\n ', found.join('\n '));
});
View
@@ -42,6 +42,7 @@ function arrayify(arg) {
*/
function Scan(ignore, fn) {
this.count = 0;
+ this.errors = 0;
this.ignore = arrayify(ignore);
if ('function' === typeof fn) {
this.typer = fn;
@@ -79,7 +80,7 @@ Scan.prototype.statOne = function(list) {
this.count++;
fs.stat(item, this.getStatCb(item, list));
} else {
- this.emit('done', this.count);
+ this.emit('done', null, this.count);
}
};
@@ -107,6 +108,7 @@ Scan.prototype.getStatCb = function(item, list) {
if (err) {
type = 'error';
+ self.errors++;
} else if (self.ignore.some(String.prototype.match.bind(item))) {
type = 'ignored';
} else {
@@ -123,7 +125,7 @@ Scan.prototype.getStatCb = function(item, list) {
self.statOne(list);
} else {
- self.emit('done', self.count);
+ self.emit('done', self.errors, self.count);
}
};
};
View
@@ -19,7 +19,7 @@ test('done count equals files + dirs + ignored', function (t) {
scan.on('file', inc);
scan.on('other', inc);
scan.on('ignored', inc);
- scan.on('done', function (count) {
+ scan.on('done', function (err, count) {
t.equal(count, items);
});
@@ -36,7 +36,7 @@ test('ignore one', function (t) {
ignored++;
});
- scan.on('done', function (count) {
+ scan.on('done', function (err, count) {
t.equal(ignored, 1);
});
@@ -53,7 +53,7 @@ test('ignore two', function (t) {
ignored++;
});
- scan.on('done', function (count) {
+ scan.on('done', function (err, count) {
t.equal(ignored, 2);
});
@@ -70,7 +70,7 @@ test('counts same for scan.absolutely()', function (t) {
count++;
});
- scan.on('done', function (done_count) {
+ scan.on('done', function (err, done_count) {
t.equal(count, done_count);
});
View
@@ -26,7 +26,7 @@ test('scan.relatively() emits "done", does nothing else', function (t) {
count++;
});
- scan.on('done', function (actual) {
+ scan.on('done', function (err, actual) {
t.same(actual, 0);
t.same(count, undefined);
});
@@ -44,7 +44,7 @@ test('scan.relatively([]) emits "done", does nothing else', function (t) {
count++;
});
- scan.on('done', function (actual) {
+ scan.on('done', function (err, actual) {
t.same(actual, 0);
t.same(count, undefined);
});
@@ -63,7 +63,7 @@ test('statOne w/ empty array', function (t) {
count++;
});
- scan.on('done', function (actual) {
+ scan.on('done', function (err, actual) {
t.equal(actual, 0);
t.same(count, undefined);
});
View
@@ -46,7 +46,8 @@ test('verify params', function (t) {
t.true(stat.isDirectory(), 'it is dir');
});
- scan.on('done', function(count) {
+ scan.on('done', function(err, count) {
+ t.same(err, 0);
t.same(typeof count, 'number');
t.end();
})
View
@@ -26,7 +26,7 @@ test('typer emit if basename contains an "i"', function(t) {
has_i++;
});
- scan.on('done', function(total) {
+ scan.on('done', function(err, total) {
t.equal(has_i, 2);
t.equal(total, items);
});

0 comments on commit 924ac3b

Please sign in to comment.