1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var gulp_util_1 = require("gulp-util");
|
5 | var common_1 = require("./common");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var 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 , this.invokeAndValidate(decodedContents, file)];
|
34 | case 1:
|
35 | callbackResult = _a.sent();
|
36 | return [2 , 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 , 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 , 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 , this.callback.call(this.thisArg, decodedContents, file)];
|
71 | case 1: return [2 , _a.sent()];
|
72 | case 2:
|
73 | error_1 = _a.sent();
|
74 |
|
75 | throw new gulp_util_1.PluginError(common_1.PLUGIN_NAME, error_1, { showStack: true });
|
76 | case 3: return [2 ];
|
77 | }
|
78 | });
|
79 | });
|
80 | };
|
81 | return ContentTransformer;
|
82 | }());
|
83 | exports.ContentTransformer = ContentTransformer;
|
84 |
|
\ | No newline at end of file |