gulp-transform
Advanced tools
Comparing version 1.0.8 to 1.1.0
@@ -26,3 +26,3 @@ 'use strict'; | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(FileStream).call(this)); | ||
var _this = _possibleConstructorReturn(this, (FileStream.__proto__ || Object.getPrototypeOf(FileStream)).call(this)); | ||
@@ -45,6 +45,12 @@ _this.fn = transformFn; | ||
value: function _flush(done) { | ||
var _this2 = this; | ||
var contents = Buffer.concat(this.data); | ||
this.push((0, _transform2.transform)(this.fn, contents, this.file, this.opts)); | ||
done(); | ||
(0, _transform2.transform)(this.fn, contents, this.file, this.opts).then(function (result) { | ||
_this2.push(result); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
} | ||
@@ -51,0 +57,0 @@ }]); |
@@ -26,7 +26,7 @@ 'use strict'; | ||
function PluginStream(transformFn) { | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
_classCallCheck(this, PluginStream); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(PluginStream).call(this, { objectMode: true })); | ||
var _this = _possibleConstructorReturn(this, (PluginStream.__proto__ || Object.getPrototypeOf(PluginStream)).call(this, { objectMode: true })); | ||
@@ -46,10 +46,14 @@ _this.fn = transformFn; | ||
if (file.isBuffer()) { | ||
file.contents = (0, _transform2.transform)(fn, file.contents, file, opts); | ||
} | ||
if (file.isStream()) { | ||
(0, _transform2.transform)(fn, file.contents, file, opts).then(function (result) { | ||
file.contents = result; | ||
next(null, file); | ||
}).catch(function (err) { | ||
next(err); | ||
}); | ||
} else if (file.isStream()) { | ||
file.contents = file.contents.pipe(new _fileStream.FileStream(fn, file, opts)); | ||
next(null, file); | ||
} else { | ||
next(null, file); | ||
} | ||
next(null, file); | ||
} | ||
@@ -56,0 +60,0 @@ }]); |
@@ -8,2 +8,4 @@ 'use strict'; | ||
var _es6Promise = require('es6-promise'); | ||
var _lodash = require('lodash'); | ||
@@ -13,7 +15,20 @@ | ||
function transform(fn, contents, file, opts) { | ||
var encoded = opts.encoding ? contents.toString(opts.encoding) : contents; | ||
var transformed = fn.call(opts.thisArg, encoded, file); | ||
function transform(fn, contents, file, _ref) { | ||
var encoding = _ref.encoding; | ||
var thisArg = _ref.thisArg; | ||
return (0, _lodash.isBuffer)(transformed) ? transformed : (0, _lodash.isString)(transformed) ? new Buffer(transformed) : (0, _err.err)('transformFn must return a string or a Buffer'); | ||
var decoded = encoding ? contents.toString(encoding) : contents; | ||
var transformed = fn.call(thisArg, decoded, file); | ||
return _es6Promise.Promise.resolve(transformed).then(toBuffer); | ||
} | ||
function toBuffer(contents) { | ||
if ((0, _lodash.isBuffer)(contents)) { | ||
return contents; | ||
} else if (contents != null) { | ||
return new Buffer(String(contents)); | ||
} else { | ||
(0, _err.err)('transformFn may not return or resolve to null or undefined'); | ||
} | ||
} |
{ | ||
"name": "gulp-transform", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "Perform arbitrary transformations on files", | ||
@@ -41,2 +41,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"es6-promise": "^4.0.5", | ||
"gulp-util": "^3.0.7", | ||
@@ -53,6 +54,6 @@ "lodash": "^4.13.1" | ||
"coveralls": "^2.11.9", | ||
"eslint": "^2.11.1", | ||
"eslint": "^2.13.1", | ||
"event-stream": "^3.3.2", | ||
"istanbul": "1.0.0-alpha.2", | ||
"mocha": "^2.5.3", | ||
"istanbul": "1.1.0-alpha.1", | ||
"mocha": "^3.1.2", | ||
"rimraf": "^2.5.2", | ||
@@ -59,0 +60,0 @@ "sinon": "^1.17.4", |
@@ -13,9 +13,2 @@ # gulp-transform | ||
* **Simple**. Just pass a callback function that takes the current file | ||
contents and returns the desired contents. | ||
* **Flexible**. Receive file contents as a Buffer or a string. Compatible with | ||
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. | ||
## Install | ||
@@ -93,5 +86,6 @@ | ||
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, whose return value will replace | ||
the file's contents. The return value may be a string, a Buffer, or a Promise | ||
resolvable to a string or Buffer. The callback is invoked once per file with the | ||
following arguments: | ||
@@ -98,0 +92,0 @@ * **contents** `Buffer` | `string` <br> |
Sorry, the diff of this file is not supported yet
139
14293
4
9
134
+ Addedes6-promise@^4.0.5
+ Addedes6-promise@4.2.8(transitive)