Reaperxxxx commited on
Commit
2a3ed54
·
verified ·
1 Parent(s): bf21892

Create terminal.js

Browse files
Files changed (1) hide show
  1. public/terminal.js +97 -0
public/terminal.js ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener("DOMContentLoaded", () => {
2
+ const username = localStorage.getItem("username");
3
+ if (!username) {
4
+ alert("You need to log in first.");
5
+ window.location.href = "/";
6
+ return;
7
+ }
8
+
9
+ const terminal = document.getElementById("terminal");
10
+ const commandInput = document.getElementById("command");
11
+
12
+ const appendToTerminal = (text) => {
13
+ const line = document.createElement("div");
14
+ line.textContent = text;
15
+ terminal.appendChild(line);
16
+ terminal.scrollTop = terminal.scrollHeight;
17
+ };
18
+
19
+ const fetchLogs = async () => {
20
+ try {
21
+ const response = await fetch(`/logs?username=${username}`);
22
+ if (response.ok) {
23
+ const logs = await response.text();
24
+ terminal.innerHTML = "";
25
+ logs.split("\n").forEach((line) => {
26
+ if (line.trim()) appendToTerminal(line);
27
+ });
28
+ } else {
29
+ appendToTerminal("Failed to fetch logs.");
30
+ }
31
+ } catch (error) {
32
+ appendToTerminal("Error fetching logs.");
33
+ }
34
+ };
35
+
36
+ const sendCommand = async (command) => {
37
+ appendToTerminal(`$ ${command}`);
38
+ try {
39
+ const response = await fetch("/execute", {
40
+ method: "POST",
41
+ headers: { "Content-Type": "application/json" },
42
+ body: JSON.stringify({ username, command }),
43
+ });
44
+ if (response.ok) {
45
+ fetchLogs();
46
+ } else {
47
+ appendToTerminal("Error executing command.");
48
+ }
49
+ } catch (error) {
50
+ appendToTerminal("Command execution failed.");
51
+ }
52
+ };
53
+
54
+ document.getElementById("send").addEventListener("click", () => {
55
+ const command = commandInput.value.trim();
56
+ if (command) {
57
+ sendCommand(command);
58
+ commandInput.value = "";
59
+ }
60
+ });
61
+
62
+ document.getElementById("start").addEventListener("click", () => {
63
+ sendCommand("yarn install && yarn start");
64
+ });
65
+
66
+ document.getElementById("stop").addEventListener("click", () => {
67
+ sendCommand("pkill -f node");
68
+ });
69
+
70
+ document.getElementById("restart").addEventListener("click", () => {
71
+ sendCommand("pkill -f node && yarn start");
72
+ });
73
+
74
+ document.getElementById("deploy").addEventListener("click", async () => {
75
+ const repoUrl = prompt("Enter the GitHub repository URL to deploy:");
76
+ if (repoUrl) {
77
+ try {
78
+ const response = await fetch("/deploy", {
79
+ method: "POST",
80
+ headers: { "Content-Type": "application/json" },
81
+ body: JSON.stringify({ username, repoUrl }),
82
+ });
83
+ if (response.ok) {
84
+ appendToTerminal("Deployment started...");
85
+ fetchLogs();
86
+ } else {
87
+ appendToTerminal("Error starting deployment.");
88
+ }
89
+ } catch (error) {
90
+ appendToTerminal("Deployment failed.");
91
+ }
92
+ }
93
+ });
94
+
95
+ // Fetch logs on load
96
+ fetchLogs();
97
+ });