From 62263cd94b28f4fe4f4ca417c1bef2eddcdd7f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20Zag=C3=B3rski?= Date: Tue, 18 Oct 2016 09:23:23 +0200 Subject: [PATCH 1/3] Support async transformFn when it returns promise --- README.md | 8 ++++---- src/file-stream.js | 11 ++++++++--- src/plugin-stream.js | 17 ++++++++++------- src/transform.js | 11 ++++++----- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e4007bb..37802e3 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ contents and returns the desired contents. pipelines in both buffer mode and streaming mode. * **Economical**. Reduce the need for gulp-specific plugins by pairing gulp-transform with ordinary node packages and functions. - +* **Async**-ready. Transform is supported by returning `Promise` from transform function. ## Install Install via [npm][NPM link]: @@ -89,9 +89,9 @@ gulp.task('cheerio', function() { ##### transformFn `function` -The callback responsible for the transformation. The return value must be a -string or a Buffer, which will replace the file's contents. The callback -is invoked once per file with the following arguments: +The callback responsible for the transformation. The return value must be value or +Promise resolvable to string or a Buffer, which will replace the file's contents. +The callbackis invoked once per file with the following arguments: * **contents** `Buffer` | `string`
The initial contents of the file. Contents are passed as a Buffer unless the diff --git a/src/file-stream.js b/src/file-stream.js index d2250d6..e495b04 100644 --- a/src/file-stream.js +++ b/src/file-stream.js @@ -19,9 +19,14 @@ export class FileStream extends Transform { _flush(done) { let contents = Buffer.concat(this.data); - this.push(transform(this.fn, contents, this.file, this.opts)); - - done(); + transform(this.fn, contents, this.file, this.opts) + .then(function(contents) { + this.push(contents); + done(); + }) + .catch(function(error) { + done(error) + }) } } diff --git a/src/plugin-stream.js b/src/plugin-stream.js index 2fc649e..28b5f49 100644 --- a/src/plugin-stream.js +++ b/src/plugin-stream.js @@ -15,14 +15,17 @@ export class PluginStream extends Transform { let {fn, opts} = this; if (file.isBuffer()) { - file.contents = transform(fn, file.contents, file, opts); - } - - if (file.isStream()) { + transform(fn, file.contents, file, opts) + .then(function(contents) { + file.contents = contents; + next(null, file) + }) + .catch(function(error) { + next(error); + }) + } else if (file.isStream()) { file.contents = file.contents.pipe(new FileStream(fn, file, opts)); + next(null, file); } - - next(null, file); } - } diff --git a/src/transform.js b/src/transform.js index fe09458..323a3dc 100644 --- a/src/transform.js +++ b/src/transform.js @@ -3,9 +3,10 @@ import {err} from './err'; export function transform(fn, contents, file, opts) { let encoded = opts.encoding ? contents.toString(opts.encoding) : contents; - let transformed = fn.call(opts.thisArg, encoded, file); - - return isBuffer(transformed) ? transformed : - isString(transformed) ? new Buffer(transformed) : - err('transformFn must return a string or a Buffer'); + return Promise.resolve(fn.call(opts.thisArg, encoded, file)) + .then(function(transformed) { + return isBuffer(transformed) ? transformed : + isString(transformed) ? new Buffer(transformed) : + err('transformFn must return a string or a Buffer'); + }) } From 259885ca6c0482f2a2e28c5084f2c9f0bea218eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20Zag=C3=B3rski?= Date: Tue, 18 Oct 2016 16:53:22 +0200 Subject: [PATCH 2/3] Fix file-stream not forwarding promise result. --- src/file-stream.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/file-stream.js b/src/file-stream.js index e495b04..a6ae06c 100644 --- a/src/file-stream.js +++ b/src/file-stream.js @@ -18,10 +18,11 @@ export class FileStream extends Transform { } _flush(done) { + let self = this; let contents = Buffer.concat(this.data); transform(this.fn, contents, this.file, this.opts) .then(function(contents) { - this.push(contents); + self.push(contents); done(); }) .catch(function(error) { From f78baac3823f6dcad4f8d27f4b03a69c41aa1fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20Zag=C3=B3rski?= Date: Wed, 19 Oct 2016 10:58:05 +0200 Subject: [PATCH 3/3] test fix: 'returns neither a string nor a Buffer' test now supports async error --- test/index.coffee | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/index.coffee b/test/index.coffee index 17cb3cb..49c5364 100644 --- a/test/index.coffee +++ b/test/index.coffee @@ -32,8 +32,13 @@ describe 'plugin: gulp-transform', -> context 'returns neither a string nor a Buffer', -> - it 'throws PluginError', -> - err -> transform((content) -> 42).write buffered() + it 'throws PluginError', (done) -> + t = transform((content) -> 42) + t.on 'error', (err) -> + done() + t.on 'data', () -> + done new Error('expected PluginError') + t.write buffered() context 'returns a Buffer or string', -> [fn, file] = [null, null]