UNPKG

3.83 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var gulp_util_1 = require("gulp-util");
5var common_1 = require("./common");
6/**
7 * Creates a function for applying a user-defined transformation to
8 * file contents.
9 *
10 * @internal
11 */
12var ContentTransformer = (function () {
13 function ContentTransformer(callback, encoding, thisArg) {
14 this.callback = callback;
15 this.encoding = encoding;
16 this.thisArg = thisArg;
17 }
18 ContentTransformer.fromConfig = function (config) {
19 var callback = config.callback, encoding = config.encoding, thisArg = config.thisArg;
20 return new this(callback, encoding, thisArg);
21 };
22 ContentTransformer.prototype.makeTransformFunction = function () {
23 var _this = this;
24 return function (contents, file) { return _this.transform(contents, file); };
25 };
26 ContentTransformer.prototype.transform = function (contents, file) {
27 return tslib_1.__awaiter(this, void 0, void 0, function () {
28 var decodedContents, callbackResult;
29 return tslib_1.__generator(this, function (_a) {
30 switch (_a.label) {
31 case 0:
32 decodedContents = this.decodeContents(contents);
33 return [4 /*yield*/, this.invokeAndValidate(decodedContents, file)];
34 case 1:
35 callbackResult = _a.sent();
36 return [2 /*return*/, new Buffer(callbackResult, this.encoding)];
37 }
38 });
39 });
40 };
41 ContentTransformer.prototype.decodeContents = function (contents) {
42 if (common_1.isString(this.encoding))
43 return contents.toString(this.encoding);
44 return contents;
45 };
46 ContentTransformer.prototype.invokeAndValidate = function (decodedContents, file) {
47 return tslib_1.__awaiter(this, void 0, void 0, function () {
48 var callbackResult;
49 return tslib_1.__generator(this, function (_a) {
50 switch (_a.label) {
51 case 0: return [4 /*yield*/, this.tryInvokeCallback(decodedContents, file)];
52 case 1:
53 callbackResult = _a.sent();
54 if (this.encoding && !common_1.isString(callbackResult))
55 throw new TypeError("callback result must be a string when encoding is given");
56 if (!this.encoding && !Buffer.isBuffer(callbackResult))
57 throw new TypeError("callback result must be a Buffer when encoding is not given");
58 return [2 /*return*/, callbackResult];
59 }
60 });
61 });
62 };
63 ContentTransformer.prototype.tryInvokeCallback = function (decodedContents, file) {
64 return tslib_1.__awaiter(this, void 0, void 0, function () {
65 var error_1;
66 return tslib_1.__generator(this, function (_a) {
67 switch (_a.label) {
68 case 0:
69 _a.trys.push([0, 2, , 3]);
70 return [4 /*yield*/, this.callback.call(this.thisArg, decodedContents, file)];
71 case 1: return [2 /*return*/, _a.sent()];
72 case 2:
73 error_1 = _a.sent();
74 // Show stack for errors in callback as message alone may not be descriptive enough.
75 throw new gulp_util_1.PluginError(common_1.PLUGIN_NAME, error_1, { showStack: true });
76 case 3: return [2 /*return*/];
77 }
78 });
79 });
80 };
81 return ContentTransformer;
82}());
83exports.ContentTransformer = ContentTransformer;
84//# sourceMappingURL=ContentTransformer.js.map
\No newline at end of file