forked from iann0036/cloud9-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminalManager.js
147 lines (147 loc) · 6.91 KB
/
terminalManager.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var vscode = require("vscode");
var net = require("net");
var TerminalManager = /** @class */ (function () {
function TerminalManager(extensionPath, eventEmitter) {
var _this = this;
this.extensionPath = extensionPath;
this.eventEmitter = eventEmitter;
this.terminals = {};
eventEmitter.on('terminal_process_created', function (pty) {
console.log("TERMINAL PROCESS DATA");
console.log(pty);
console.log(_this.lastSocket);
_this.terminals[pty["id"]] = {
"terminal": _this.lastCreatedTerminal,
"pid": parseInt(pty["pid"]),
"tid": _this.lastTid,
"socket": _this.lastSocket
};
_this.eventEmitter.emit('send_ch4_message', ["resize", pty["pid"], 159, 33]);
_this.eventEmitter.emit('send_ch4_message', ["tmux", "", { "capturePane": { "start": -32768, "end": 1000, "pane": "cloud9_terminal_" + _this.lastTid + ":0.0" }, "encoding": "utf8", "name": "xterm-color", "command": "" }, { "$": pty["id"] }]);
});
eventEmitter.on('ch4_data', function (data, environmentId) {
if (Array.isArray(data)) {
if (data.length > 2) {
if (data[0] == "onEnd") {
if (Object.keys(_this.terminals).map(Number).indexOf(data[1]) != -1) {
console.log("Terminating terminal");
_this.closeTerminal(_this.terminals[data[1]]);
delete _this.terminals[data[1]];
}
}
else if (data[0] == "onData") {
if (Object.keys(_this.terminals).map(Number).indexOf(data[1]) != -1) {
console.log("Emitting terminal data");
_this.emitTerminalData(_this.terminals[data[1]], data[2]);
}
}
else if (data[0] == 90) { // terminal creation channel
var contents = data[2];
console.log("Terminal Process Created");
eventEmitter.emit('terminal_process_created', contents["pty"]);
}
}
}
});
vscode.window.onDidCloseTerminal(function (closedTerminal) {
closedTerminal.processId.then(function (closedTerminalPid) {
for (var t in _this.terminals) {
_this.terminals[t]['terminal'].processId.then(function (pid) {
if (pid == closedTerminalPid) {
delete _this.terminals[t];
}
});
}
});
});
}
TerminalManager.prototype.addTerminal = function (shared, vfsid) {
var _this = this;
if (shared) {
vscode.window.showInformationMessage("Shared terminal requires Visual Studio Code 1.26.0 or above.");
return;
}
var terminalPath = this.getTerminalPath();
this.vfsid = vfsid;
if (terminalPath == null) {
vscode.window.showWarningMessage("Unsupported platform for terminal. Upgrade Visual Studio Code to 1.26.0 or above for full compatibility.");
return;
}
var server = net.createServer(function (socket) {
_this.lastSocket = socket;
console.log("SOCKET OBTAINED");
socket.on('end', function () {
console.log('socket closed');
});
socket.on('data', function (data) {
var correctTerminalId = null;
for (var t in _this.terminals) {
if (_this.terminals[t]['socket'] === socket) {
correctTerminalId = t;
}
}
if (correctTerminalId == null) {
console.warn("Missing terminal socket");
return;
}
_this.eventEmitter.emit('send_ch4_message', ["write", correctTerminalId.toString(), data.toString()]); // TODO: Fix
});
_this.lastTid = Math.floor(900 * Math.random()) + 100;
_this.eventEmitter.emit('send_ch4_message', ["tmux", "", { "cwd": "/home/ec2-user/environment", "cols": 125, "rows": 33, "name": "xterm-color", "base": "/home/ec2-user/.c9", "attach": false, "session": "cloud9_terminal_" + _this.lastTid, "output": false, "terminal": true, "detachOthers": true, "defaultEditor": false, "encoding": "utf8", "command": "bash -l" }, { "$": 90 }]);
console.log("init'd remote terminal");
}).on('error', function (err) {
console.error(err);
});
server.listen({
host: 'localhost',
port: 0,
exclusive: true
}, function () {
var addr = server.address();
var title = "Cloud9 Terminal";
console.log('opened server on', addr);
if (process.platform == "win32") {
_this.lastCreatedTerminal = vscode.window.createTerminal(title, _this.extensionPath + "/terminalApp/ansicon/" + process.arch + "/ansicon.exe", [terminalPath, addr['address'] + ":" + addr['port']]);
}
else {
_this.lastCreatedTerminal = vscode.window.createTerminal(title, terminalPath, [addr['address'] + ":" + addr['port']]);
}
_this.lastCreatedTerminal.show();
});
};
TerminalManager.prototype.getTerminalPath = function () {
var terminalPath = this.extensionPath + '/terminalApp/terminal-';
if (process.arch == "x64" && process.platform == "darwin") {
return terminalPath + "darwin-amd64";
/*} else if (process.arch == "x32" && process.platform == "win32") {
return terminalPath + "windows-386.exe";
} else if (process.arch == "x64" && process.platform == "win32") {
return terminalPath + "windows-amd64.exe";*/
}
else if (process.arch == "x32" && process.platform == "linux") {
return terminalPath + "linux-386";
}
else if (process.arch == "x64" && process.platform == "linux") {
return terminalPath + "linux-amd64";
}
return null;
};
TerminalManager.prototype.closeTerminal = function (terminal) {
terminal['socket'].destroy();
};
TerminalManager.prototype.closeAll = function () {
var _this = this;
Object.values(this.terminals).forEach(function (terminal) {
_this.closeTerminal(terminal);
});
};
TerminalManager.prototype.emitTerminalData = function (terminal, data) {
if (typeof data == "string") {
terminal['socket'].write(data);
}
};
return TerminalManager;
}());
exports.TerminalManager = TerminalManager;