-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathterminal-config.ts
298 lines (245 loc) · 6.79 KB
/
terminal-config.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
import type { TerminalPanelType, TerminalSchema } from '@tutorialkit/types';
import type { WebContainerProcess } from '@webcontainer/api';
import type { ITerminal } from '../utils/terminal.js';
interface NormalizedTerminalConfig {
panels: TerminalPanel[];
activePanel: number;
defaultOpen: boolean;
}
interface TerminalPanelOptions {
id?: string;
title?: string;
allowRedirects?: boolean;
allowCommands?: string[];
}
export class TerminalConfig {
private _config: NormalizedTerminalConfig;
constructor(config?: TerminalSchema) {
const normalized = normalizeTerminalConfig(config);
this._config = normalized;
}
get panels() {
return this._config.panels;
}
get activePanel() {
return this._config.activePanel;
}
get defaultOpen() {
return this._config.defaultOpen;
}
}
const TERMINAL_PANEL_TITLES: Record<TerminalPanelType, string> = {
output: 'Output',
terminal: 'Terminal',
};
let globalId = 0;
/**
* This class contains the state for a terminal panel. This is a panel which is attached to a process and renders
* the process output to a screen.
*/
export class TerminalPanel implements ITerminal {
static panelCount: Record<TerminalPanelType, number> = {
output: 0,
terminal: 0,
};
static resetCount() {
this.panelCount = {
output: 0,
terminal: 0,
};
}
readonly id: string;
readonly title: string;
private _terminal?: ITerminal;
private _process?: WebContainerProcess;
private _data: { data: string; type: 'input' | 'echo' }[] = [];
private _onData?: (data: string) => void;
constructor(
readonly type: TerminalPanelType,
private readonly _options?: TerminalPanelOptions,
) {
let title = _options?.title;
// automatically infer a title if no title is provided
if (!title) {
title = TERMINAL_PANEL_TITLES[type];
// we keep track of all untitled panel and add an index to the title
const count = TerminalPanel.panelCount[type];
if (count > 0) {
title += ` ${count}`;
}
TerminalPanel.panelCount[type]++;
}
this.title = title;
this.id = _options?.id ?? (type === 'output' ? 'output' : `${type}-${globalId++}`);
}
get terminal() {
return this._terminal;
}
get process() {
return this._process;
}
get processOptions() {
if (this.type === 'output') {
return undefined;
}
return {
allowRedirects: this._options?.allowRedirects ?? false,
allowCommands: this._options?.allowCommands,
};
}
// #region ITerminal methods
get cols() {
// we fallback to a default
return this._terminal?.cols;
}
get rows() {
return this._terminal?.rows;
}
reset() {
if (this._terminal) {
this._terminal.reset();
} else {
this._data = [];
}
}
/** @internal*/
write(data: string) {
if (this._terminal) {
this._terminal.write(data);
} else {
this._data.push({ data, type: 'echo' });
}
}
input(data: string) {
if (this.type !== 'terminal') {
throw new Error('Cannot write data to output-only terminal');
}
if (this._terminal) {
this._terminal.input(data);
} else {
this._data.push({ data, type: 'input' });
}
}
onData(callback: (data: string) => void) {
if (this._terminal) {
this._terminal.onData(callback);
} else {
this._onData = callback;
}
}
// #endregion
/**
* Attach a WebContainer process to this panel.
*
* @param process The WebContainer process
*/
attachProcess(process: WebContainerProcess) {
this._process = process;
if (this.cols != null && this.rows != null) {
this._process.resize({ cols: this.cols, rows: this.rows });
}
}
/**
* Attach a terminal to this panel.
*
* @param terminal The terminal.
*/
attachTerminal(terminal: ITerminal) {
for (const { type, data } of this._data) {
if (type === 'echo') {
terminal.write(data);
} else {
terminal.input(data);
}
}
this._data = [];
this._terminal = terminal;
if (this._onData) {
terminal.onData(this._onData);
}
if (this.cols != null && this.rows != null) {
this._process?.resize({ cols: this.cols, rows: this.rows });
}
}
}
// set the default commands for the terminal
const DEFAULT_COMMANDS = ['ls', 'echo'];
/**
* Normalize the provided configuration to a configuration which is easier to parse.
*
* @param config The terminal configuration.
* @returns A normalized terminal configuration.
*/
function normalizeTerminalConfig(config?: TerminalSchema): NormalizedTerminalConfig {
let activePanel = 0;
if (config === false) {
// if the value is `false`, we don't render anything
return {
panels: [],
activePanel,
defaultOpen: false,
};
}
// reset the count so that the auto-infered titles are indexed properly
TerminalPanel.resetCount();
// if no config is set, or the value is `true`, we just render the output panel
if (config === undefined || config === true) {
return {
panels: [new TerminalPanel('output')],
activePanel,
defaultOpen: false,
};
}
const panels: TerminalPanel[] = [];
const resolveAllowCommands = (globalCommands?: string[], panelCommands?: string[]): string[] | undefined => {
if (panelCommands === undefined) {
return globalCommands ?? DEFAULT_COMMANDS;
}
if (Array.isArray(panelCommands) && panelCommands.length === 0) {
return undefined;
}
return panelCommands;
};
const options = {
allowRedirects: config.allowRedirects,
allowCommands: config.allowCommands,
};
if (config.panels) {
if (config.panels === 'output') {
panels.push(new TerminalPanel('output'));
} else if (config.panels === 'terminal') {
panels.push(new TerminalPanel('terminal', options));
} else if (Array.isArray(config.panels)) {
for (const panel of config.panels) {
let terminalPanel: TerminalPanel;
if (typeof panel === 'string') {
terminalPanel = new TerminalPanel(panel, options);
} else if (Array.isArray(panel)) {
terminalPanel = new TerminalPanel(panel[0], {
title: panel[1],
...options,
});
} else {
terminalPanel = new TerminalPanel(panel.type, {
id: panel.id,
title: panel.title,
allowRedirects: panel.allowRedirects ?? config.allowRedirects,
allowCommands: resolveAllowCommands(config.allowCommands, panel.allowCommands),
});
}
panels.push(terminalPanel);
}
}
}
if (typeof config.activePanel === 'number') {
activePanel = config.activePanel;
if (activePanel >= panels.length) {
activePanel = 0;
}
}
return {
activePanel,
panels,
defaultOpen: config.open || false,
};
}