Skip to content

Commit 8f8d046

Browse files
committed
feat(runtime): add terminal.input for writing to stdin
1 parent f49e910 commit 8f8d046

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

packages/runtime/src/utils/terminal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface ITerminal {
44

55
reset: () => void;
66
write: (data: string) => void;
7+
input: (data: string) => void;
78
onData: (cb: (data: string) => void) => void;
89
}
910

packages/runtime/src/webcontainer/terminal-config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class TerminalPanel implements ITerminal {
6767
private _terminal?: ITerminal;
6868
private _process?: WebContainerProcess;
6969
private _data: string[] = [];
70+
private _input: string[] = [];
7071
private _onData?: (data: string) => void;
7172

7273
constructor(
@@ -127,9 +128,15 @@ export class TerminalPanel implements ITerminal {
127128
this._terminal.reset();
128129
} else {
129130
this._data = [];
131+
this._input = [];
130132
}
131133
}
132134

135+
/**
136+
* Write data to stdout.
137+
*
138+
* @internal
139+
*/
133140
write(data: string) {
134141
if (this._terminal) {
135142
this._terminal.write(data);
@@ -138,6 +145,20 @@ export class TerminalPanel implements ITerminal {
138145
}
139146
}
140147

148+
/** Write data to stdin */
149+
input(data: string) {
150+
if (this.type !== 'terminal') {
151+
throw new Error('Cannot write data to output-only terminal');
152+
}
153+
154+
if (this._terminal) {
155+
this._terminal.input(data);
156+
} else {
157+
this._input.push(data);
158+
}
159+
}
160+
161+
/** Callback invoked when data is written to stdin */
141162
onData(callback: (data: string) => void) {
142163
if (this._terminal) {
143164
this._terminal.onData(callback);
@@ -170,7 +191,12 @@ export class TerminalPanel implements ITerminal {
170191
terminal.write(data);
171192
}
172193

194+
for (const data of this._input) {
195+
terminal.input(data);
196+
}
197+
173198
this._data = [];
199+
this._input = [];
174200
this._terminal = terminal;
175201

176202
if (this._onData) {

0 commit comments

Comments
 (0)