-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathnode-pty.ts
More file actions
106 lines (90 loc) · 2.53 KB
/
node-pty.ts
File metadata and controls
106 lines (90 loc) · 2.53 KB
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
import { randomUUID } from "crypto";
import { ipcRenderer } from "electron";
import { IPtyForkOptions, IWindowsPtyForkOptions } from "node-pty";
import { Observable } from "babylonjs";
/**
* Creates a new node-pty instance.
* @param command The command to run in the pty process.
* @param options The options to pass to the pty process.
* @returns A promise that resolves with the node-pty instance.
*/
export async function execNodePty(
command: string,
options: IPtyForkOptions | IWindowsPtyForkOptions | (IPtyForkOptions & { interactive?: boolean }) | (IWindowsPtyForkOptions & { interactive?: boolean }) = {}
): Promise<NodePtyInstance> {
const id = randomUUID();
await new Promise<void>((resolve) => {
ipcRenderer.once(`editor:create-node-pty-${id}`, () => resolve());
ipcRenderer.send("editor:create-node-pty", command, id, options);
});
if (id === null) {
throw new Error("Failed to create node-pty instance.");
}
return new NodePtyInstance(id);
}
export class NodePtyInstance {
/**
* The id of the node-pty instance.
*/
public readonly id: string;
/**
* An observable that is triggered when data is received from the pty.
*/
public onGetDataObservable: Observable<string> = new Observable<string>();
private _exited: boolean = false;
private _exitCode: number = -1;
/**
* Constructor.
* @param id The id of the node-pty instance.
*/
public constructor(id: string) {
this.id = id;
ipcRenderer.once(`editor:node-pty-exit:${this.id}`, (_, code) => {
this._exited = true;
this._exitCode = code;
});
ipcRenderer.on(`editor:node-pty-data:${id}`, (_, data) => {
// console.log(data);
this.onGetDataObservable.notifyObservers(data);
});
}
/**
* Writes data to the pty.
* @param data The data to write.
*/
public write(data: string): void {
if (this._exited) {
return;
}
ipcRenderer.send("editor:node-pty-write", this.id, data);
}
/**
* Kills the pty process.
*/
public kill(): void {
if (this._exited) {
return;
}
ipcRenderer.send("editor:kill-node-pty", this.id);
}
/**
* Waits until the
*/
public wait(): Promise<number> {
if (this._exited) {
return Promise.resolve(this._exitCode);
}
return new Promise<number>((resolve) => {
ipcRenderer.once(`editor:node-pty-exit:${this.id}`, () => resolve(this._exitCode));
});
}
/**
* Resizes the node-pty process in case it is used using xterm.
*/
public resize(cols: number, rows: number): void {
if (this._exited) {
return;
}
ipcRenderer.send("editor:resize-node-pty", this.id, cols, rows);
}
}