Repository: jamesramsay/hercule
Pull request #195 (2016-03-12 01:04:38)

Breaking: follow async 'error-first, single-parameter' convention

codecov-io: ## [Current coverage][1] is `99.06%`
>Branch **#195** has no coverage reports uploaded yet.


> No diff could be generated. No reports for `next` found.



[1]: https://codecov.io/github/jamesramsay/hercule?ref=3c61f1911e77253f6dfc197266a4d11a218fd368
[2]: https://codecov.io/github/jamesramsay/hercule/features/suggestions?ref=3c61f1911e77253f6dfc197266a4d11a218fd368
[3]: https://codecov.io/github/jamesramsay/hercule/commit/3c61f1911e77253f6dfc197266a4d11a218fd368


> Powered by [Codecov](https://codecov.io). Updated on successful CI builds.

Patch:
diff --git a/README.md b/README.md
index 2b98461..d413ca9 100644
--- a/README.md
+++ b/README.md
@@ -242,7 +242,7 @@ __Examples__
 // async
 import { trancludeString } from 'hercule';
 
-trancludeString(':[foo](bar.md)', (output) => {
+trancludeString(':[foo](bar.md)', (err, output) => {
   console.log(output);
 });
 
@@ -284,7 +284,7 @@ __Examples__
 // async
 import { trancludeFile } from 'hercule';
 
-trancludeFileSync('foo.md', (output) => {
+trancludeFileSync('foo.md', (err, output) => {
   console.log(output);
 });
 
diff --git a/src/hercule.js b/src/hercule.js
index 5bfa8b1..7946283 100644
--- a/src/hercule.js
+++ b/src/hercule.js
@@ -32,6 +32,8 @@ export function transcludeString(...args) {
   const transclude = new Transcluder(options, log);
   let outputString = '';
 
+  transclude.on('error', (err) => cb(err));
+
   transclude.on('readable', function read() {
     let content = null;
     while ((content = this.read()) !== null) {
@@ -39,7 +41,7 @@ export function transcludeString(...args) {
     }
   });
 
-  transclude.on('end', () => cb(outputString));
+  transclude.on('end', () => cb(null, outputString));
 
   transclude.write(input, 'utf8');
   transclude.end();
@@ -55,6 +57,10 @@ export function transcludeFile(...args) {
   const inputStream = fs.createReadStream(input, { encoding: 'utf8' });
   let outputString = '';
 
+  inputStream.on('error', (err) => cb(err));
+
+  transclude.on('error', (err) => cb(err));
+
   transclude.on('readable', function read() {
     let content = null;
     while ((content = this.read()) !== null) {
@@ -62,7 +68,7 @@ export function transcludeFile(...args) {
     }
   });
 
-  transclude.on('end', () => cb(outputString));
+  transclude.on('end', () => cb(null, outputString));
 
   inputStream.pipe(transclude);
 }


Test patch:
diff --git a/test/integration/transcludeFile.js b/test/integration/transcludeFile.js
index 18306ee..00ca453 100644
--- a/test/integration/transcludeFile.js
+++ b/test/integration/transcludeFile.js
@@ -12,7 +12,8 @@ _.forEach((fixtures.fixtures), (fixture) => {
       relativePath: path.resolve(__dirname, '../fixtures', fixture.name),
     };
 
-    transcludeFile(fixture.inputFile, options, (output) => {
+    transcludeFile(fixture.inputFile, options, (err, output) => {
+      t.same(err, null);
       t.same(output, fixture.expectedOutput);
       t.end();
     });
diff --git a/test/integration/transcludeString.js b/test/integration/transcludeString.js
index fce9758..6ec3602 100644
--- a/test/integration/transcludeString.js
+++ b/test/integration/transcludeString.js
@@ -13,7 +13,8 @@ _.forEach((fixtures.fixtures), (fixture) => {
       relativePath: path.resolve(__dirname, '../fixtures', fixture.name),
     };
 
-    transcludeString(fixture.input, options, (output) => {
+    transcludeString(fixture.input, options, (err, output) => {
+      t.same(err, null);
       t.same(output, fixture.expectedOutput);
       t.end();
     });
diff --git a/test/units/transcludeFile.js b/test/units/transcludeFile.js
index 3dd7de1..36fdd07 100644
--- a/test/units/transcludeFile.js
+++ b/test/units/transcludeFile.js
@@ -6,7 +6,8 @@ import { transcludeFile } from '../../lib/hercule';
 test.cb('should transclude with only required arguments', (t) => {
   const input = path.join(__dirname, '../fixtures/no-link/index.md');
   const expected = 'The quick brown fox jumps over the lazy dog.\n';
-  transcludeFile(input, (output) => {
+  transcludeFile(input, (err, output) => {
+    t.same(err, null);
     t.same(output, expected);
     t.end();
   });
@@ -15,7 +16,8 @@ test.cb('should transclude with only required arguments', (t) => {
 test.cb('should transclude with optional relativePath argument', (t) => {
   const input = path.join(__dirname, '../fixtures/no-link/index.md');
   const expected = 'The quick brown fox jumps over the lazy dog.\n';
-  transcludeFile(input, { relativePath: 'test' }, (output) => {
+  transcludeFile(input, { relativePath: 'test' }, (err, output) => {
+    t.same(err, null);
     t.same(output, expected);
     t.end();
   });
@@ -28,8 +30,18 @@ test.cb('should transclude with optional log handler', (t) => {
     error: () => t.fail(),
     warn: () => t.fail(),
   };
-  transcludeFile(input, null, logger, (output) => {
+  transcludeFile(input, null, logger, (err, output) => {
+    t.same(err, null);
     t.same(output, expected);
     t.end();
   });
 });
+
+test.cb('should return error if file doesn\'t exist', (t) => {
+  const input = path.join('i-dont-exist.md');
+  transcludeFile(input, null, (err, output) => {
+    t.ok(err);
+    t.notOk(output);
+    t.end();
+  });
+});
diff --git a/test/units/transcludeString.js b/test/units/transcludeString.js
index 6e3b494..82551b0 100644
--- a/test/units/transcludeString.js
+++ b/test/units/transcludeString.js
@@ -5,7 +5,8 @@ import { transcludeString } from '../../lib/hercule';
 test.cb('should transclude with only required arguments', (t) => {
   const input = 'The quick brown fox jumps over the lazy dog.';
   const expected = 'The quick brown fox jumps over the lazy dog.';
-  transcludeString(input, (output) => {
+  transcludeString(input, (err, output) => {
+    t.same(err, null);
     t.same(output, expected);
     t.end();
   });
@@ -14,7 +15,8 @@ test.cb('should transclude with only required arguments', (t) => {
 test.cb('should transclude with optional relativePath argument', (t) => {
   const input = 'The quick brown fox jumps over the lazy dog.';
   const expected = 'The quick brown fox jumps over the lazy dog.';
-  transcludeString(input, { relativePath: 'test' }, (output) => {
+  transcludeString(input, { relativePath: 'test' }, (err, output) => {
+    t.same(err, null);
     t.same(output, expected);
     t.end();
   });
@@ -27,7 +29,8 @@ test.cb('should transclude with optional log handler', (t) => {
     error: () => t.fail(),
     warn: () => t.fail(),
   };
-  transcludeString(input, null, logger, (output) => {
+  transcludeString(input, null, logger, (err, output) => {
+    t.same(err, null);
     t.same(output, expected);
     t.end();
   });