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