-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathTerminalStreamWritable.ts
77 lines (71 loc) · 2.25 KB
/
TerminalStreamWritable.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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { Writable, type WritableOptions } from 'stream';
import type { ITerminal } from './ITerminal';
import { TerminalProviderSeverity } from './ITerminalProvider';
/**
* Options for {@link TerminalStreamWritable}.
*
* @beta
*/
export interface ITerminalStreamWritableOptions {
/**
* The {@link ITerminal} that the Writable will write to.
*/
terminal: ITerminal;
/**
* The severity of the messages that will be written to the {@link ITerminal}.
*/
severity: TerminalProviderSeverity;
/**
* Options for the underlying Writable.
*/
writableOptions?: WritableOptions;
}
/**
* A adapter to allow writing to a provided terminal using Writable streams.
*
* @beta
*/
export class TerminalStreamWritable extends Writable {
private _writeMethod: (data: string) => void;
public constructor(options: ITerminalStreamWritableOptions) {
const { terminal, severity, writableOptions } = options;
super(writableOptions);
this._writev = undefined;
switch (severity) {
case TerminalProviderSeverity.log:
this._writeMethod = terminal.write.bind(terminal);
break;
case TerminalProviderSeverity.verbose:
this._writeMethod = terminal.writeVerbose.bind(terminal);
break;
case TerminalProviderSeverity.debug:
this._writeMethod = terminal.writeDebug.bind(terminal);
break;
case TerminalProviderSeverity.warning:
this._writeMethod = terminal.writeWarning.bind(terminal);
break;
case TerminalProviderSeverity.error:
this._writeMethod = terminal.writeError.bind(terminal);
break;
default:
throw new Error(`Unknown severity: ${severity}`);
}
}
public _write(
chunk: string | Buffer | Uint8Array,
encoding: string,
// eslint-disable-next-line @rushstack/no-new-null
callback: (error?: Error | null) => void
): void {
try {
const chunkData: string | Buffer = typeof chunk === 'string' ? chunk : Buffer.from(chunk);
this._writeMethod(chunkData.toString());
} catch (e: unknown) {
callback(e as Error);
return;
}
callback();
}
}