-
Notifications
You must be signed in to change notification settings - Fork 30k
/
Copy pathterminalProcess.ts
165 lines (147 loc) · 3.69 KB
/
terminalProcess.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as os from 'os';
import * as path from 'path';
import * as pty from 'node-pty';
// The pty process needs to be run in its own child process to get around maxing out CPU on Mac,
// see https://github.com/electron/electron/issues/38
var shellName: string;
if (os.platform() === 'win32') {
shellName = path.basename(process.env.PTYSHELL);
} else {
// Using 'xterm-256color' here helps ensure that the majority of Linux distributions will use a
// color prompt as defined in the default ~/.bashrc file.
shellName = 'xterm-256color';
}
var shell = process.env.PTYSHELL;
var args = getArgs();
var cwd = process.env.PTYCWD;
var cols = process.env.PTYCOLS;
var rows = process.env.PTYROWS;
var currentTitle = '';
setupPlanB(Number(process.env.PTYPID));
cleanEnv();
interface IOptions {
name: string;
cwd: string;
cols?: number;
rows?: number;
}
var options: IOptions = {
name: shellName,
cwd
};
if (cols && rows) {
options.cols = parseInt(cols, 10);
options.rows = parseInt(rows, 10);
}
var ptyProcess = pty.spawn(shell, args, options);
var closeTimeout: number;
var exitCode: number;
// Allow any trailing data events to be sent before the exit event is sent.
// See https://github.com/Tyriar/node-pty/issues/72
function queueProcessExit() {
if (closeTimeout) {
clearTimeout(closeTimeout);
}
closeTimeout = setTimeout(function () {
ptyProcess.kill();
process.exit(exitCode);
}, 250);
}
ptyProcess.on('data', function (data) {
process.send({
type: 'data',
content: data
});
if (closeTimeout) {
clearTimeout(closeTimeout);
queueProcessExit();
}
});
ptyProcess.on('exit', function (code) {
exitCode = code;
queueProcessExit();
});
process.on('message', function (message) {
if (message.event === 'input') {
ptyProcess.write(message.data);
} else if (message.event === 'resize') {
// Ensure that cols and rows are always >= 1, this prevents a native
// exception in winpty.
ptyProcess.resize(Math.max(message.cols, 1), Math.max(message.rows, 1));
} else if (message.event === 'shutdown') {
queueProcessExit();
}
});
sendProcessId();
setupTitlePolling();
function getArgs(): string | string[] {
if (process.env['PTYSHELLCMDLINE']) {
return process.env['PTYSHELLCMDLINE'];
}
var args = [];
var i = 0;
while (process.env['PTYSHELLARG' + i]) {
args.push(process.env['PTYSHELLARG' + i]);
i++;
}
return args;
}
function cleanEnv() {
var keys = [
'AMD_ENTRYPOINT',
'ELECTRON_NO_ASAR',
'ELECTRON_RUN_AS_NODE',
'GOOGLE_API_KEY',
'PTYCWD',
'PTYPID',
'PTYSHELL',
'PTYCOLS',
'PTYROWS',
'PTYSHELLCMDLINE',
'VSCODE_LOGS'
];
keys.forEach(function (key) {
if (process.env[key]) {
delete process.env[key];
}
});
var i = 0;
while (process.env['PTYSHELLARG' + i]) {
delete process.env['PTYSHELLARG' + i];
i++;
}
}
function setupPlanB(parentPid: number) {
setInterval(function () {
try {
process.kill(parentPid, 0); // throws an exception if the main process doesn't exist anymore.
} catch (e) {
process.exit();
}
}, 5000);
}
function sendProcessId() {
process.send({
type: 'pid',
content: ptyProcess.pid
});
}
function setupTitlePolling() {
sendProcessTitle();
setInterval(function () {
if (currentTitle !== ptyProcess.process) {
sendProcessTitle();
}
}, 200);
}
function sendProcessTitle() {
process.send({
type: 'title',
content: ptyProcess.process
});
currentTitle = ptyProcess.process;
}