-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathStreamReport.ts
703 lines (559 loc) Β· 20.7 KB
/
StreamReport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import sliceAnsi from '@arcanis/slice-ansi';
import CI from 'ci-info';
import {Writable} from 'stream';
import {WriteStream} from 'tty';
import {Configuration} from './Configuration';
import {MessageName, stringifyMessageName} from './MessageName';
import {ProgressDefinition, Report, SectionOptions, TimerOptions, ProgressIterable} from './Report';
import {YarnVersion} from './YarnVersion';
import * as formatUtils from './formatUtils';
export type StreamReportOptions = {
configuration: Configuration;
forceSectionAlignment?: boolean;
includeFooter?: boolean;
includeInfos?: boolean;
includeLogs?: boolean;
includeNames?: boolean;
includeVersion?: boolean;
includeWarnings?: boolean;
includePrefix?: boolean;
json?: boolean;
stdout: Writable;
};
export const SINGLE_LINE_CHAR = `Β·`;
const PROGRESS_FRAMES = [`β `, `β `, `β Ή`, `β Έ`, `β Ό`, `β ΄`, `β ¦`, `β §`, `β `, `β `];
const PROGRESS_INTERVAL = 80;
const GROUP = CI.GITHUB_ACTIONS
? {start: (what: string) => `::group::${what}\n`, end: (what: string) => `::endgroup::\n`}
: CI.TRAVIS
? {start: (what: string) => `travis_fold:start:${what}\n`, end: (what: string) => `travis_fold:end:${what}\n`}
: CI.GITLAB
? {start: (what: string) => `section_start:${Math.floor(Date.now() / 1000)}:${what.toLowerCase().replace(/\W+/g, `_`)}[collapsed=true]\r\x1b[0K${what}\n`, end: (what: string) => `section_end:${Math.floor(Date.now() / 1000)}:${what.toLowerCase().replace(/\W+/g, `_`)}\r\x1b[0K`}
: null;
export const SUPPORTS_GROUPS = GROUP !== null;
const now = new Date();
// We only want to support environments that will out-of-the-box accept the
// characters we want to use. Others can enforce the style from the project
// configuration.
const supportsEmojis = [`iTerm.app`, `Apple_Terminal`, `WarpTerminal`, `vscode`].includes(process.env.TERM_PROGRAM!) || !!process.env.WT_SESSION;
const makeRecord = <T>(obj: {[key: string]: T}) => obj;
const PROGRESS_STYLES = makeRecord({
patrick: {
date: [17, 3],
chars: [`π`, `π±`],
size: 40,
},
simba: {
date: [19, 7],
chars: [`π¦`, `π΄`],
size: 40,
},
jack: {
date: [31, 10],
chars: [`π`, `π¦`],
size: 40,
},
hogsfather: {
date: [31, 12],
chars: [`π`, `π`],
size: 40,
},
default: {
chars: [`=`, `-`],
size: 80,
},
});
const defaultStyle = (supportsEmojis && Object.keys(PROGRESS_STYLES).find(name => {
const style = PROGRESS_STYLES[name];
if (style.date && (style.date[0] !== now.getDate() || style.date[1] !== now.getMonth() + 1))
return false;
return true;
})) || `default`;
export function formatName(name: MessageName | null, {configuration, json}: {configuration: Configuration, json: boolean}) {
if (!configuration.get(`enableMessageNames`))
return ``;
const num = name === null ? 0 : name;
const label = stringifyMessageName(num);
if (!json && name === null) {
return formatUtils.pretty(configuration, label, `grey`);
} else {
return label;
}
}
export function formatNameWithHyperlink(name: MessageName | null, {configuration, json}: {configuration: Configuration, json: boolean}) {
const code = formatName(name, {configuration, json});
if (!code)
return code;
// Don't print hyperlinks for the generic messages
if (name === null || name === MessageName.UNNAMED)
return code;
const desc = MessageName[name];
const href = `https://yarnpkg.com/advanced/error-codes#${code}---${desc}`.toLowerCase();
return formatUtils.applyHyperlink(configuration, code, href);
}
/**
* @internal
*/
export async function reportOptionDeprecations({configuration, stdout, forceError}: {configuration: Configuration, stdout: Writable, forceError?: boolean}, checks: Array<{option: unknown, message: string, error?: boolean, callback?: () => void}>) {
const deprecationReport = await StreamReport.start({
configuration,
stdout,
includeFooter: false,
}, async report => {
let hasWarnings = false;
let hasErrors = false;
for (const check of checks) {
if (typeof check.option !== `undefined`) {
if (check.error || forceError) {
hasErrors = true;
report.reportError(MessageName.DEPRECATED_CLI_SETTINGS, check.message);
} else {
hasWarnings = true;
report.reportWarning(MessageName.DEPRECATED_CLI_SETTINGS, check.message);
}
check.callback?.();
}
}
if (hasWarnings && !hasErrors) {
report.reportSeparator();
}
});
if (deprecationReport.hasErrors()) {
return deprecationReport.exitCode();
} else {
return null;
}
}
export class StreamReport extends Report {
static async start(opts: StreamReportOptions, cb: (report: StreamReport) => Promise<void>) {
const report = new this(opts);
const emitWarning = process.emitWarning;
process.emitWarning = (message, name) => {
if (typeof message !== `string`) {
const error = message;
message = error.message;
name = name ?? error.name;
}
const fullMessage = typeof name !== `undefined`
? `${name}: ${message}`
: message;
report.reportWarning(MessageName.UNNAMED, fullMessage);
};
if (opts.includeVersion)
report.reportInfo(MessageName.UNNAMED, formatUtils.applyStyle(opts.configuration, `Yarn ${YarnVersion}`, formatUtils.Style.BOLD));
try {
await cb(report);
} catch (error) {
report.reportExceptionOnce(error);
} finally {
await report.finalize();
process.emitWarning = emitWarning;
}
return report;
}
private configuration: Configuration;
private forceSectionAlignment: boolean;
private includeNames: boolean;
private includePrefix: boolean;
private includeFooter: boolean;
private includeInfos: boolean;
private includeWarnings: boolean;
private json: boolean;
private stdout: Writable;
private uncommitted = new Set<{
committed: boolean;
action: () => void;
}>();
private warningCount: number = 0;
private errorCount: number = 0;
private timerFooter: Array<() => void> = [];
private startTime: number = Date.now();
private indent: number = 0;
private level: number = 0;
private progress: Map<ProgressIterable, {
definition: ProgressDefinition;
lastScaledSize?: number;
lastTitle?: string;
}> = new Map();
private progressTime: number = 0;
private progressFrame: number = 0;
private progressTimeout: ReturnType<typeof setTimeout> | null = null;
private progressStyle: {date?: Array<number>, chars: Array<string>, size: number} | null = null;
private progressMaxScaledSize: number | null = null;
constructor({
configuration,
stdout,
json = false,
forceSectionAlignment = false,
includeNames = true,
includePrefix = true,
includeFooter = true,
includeLogs = !json,
includeInfos = includeLogs,
includeWarnings = includeLogs,
}: StreamReportOptions) {
super();
formatUtils.addLogFilterSupport(this, {configuration});
this.configuration = configuration;
this.forceSectionAlignment = forceSectionAlignment;
this.includeNames = includeNames;
this.includePrefix = includePrefix;
this.includeFooter = includeFooter;
this.includeInfos = includeInfos;
this.includeWarnings = includeWarnings;
this.json = json;
this.stdout = stdout;
// Setup progress
if (configuration.get(`enableProgressBars`) && !json && (stdout as WriteStream).isTTY && (stdout as WriteStream).columns > 22) {
const styleName = configuration.get(`progressBarStyle`) || defaultStyle;
if (!Object.hasOwn(PROGRESS_STYLES, styleName))
throw new Error(`Assertion failed: Invalid progress bar style`);
this.progressStyle = PROGRESS_STYLES[styleName];
const maxWidth = Math.min(this.getRecommendedLength(), 80);
this.progressMaxScaledSize = Math.floor(this.progressStyle.size * maxWidth / 80);
}
}
hasErrors() {
return this.errorCount > 0;
}
exitCode() {
return this.hasErrors() ? 1 : 0;
}
getRecommendedLength() {
const PREFIX_SIZE = `β€ YN0000: β `.length;
// The `- 1` is to account for terminals that would wrap after the last column rather before the first overwrite
const recommendedLength = this.progressStyle !== null
? (this.stdout as WriteStream).columns - 1
: super.getRecommendedLength();
return Math.max(40, recommendedLength - PREFIX_SIZE - this.indent * 2);
}
startSectionSync<T>({reportHeader, reportFooter, skipIfEmpty}: SectionOptions, cb: () => T) {
const mark = {committed: false, action: () => {
reportHeader?.();
}};
if (skipIfEmpty) {
this.uncommitted.add(mark);
} else {
mark.action();
mark.committed = true;
}
const before = Date.now();
try {
return cb();
} catch (error) {
this.reportExceptionOnce(error);
throw error;
} finally {
const after = Date.now();
this.uncommitted.delete(mark);
if (mark.committed) {
reportFooter?.(after - before);
}
}
}
async startSectionPromise<T>({reportHeader, reportFooter, skipIfEmpty}: SectionOptions, cb: () => Promise<T>) {
const mark = {committed: false, action: () => {
reportHeader?.();
}};
if (skipIfEmpty) {
this.uncommitted.add(mark);
} else {
mark.action();
mark.committed = true;
}
const before = Date.now();
try {
return await cb();
} catch (error) {
this.reportExceptionOnce(error);
throw error;
} finally {
const after = Date.now();
this.uncommitted.delete(mark);
if (mark.committed) {
reportFooter?.(after - before);
}
}
}
private startTimerImpl<Callback extends Function>(what: string, opts: TimerOptions | Callback, cb?: Callback) {
const realOpts = typeof opts === `function` ? {} : opts;
const realCb = typeof opts === `function` ? opts : cb!;
return {
cb: realCb,
reportHeader: () => {
this.level += 1;
this.reportInfo(null, `β ${what}`);
this.indent += 1;
if (GROUP !== null && !this.json && this.includeInfos) {
this.stdout.write(GROUP.start(what));
}
},
reportFooter: elapsedTime => {
this.indent -= 1;
if (GROUP !== null && !this.json && this.includeInfos) {
this.stdout.write(GROUP.end(what));
for (const cb of this.timerFooter) {
cb();
}
}
if (this.configuration.get(`enableTimers`) && elapsedTime > 200)
this.reportInfo(null, `β Completed in ${formatUtils.pretty(this.configuration, elapsedTime, formatUtils.Type.DURATION)}`);
else
this.reportInfo(null, `β Completed`);
this.level -= 1;
},
skipIfEmpty: realOpts.skipIfEmpty,
} as SectionOptions & {cb: Callback};
}
startTimerSync<T>(what: string, opts: TimerOptions, cb: () => T): T;
startTimerSync<T>(what: string, cb: () => T): T;
startTimerSync<T>(what: string, opts: TimerOptions | (() => T), cb?: () => T) {
const {cb: realCb, ...sectionOps} = this.startTimerImpl(what, opts, cb);
return this.startSectionSync(sectionOps, realCb);
}
async startTimerPromise<T>(what: string, opts: TimerOptions, cb: () => Promise<T>): Promise<T>;
async startTimerPromise<T>(what: string, cb: () => Promise<T>): Promise<T>;
async startTimerPromise<T>(what: string, opts: TimerOptions | (() => Promise<T>), cb?: () => Promise<T>) {
const {cb: realCb, ...sectionOps} = this.startTimerImpl(what, opts, cb);
return this.startSectionPromise(sectionOps, realCb);
}
reportSeparator() {
if (this.indent === 0) {
this.writeLine(``);
} else {
this.reportInfo(null, ``);
}
}
reportInfo(name: MessageName | null, text: string) {
if (!this.includeInfos)
return;
this.commit();
const formattedName = this.formatNameWithHyperlink(name);
const prefix = formattedName ? `${formattedName}: ` : ``;
const message = `${this.formatPrefix(prefix, `blueBright`)}${text}`;
if (!this.json) {
this.writeLine(message);
} else {
this.reportJson({type: `info`, name, displayName: this.formatName(name), indent: this.formatIndent(), data: text});
}
}
reportWarning(name: MessageName, text: string) {
this.warningCount += 1;
if (!this.includeWarnings)
return;
this.commit();
const formattedName = this.formatNameWithHyperlink(name);
const prefix = formattedName ? `${formattedName}: ` : ``;
if (!this.json) {
this.writeLine(`${this.formatPrefix(prefix, `yellowBright`)}${text}`);
} else {
this.reportJson({type: `warning`, name, displayName: this.formatName(name), indent: this.formatIndent(), data: text});
}
}
reportError(name: MessageName, text: string) {
this.errorCount += 1;
this.timerFooter.push(() => this.reportErrorImpl(name, text));
this.reportErrorImpl(name, text);
}
reportErrorImpl(name: MessageName, text: string) {
this.commit();
const formattedName = this.formatNameWithHyperlink(name);
const prefix = formattedName ? `${formattedName}: ` : ``;
if (!this.json) {
this.writeLine(`${this.formatPrefix(prefix, `redBright`)}${text}`, {truncate: false});
} else {
this.reportJson({type: `error`, name, displayName: this.formatName(name), indent: this.formatIndent(), data: text});
}
}
reportFold(title: string, text: string) {
if (!GROUP)
return;
const message = `${GROUP.start(title)}${text}${GROUP.end(title)}`;
this.timerFooter.push(() => this.stdout.write(message));
}
reportProgress(progressIt: ProgressIterable) {
if (this.progressStyle === null)
return {...Promise.resolve(), stop: () => {}};
if (progressIt.hasProgress && progressIt.hasTitle)
throw new Error(`Unimplemented: Progress bars can't have both progress and titles.`);
let stopped = false;
const promise = Promise.resolve().then(async () => {
const progressDefinition: ProgressDefinition = {
progress: progressIt.hasProgress ? 0 : undefined,
title: progressIt.hasTitle ? `` : undefined,
};
this.progress.set(progressIt, {
definition: progressDefinition,
lastScaledSize: progressIt.hasProgress ? -1 : undefined,
lastTitle: undefined,
});
this.refreshProgress({delta: -1});
for await (const {progress, title} of progressIt) {
if (stopped)
continue;
if (progressDefinition.progress === progress && progressDefinition.title === title)
continue;
progressDefinition.progress = progress;
progressDefinition.title = title;
this.refreshProgress();
}
stop();
});
const stop = () => {
if (stopped)
return;
stopped = true;
this.progress.delete(progressIt);
this.refreshProgress({delta: +1});
};
return {...promise, stop};
}
reportJson(data: any) {
if (this.json) {
this.writeLine(`${JSON.stringify(data)}`);
}
}
async finalize() {
if (!this.includeFooter)
return;
let installStatus = ``;
if (this.errorCount > 0)
installStatus = `Failed with errors`;
else if (this.warningCount > 0)
installStatus = `Done with warnings`;
else
installStatus = `Done`;
const timing = formatUtils.pretty(this.configuration, Date.now() - this.startTime, formatUtils.Type.DURATION);
const message = this.configuration.get(`enableTimers`)
? `${installStatus} in ${timing}`
: installStatus;
if (this.errorCount > 0) {
this.reportError(MessageName.UNNAMED, message);
} else if (this.warningCount > 0) {
this.reportWarning(MessageName.UNNAMED, message);
} else {
this.reportInfo(MessageName.UNNAMED, message);
}
}
private writeLine(str: string, {truncate}: {truncate?: boolean} = {}) {
this.clearProgress({clear: true});
this.stdout.write(`${this.truncate(str, {truncate})}\n`);
this.writeProgress();
}
private writeLines(lines: Array<string>, {truncate}: {truncate?: boolean} = {}) {
this.clearProgress({delta: lines.length});
for (const line of lines)
this.stdout.write(`${this.truncate(line, {truncate})}\n`);
this.writeProgress();
}
private commit() {
const marks = this.uncommitted;
this.uncommitted = new Set();
for (const mark of marks) {
mark.committed = true;
mark.action();
}
}
private clearProgress({delta = 0, clear = false}: {delta?: number, clear?: boolean}) {
if (this.progressStyle === null)
return;
if (this.progress.size + delta > 0) {
this.stdout.write(`\x1b[${this.progress.size + delta}A`);
if (delta > 0 || clear) {
this.stdout.write(`\x1b[0J`);
}
}
}
private writeProgress() {
if (this.progressStyle === null)
return;
if (this.progressTimeout !== null)
clearTimeout(this.progressTimeout);
this.progressTimeout = null;
if (this.progress.size === 0)
return;
const now = Date.now();
if (now - this.progressTime > PROGRESS_INTERVAL) {
this.progressFrame = (this.progressFrame + 1) % PROGRESS_FRAMES.length;
this.progressTime = now;
}
const spinner = PROGRESS_FRAMES[this.progressFrame];
for (const progress of this.progress.values()) {
let progressBar = ``;
if (typeof progress.lastScaledSize !== `undefined`) {
const ok = this.progressStyle.chars[0].repeat(progress.lastScaledSize);
const ko = this.progressStyle.chars[1].repeat(this.progressMaxScaledSize! - progress.lastScaledSize);
progressBar = ` ${ok}${ko}`;
}
const formattedName = this.formatName(null);
const prefix = formattedName ? `${formattedName}: ` : ``;
const title = progress.definition.title ? ` ${progress.definition.title}` : ``;
this.stdout.write(`${formatUtils.pretty(this.configuration, `β€`, `blueBright`)} ${prefix}${spinner}${progressBar}${title}\n`);
}
this.progressTimeout = setTimeout(() => {
this.refreshProgress({force: true});
}, PROGRESS_INTERVAL);
}
private refreshProgress({delta = 0, force = false}: {delta?: number, force?: boolean} = {}) {
let needsUpdate = false;
let needsClear = false;
if (force || this.progress.size === 0) {
needsUpdate = true;
} else {
for (const progress of this.progress.values()) {
const refreshedScaledSize = typeof progress.definition.progress !== `undefined`
? Math.trunc(this.progressMaxScaledSize! * progress.definition.progress)
: undefined;
const previousScaledSize = progress.lastScaledSize;
progress.lastScaledSize = refreshedScaledSize;
const previousTitle = progress.lastTitle;
progress.lastTitle = progress.definition.title;
if ((refreshedScaledSize !== previousScaledSize) || (needsClear = previousTitle !== progress.definition.title)) {
needsUpdate = true;
break;
}
}
}
if (needsUpdate) {
this.clearProgress({delta, clear: needsClear});
this.writeProgress();
}
}
private truncate(str: string, {truncate}: {truncate?: boolean} = {}) {
if (this.progressStyle === null)
truncate = false;
if (typeof truncate === `undefined`)
truncate = this.configuration.get(`preferTruncatedLines`);
// The -1 is to account for terminals that would wrap after
// the last column rather before the first overwrite
if (truncate)
str = sliceAnsi(str, 0, (this.stdout as WriteStream).columns - 1);
return str;
}
private formatName(name: MessageName | null) {
if (!this.includeNames)
return ``;
return formatName(name, {
configuration: this.configuration,
json: this.json,
});
}
private formatPrefix(prefix: string, caretColor: string) {
return this.includePrefix
? `${formatUtils.pretty(this.configuration, `β€`, caretColor)} ${prefix}${this.formatIndent()}`
: ``;
}
private formatNameWithHyperlink(name: MessageName | null) {
if (!this.includeNames)
return ``;
return formatNameWithHyperlink(name, {
configuration: this.configuration,
json: this.json,
});
}
private formatIndent() {
return this.level > 0 || !this.forceSectionAlignment
? `β `.repeat(this.indent)
: `${SINGLE_LINE_CHAR} `;
}
}