1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Report = exports.isReportError = exports.ReportError = void 0;
|
4 | const stream_1 = require("stream");
|
5 | const string_decoder_1 = require("string_decoder");
|
6 | const MessageName_1 = require("./MessageName");
|
7 | class ReportError extends Error {
|
8 | constructor(code, message, reportExtra) {
|
9 | super(message);
|
10 | this.reportExtra = reportExtra;
|
11 | this.reportCode = code;
|
12 | }
|
13 | }
|
14 | exports.ReportError = ReportError;
|
15 | function isReportError(error) {
|
16 | return typeof error.reportCode !== `undefined`;
|
17 | }
|
18 | exports.isReportError = isReportError;
|
19 | class Report {
|
20 | constructor() {
|
21 | this.reportedInfos = new Set();
|
22 | this.reportedWarnings = new Set();
|
23 | this.reportedErrors = new Set();
|
24 | }
|
25 | static progressViaCounter(max) {
|
26 | let current = 0;
|
27 | let unlock;
|
28 | let lock = new Promise(resolve => {
|
29 | unlock = resolve;
|
30 | });
|
31 | const set = (n) => {
|
32 | const thisUnlock = unlock;
|
33 | lock = new Promise(resolve => {
|
34 | unlock = resolve;
|
35 | });
|
36 | current = n;
|
37 | thisUnlock();
|
38 | };
|
39 | const tick = (n = 0) => {
|
40 | set(current + 1);
|
41 | };
|
42 | const gen = (async function* () {
|
43 | while (current < max) {
|
44 | await lock;
|
45 | yield {
|
46 | progress: current / max,
|
47 | };
|
48 | }
|
49 | })();
|
50 | return {
|
51 | [Symbol.asyncIterator]() {
|
52 | return gen;
|
53 | },
|
54 | set,
|
55 | tick,
|
56 | };
|
57 | }
|
58 | reportInfoOnce(name, text, opts) {
|
59 | const key = opts && opts.key ? opts.key : text;
|
60 | if (!this.reportedInfos.has(key)) {
|
61 | this.reportedInfos.add(key);
|
62 | this.reportInfo(name, text);
|
63 | }
|
64 | }
|
65 | reportWarningOnce(name, text, opts) {
|
66 | const key = opts && opts.key ? opts.key : text;
|
67 | if (!this.reportedWarnings.has(key)) {
|
68 | this.reportedWarnings.add(key);
|
69 | this.reportWarning(name, text);
|
70 | }
|
71 | }
|
72 | reportErrorOnce(name, text, opts) {
|
73 | var _a;
|
74 | const key = opts && opts.key ? opts.key : text;
|
75 | if (!this.reportedErrors.has(key)) {
|
76 | this.reportedErrors.add(key);
|
77 | this.reportError(name, text);
|
78 | (_a = opts === null || opts === void 0 ? void 0 : opts.reportExtra) === null || _a === void 0 ? void 0 : _a.call(opts, this);
|
79 | }
|
80 | }
|
81 | reportExceptionOnce(error) {
|
82 | if (isReportError(error)) {
|
83 | this.reportErrorOnce(error.reportCode, error.message, { key: error, reportExtra: error.reportExtra });
|
84 | }
|
85 | else {
|
86 | this.reportErrorOnce(MessageName_1.MessageName.EXCEPTION, error.stack || error.message, { key: error });
|
87 | }
|
88 | }
|
89 | createStreamReporter(prefix = null) {
|
90 | const stream = new stream_1.PassThrough();
|
91 | const decoder = new string_decoder_1.StringDecoder();
|
92 | let buffer = ``;
|
93 | stream.on(`data`, chunk => {
|
94 | let chunkStr = decoder.write(chunk);
|
95 | let lineIndex;
|
96 | do {
|
97 | lineIndex = chunkStr.indexOf(`\n`);
|
98 | if (lineIndex !== -1) {
|
99 | const line = buffer + chunkStr.substr(0, lineIndex);
|
100 | chunkStr = chunkStr.substr(lineIndex + 1);
|
101 | buffer = ``;
|
102 | if (prefix !== null) {
|
103 | this.reportInfo(null, `${prefix} ${line}`);
|
104 | }
|
105 | else {
|
106 | this.reportInfo(null, line);
|
107 | }
|
108 | }
|
109 | } while (lineIndex !== -1);
|
110 | buffer += chunkStr;
|
111 | });
|
112 | stream.on(`end`, () => {
|
113 | const last = decoder.end();
|
114 | if (last !== ``) {
|
115 | if (prefix !== null) {
|
116 | this.reportInfo(null, `${prefix} ${last}`);
|
117 | }
|
118 | else {
|
119 | this.reportInfo(null, last);
|
120 | }
|
121 | }
|
122 | });
|
123 | return stream;
|
124 | }
|
125 | }
|
126 | exports.Report = Report;
|