-
Notifications
You must be signed in to change notification settings - Fork 53
/
console-executor.js
65 lines (52 loc) · 1.72 KB
/
console-executor.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
const { spawn } = require('child_process');
const { app } = require('electron');
const path = require('path');
async function setPATHEnv() {
const puttyLookup = [
path.join(__dirname, 'dist', 'putty'),
path.join(path.dirname(app.getPath('exe')), 'dist', 'putty')
];
// prevent adding duplicates
let extra = [
...puttyLookup,
].filter((dir) => {
return process.env.PATH.indexOf(dir) < 0;
});
extra.push(process.env.PATH);
process.env.PATH = extra.join(";");
}
exports.openConsole = async (consoleRequest) => {
// const genericConsoleCommand = 'xfce4-terminal --tab -T "%d" -e "telnet %h %p"';
const genericConsoleCommand = 'putty.exe -telnet %h %p -loghost "%d"';
const command = prepareCommand(genericConsoleCommand, consoleRequest);
console.log(`Setting up PATH`);
await setPATHEnv();
console.log(`Starting console with command: '${command}'`);
let consoleProcess = spawn(command, [], {
shell :true
});
consoleProcess.stdout.on('data', (data) => {
console.log(`Console stdout is producing: ${data.toString()}`);
});
consoleProcess.stderr.on('data', (data) => {
console.log(`Console stderr is producing: ${data.toString()}`);
});
consoleProcess.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
}
function prepareCommand(consoleCommand, consoleRequest) {
const mapping = {
h: consoleRequest.host,
p: consoleRequest.port,
d: consoleRequest.name,
i: consoleRequest.project_id,
n: consoleRequest.node_id,
c: consoleRequest.server_url
};
for(var key in mapping) {
const regExp = new RegExp(`%${key}`, 'g');
consoleCommand = consoleCommand.replace(regExp, mapping[key]);
}
return consoleCommand;
}