Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support async transformFn when it returns Promise #2

Merged
merged 3 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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` <br>
The initial contents of the file. Contents are passed as a Buffer unless the
Expand Down
12 changes: 9 additions & 3 deletions src/file-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ export class FileStream extends Transform {
}

_flush(done) {
let self = this;
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) {
self.push(contents);
done();
})
.catch(function(error) {
done(error)
})
}

}
17 changes: 10 additions & 7 deletions src/plugin-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
11 changes: 6 additions & 5 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})
}
9 changes: 7 additions & 2 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down