Update backend/terminalManager.js
Browse files- backend/terminalManager.js +61 -5
backend/terminalManager.js
CHANGED
@@ -1,28 +1,84 @@
|
|
1 |
const { spawn } = require("child_process");
|
|
|
|
|
|
|
2 |
const terminals = {};
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
function handleTerminal(directoryName, userDir, stop = false) {
|
|
|
5 |
if (stop) {
|
6 |
if (terminals[directoryName]) {
|
7 |
terminals[directoryName].kill();
|
8 |
delete terminals[directoryName];
|
|
|
|
|
|
|
9 |
}
|
10 |
return;
|
11 |
}
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
terminals[directoryName] = terminal;
|
15 |
|
|
|
|
|
|
|
16 |
terminal.stdout.on("data", (data) => {
|
17 |
-
console.log(`[${directoryName}] ${data.toString()}`);
|
18 |
});
|
19 |
|
|
|
20 |
terminal.stderr.on("data", (data) => {
|
21 |
-
console.error(`[${directoryName}] ${data.toString()}`);
|
22 |
});
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
delete terminals[directoryName];
|
27 |
});
|
28 |
}
|
|
|
1 |
const { spawn } = require("child_process");
|
2 |
+
const fs = require("fs");
|
3 |
+
const path = require("path");
|
4 |
+
|
5 |
const terminals = {};
|
6 |
|
7 |
+
/**
|
8 |
+
* Starts or stops a terminal process for a given directory.
|
9 |
+
* @param {string} directoryName - The name of the user directory.
|
10 |
+
* @param {string|null} userDir - The path to the user directory.
|
11 |
+
* @param {boolean} stop - Whether to stop the running process.
|
12 |
+
*/
|
13 |
function handleTerminal(directoryName, userDir, stop = false) {
|
14 |
+
// Stop the running process if requested
|
15 |
if (stop) {
|
16 |
if (terminals[directoryName]) {
|
17 |
terminals[directoryName].kill();
|
18 |
delete terminals[directoryName];
|
19 |
+
console.log(`[${directoryName}] Terminal stopped successfully.`);
|
20 |
+
} else {
|
21 |
+
console.warn(`[${directoryName}] No running terminal to stop.`);
|
22 |
}
|
23 |
return;
|
24 |
}
|
25 |
|
26 |
+
// Validate user directory path
|
27 |
+
if (!userDir || !fs.existsSync(userDir)) {
|
28 |
+
console.error(`[${directoryName}] Error: User directory does not exist at ${userDir}`);
|
29 |
+
return;
|
30 |
+
}
|
31 |
+
|
32 |
+
// Validate index.js existence
|
33 |
+
const indexPath = path.join(userDir, "index.js");
|
34 |
+
if (!fs.existsSync(indexPath)) {
|
35 |
+
console.error(`[${directoryName}] Error: index.js not found in ${userDir}`);
|
36 |
+
return;
|
37 |
+
}
|
38 |
+
|
39 |
+
// Check if a process is already running
|
40 |
+
if (terminals[directoryName]) {
|
41 |
+
console.warn(`[${directoryName}] Warning: A terminal is already running.`);
|
42 |
+
return;
|
43 |
+
}
|
44 |
+
|
45 |
+
// Use the correct Node.js executable
|
46 |
+
const nodePath = process.execPath;
|
47 |
+
|
48 |
+
// Spawn a new terminal process
|
49 |
+
const terminal = spawn(nodePath, ["index.js"], {
|
50 |
+
cwd: userDir,
|
51 |
+
env: process.env,
|
52 |
+
stdio: "pipe",
|
53 |
+
});
|
54 |
+
|
55 |
terminals[directoryName] = terminal;
|
56 |
|
57 |
+
console.log(`[${directoryName}] Terminal process started.`);
|
58 |
+
|
59 |
+
// Handle standard output
|
60 |
terminal.stdout.on("data", (data) => {
|
61 |
+
console.log(`[${directoryName}] STDOUT: ${data.toString()}`);
|
62 |
});
|
63 |
|
64 |
+
// Handle standard error output
|
65 |
terminal.stderr.on("data", (data) => {
|
66 |
+
console.error(`[${directoryName}] STDERR: ${data.toString()}`);
|
67 |
});
|
68 |
|
69 |
+
// Handle process errors (e.g., spawn issues)
|
70 |
+
terminal.on("error", (err) => {
|
71 |
+
console.error(`[${directoryName}] Process error: ${err.message}`);
|
72 |
+
delete terminals[directoryName];
|
73 |
+
});
|
74 |
+
|
75 |
+
// Handle process exit
|
76 |
+
terminal.on("exit", (code, signal) => {
|
77 |
+
if (code !== 0) {
|
78 |
+
console.error(`[${directoryName}] Terminal exited with code ${code} and signal ${signal}`);
|
79 |
+
} else {
|
80 |
+
console.log(`[${directoryName}] Terminal exited successfully.`);
|
81 |
+
}
|
82 |
delete terminals[directoryName];
|
83 |
});
|
84 |
}
|