1 | (function (factory) {
|
2 | if (typeof module === "object" && typeof module.exports === "object") {
|
3 | var v = factory(require, exports);
|
4 | if (v !== undefined) module.exports = v;
|
5 | }
|
6 | else if (typeof define === "function" && define.amd) {
|
7 | define(["require", "exports", "@theintern/common"], factory);
|
8 | }
|
9 | })(function (require, exports) {
|
10 | "use strict";
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.eventHandler = exports.createEventHandler = void 0;
|
13 | var common_1 = require("@theintern/common");
|
14 | var Reporter = (function () {
|
15 | function Reporter(executor, options) {
|
16 | if (options === void 0) { options = {}; }
|
17 | if (options.output) {
|
18 | this.output = options.output;
|
19 | }
|
20 | if (options.console) {
|
21 | this.console = options.console;
|
22 | }
|
23 | this.executor = executor;
|
24 | this._registerEventHandlers();
|
25 | }
|
26 | Object.defineProperty(Reporter.prototype, "console", {
|
27 | get: function () {
|
28 | if (!this._console) {
|
29 | this._console = getConsole();
|
30 | }
|
31 | return this._console;
|
32 | },
|
33 | set: function (value) {
|
34 | this._console = value;
|
35 | },
|
36 | enumerable: false,
|
37 | configurable: true
|
38 | });
|
39 | Object.defineProperty(Reporter.prototype, "output", {
|
40 | get: function () {
|
41 | if (!this._output) {
|
42 | if (common_1.global.process != null) {
|
43 | return common_1.global.process.stdout;
|
44 | }
|
45 | else {
|
46 | var _console_1 = this.console;
|
47 | this._output = {
|
48 | write: function (chunk, _encoding, callback) {
|
49 | _console_1.log(chunk);
|
50 | callback();
|
51 | },
|
52 | end: function (chunk, _encoding, callback) {
|
53 | _console_1.log(chunk);
|
54 | callback();
|
55 | }
|
56 | };
|
57 | }
|
58 | }
|
59 | return this._output;
|
60 | },
|
61 | set: function (value) {
|
62 | this._output = value;
|
63 | },
|
64 | enumerable: false,
|
65 | configurable: true
|
66 | });
|
67 | Reporter.prototype.formatError = function (error, options) {
|
68 | return this.executor.formatError(error, options);
|
69 | };
|
70 | Reporter.prototype._registerEventHandlers = function () {
|
71 | var _this = this;
|
72 | if (!this._eventHandlers) {
|
73 | return;
|
74 | }
|
75 | var _loop_1 = function (name_1) {
|
76 | this_1.executor.on(name_1, function () {
|
77 | var _a;
|
78 | var args = [];
|
79 | for (var _i = 0; _i < arguments.length; _i++) {
|
80 | args[_i] = arguments[_i];
|
81 | }
|
82 | var handler = _this._eventHandlers[name_1];
|
83 | return (_a = _this)[handler].apply(_a, args);
|
84 | });
|
85 | };
|
86 | var this_1 = this;
|
87 | for (var name_1 in this._eventHandlers) {
|
88 | _loop_1(name_1);
|
89 | }
|
90 | };
|
91 | return Reporter;
|
92 | }());
|
93 | exports.default = Reporter;
|
94 | function createEventHandler() {
|
95 | return function () {
|
96 | function decorate(target, propertyKey, _descriptor) {
|
97 | if (!target.hasOwnProperty('_eventHandlers')) {
|
98 | if (target._eventHandlers != null) {
|
99 | target._eventHandlers = Object.create(target._eventHandlers);
|
100 | }
|
101 | else {
|
102 | target._eventHandlers = {};
|
103 | }
|
104 | }
|
105 | target._eventHandlers[propertyKey] = propertyKey;
|
106 | }
|
107 | return decorate;
|
108 | };
|
109 | }
|
110 | exports.createEventHandler = createEventHandler;
|
111 | exports.eventHandler = createEventHandler();
|
112 | function getConsole() {
|
113 | if (typeof console !== 'undefined') {
|
114 | return console;
|
115 | }
|
116 | return {
|
117 | assert: noop,
|
118 | count: noop,
|
119 | dir: noop,
|
120 | error: noop,
|
121 | exception: noop,
|
122 | info: noop,
|
123 | log: noop,
|
124 | table: noop,
|
125 | time: noop,
|
126 | timeEnd: noop,
|
127 | trace: noop,
|
128 | warn: noop
|
129 | };
|
130 | }
|
131 | function noop() {
|
132 | }
|
133 | });
|
134 |
|
\ | No newline at end of file |