-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathConsoleTerminalProvider.ts
96 lines (82 loc) · 2.47 KB
/
ConsoleTerminalProvider.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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { EOL } from 'os';
import supportsColor from 'supports-color';
import { type ITerminalProvider, TerminalProviderSeverity } from './ITerminalProvider';
/**
* Options to be provided to a {@link ConsoleTerminalProvider}
*
* @beta
*/
export interface IConsoleTerminalProviderOptions {
/**
* If true, print verbose logging messages.
*/
verboseEnabled: boolean;
/**
* If true, print debug logging messages. Note that "verbose" and "debug" are considered
* separate message filters; if you want debug to imply verbose, it is up to your
* application code to enforce that.
*/
debugEnabled: boolean;
}
/**
* Terminal provider that prints to STDOUT (for log- and verbose-level messages) and
* STDERR (for warning- and error-level messages).
*
* @beta
*/
export class ConsoleTerminalProvider implements ITerminalProvider {
public static readonly supportsColor: boolean = !!supportsColor.stdout && !!supportsColor.stderr;
/**
* If true, verbose-level messages should be written to the console.
*/
public verboseEnabled: boolean;
/**
* If true, debug-level messages should be written to the console.
*/
public debugEnabled: boolean;
/**
* {@inheritDoc ITerminalProvider.supportsColor}
*/
public readonly supportsColor: boolean = ConsoleTerminalProvider.supportsColor;
public constructor(options: Partial<IConsoleTerminalProviderOptions> = {}) {
this.verboseEnabled = !!options.verboseEnabled;
this.debugEnabled = !!options.debugEnabled;
}
/**
* {@inheritDoc ITerminalProvider.write}
*/
public write(data: string, severity: TerminalProviderSeverity): void {
switch (severity) {
case TerminalProviderSeverity.warning:
case TerminalProviderSeverity.error: {
process.stderr.write(data);
break;
}
case TerminalProviderSeverity.verbose: {
if (this.verboseEnabled) {
process.stdout.write(data);
}
break;
}
case TerminalProviderSeverity.debug: {
if (this.debugEnabled) {
process.stdout.write(data);
}
break;
}
case TerminalProviderSeverity.log:
default: {
process.stdout.write(data);
break;
}
}
}
/**
* {@inheritDoc ITerminalProvider.eolCharacter}
*/
public get eolCharacter(): string {
return EOL;
}
}