-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
299 lines (244 loc) · 9.55 KB
/
render.js
File metadata and controls
299 lines (244 loc) · 9.55 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const { spawn } = require('child_process');
const path = require('path');
const kill = require('tree-kill');
const { Terminal } = require('@xterm/xterm');
const net = require('net');
const os = require('os');
const fs = require('fs');
const { ipcRenderer } = require('electron');
const http = require('http');
let terminal = new Terminal({
rendererType: 'dom',
convertEol: true, // Make sure that all line ends are converted to line feeds
windowsMode: os.platform() === 'win32', // Set based on the platform
wordWrap: true, // Enable word wrapping
cols: 100, // Set the width of the terminal (number of characters per line)
});
terminal.open(document.getElementById('terminal-container'));
let startFilePath;
// Detect the os
const platform = os.platform();
if (platform === 'win32') { // Windows
startFilePath = path.join(__dirname, 'server', 'windows', 'windows_setup.py');
} else if (platform === 'darwin') { // macOS
startFilePath = path.join(__dirname, 'server', 'mac', 'mac_setup.py');
} else {
logTerminalData('Unsupported platform');
}
process.chdir(path.dirname(startFilePath));
let child;
function trySpawn(command, startFilePath) {
return new Promise((resolve, reject) => {
const process = spawn(command, [startFilePath], { windowsHide: os.platform() === 'win32' });
let commandNotFound = true;
process.on('error', (err) => {
if (err.code === 'ENOENT') {
reject(`${command} not found`); // Reject if command not found
} else {
logTerminalData(`Error starting Python process with ${command}: ${err}`);
reject(err); // Reject on other errors
}
});
process.stdout.on('data', (data) => {
if (commandNotFound) {
commandNotFound = false; // Data received, command exists
resolve(process); // Resolve
}
logTerminalData(data.toString());
});
process.stderr.on('data', (data) => {
logTerminalData(data.toString());
});
process.on('close', (code) => {
logTerminalData(`Process exited with code ${code}\r\n`);
if (commandNotFound) {
// If the process closes before any output, consider it a failure
reject(`Process with ${command} exited prematurely.`);
}
});
});
}
async function startPythonProcess(startFilePath) {
const commands = ['python', 'python3'];
for (const command of commands) {
try {
child = await trySpawn(command, startFilePath);
// console.log(`${command} command was successful.`);
return; // Exit the loop and function if successful
} catch (error) {
// console.log(error);
}
}
logTerminalData("Failed to find a valid Python command ('python' or 'python3').\r\n");
}
document.getElementById('start').addEventListener('click', function () {
startPythonProcess(startFilePath);
});
document.getElementById('stop').addEventListener('click', function () {
if (child) {
kill(child.pid, 'SIGTERM', function (err) {
if (err) {
logTerminalData('Error stopping the server.\r\n');
} else {
logTerminalData('Server stopped successfully.\r\n');
}
});
}
});
document.getElementById('restart').addEventListener('click', function () {
terminal.write('Server restarting...\r\n');
if (child) {
kill(child.pid, 'SIGTERM', function (err) {
if (err) {
terminal.write('Error stopping the server.\r\n');
} else {
terminal.write('Server stopped successfully.\r\n');
setTimeout(() => {
startPythonProcess(startFilePath);
}, 1000);
}
});
}
});
let logContent = '';
function stripAnsiCodes(str) {
const ansiEscapeCodesPattern = new RegExp([
'(\\u001B\\u005B[\\d;]*m)', // Match ANSI color codes
'(\\u001B\\u005B\\d*G)', // Match cursor horizontal absolute codes
'(\\u001B\\u005B\\d*J)', // Match erase display codes
'(\\u001B\\u005B\\d*K)', // Match erase in line codes
'(\\u001B\\u005B\\d*;\\d*H)', // Match cursor position codes
'(\\u001B\\u005B\\d*A)', // Match cursor up codes
'(\\u001B\\u005B\\d*B)', // Match cursor down codes
'(\\u001B\\u005B\\d*C)', // Match cursor forward codes
'(\\u001B\\u005B\\d*D)', // Match cursor back codes
'(\\u001B\\u005B\\d*;\\d*H)', // Match cursor position codes
'(\\u001B\\u005B\\d*;\\d*f)' // Match horizontal and vertical position codes
].join('|'), 'g');
return str.replace(ansiEscapeCodesPattern, '');
}
function logTerminalData(data) {
let timestamp = new Date().toISOString();
let semiCleanData = stripAnsiCodes(data); // Strip ANSI codes
let cleanData = semiCleanData.replace(/\\u25b2/g, '');
let logEntry = `${timestamp}: ${cleanData}`;
logContent += logEntry;
terminal.write(data)
}
document.getElementById('saveLogs').addEventListener('click', function () {
let timestamp = new Date().toISOString().replace(/[:.]/g, '-');
let logFileName = `server-logs-${timestamp}.txt`;
let logsFolderPath = path.join(__dirname, 'logs');
let logFilePath = path.join(logsFolderPath, logFileName);
if (!fs.existsSync(logsFolderPath)) {
fs.mkdirSync(logsFolderPath);
}
fs.writeFile(logFilePath, logContent, (err) => {
if (err) {
// console.error('Failed to save logs:', err);
logTerminalData('Failed to save logs.\r\n');
} else {
logTerminalData('Logs saved successfully.\r\n');
openFileLocation(logFilePath);
}
});
});
function openFileLocation(filePath) {
let command = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'explorer' : 'xdg-open';
let args = process.platform === 'win32' ? ["/select,", filePath] : [path.dirname(filePath)];
spawn(command, args);
}
function checkPortStatus(port, statusID, callback) {
let requestOptions = {
hostname: 'localhost',
port: port,
timeout: 5000, // 5 seconds timeout
};
let req = http.get(requestOptions, (res) => {
let acceptableStatusCodes = statusID === 'server-status' ? [200, 302] : [200];
if (acceptableStatusCodes.includes(res.statusCode)) {
callback('in use', statusID);
} else {
console.log(`Received status code ${res.statusCode} for port ${port}`);
callback('free', statusID);
}
res.resume(); // Consume response data to free up memory
});
req.on('error', (e) => {
if (e.code === 'ECONNREFUSED') {
// console.log("Connection refused")
callback('free', statusID);
} else {
console.error(`Error while checking port ${port}: ${e.message}`);
callback('error', statusID);
}
});
req.on('timeout', () => {
console.error(`Request to port ${port} timed out`);
req.abort(); // Abort the request on timeout
callback('error', statusID);
});
}
function displayServerStatus(status, statusID) {
let statusDiv = document.getElementById(statusID);
if (status === 'in use') {
statusDiv.textContent = 'IN USE';
statusDiv.style.backgroundColor = "#008000"; // Green
} else if (status === 'free') {
statusDiv.textContent = 'STOPPED';
statusDiv.style.backgroundColor = "#ff0000"; // Red
} else {
statusDiv.textContent = 'ERROR';
statusDiv.style.backgroundColor = '#ff9800'; // Orange
}
}
// Function to check port status and update display
function updateServerStatus(port, statusID) {
checkPortStatus(port, statusID, displayServerStatus);
}
ipcRenderer.on('quit-app', () => {
stopServer();
});
function stopServer() {
if (child) {
kill(child.pid, 'SIGTERM', (err) => {
if (err) {
console.error('Failed to stop the server:', err);
}
});
}
}
function loadSettings() {
const settingsFilePath = path.join(__dirname, 'settings.json');
if (fs.existsSync(settingsFilePath)) {
const settingsData = fs.readFileSync(settingsFilePath, 'utf-8');
const settings = JSON.parse(settingsData);
document.getElementById('port-server').value = settings.serverPort;
document.getElementById('port-live').value = settings.livePort;
updateServerStatus(settings.serverPort, 'server-status');
updateServerStatus(settings.livePort, 'live-status');
startStatusUpdateLoop(settings.serverPort, 'server-status', settings.livePort, 'live-status');
}
}
let serverStatusUpdateInterval;
let liveStatusUpdateInterval;
function startStatusUpdateLoop(serverPort, serverStatusID, livePort, liveStatusID) {
// Clear existing intervals
if (serverStatusUpdateInterval || liveStatusUpdateInterval) {
clearInterval(serverStatusUpdateInterval);
clearInterval(liveStatusUpdateInterval);
}
try {
document.getElementById("serverPortNum").textContent = `PORT ${serverPort}`;
document.getElementById("livePortNum").textContent = `PORT ${livePort}`;
} catch (errror) {
// console.log(error);
}
serverStatusUpdateInterval = setInterval(function () {
updateServerStatus(serverPort, serverStatusID);
}, 3000);
liveStatusUpdateInterval = setInterval(function () {
updateServerStatus(livePort, liveStatusID);
}, 3000);
}
loadSettings();