Create script.js
Browse files- public/script.js +132 -0
public/script.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 2 |
+
const socket = io();
|
| 3 |
+
const outputEl = document.getElementById('output');
|
| 4 |
+
const statusEl = document.getElementById('status');
|
| 5 |
+
const commandInput = document.getElementById('commandInput');
|
| 6 |
+
const fileListEl = document.getElementById('fileList');
|
| 7 |
+
const fileEditor = document.getElementById('fileEditor');
|
| 8 |
+
const currentFile = { path: null };
|
| 9 |
+
|
| 10 |
+
// Update status indicator
|
| 11 |
+
socket.on('connect', () => {
|
| 12 |
+
statusEl.textContent = 'Connected';
|
| 13 |
+
statusEl.className = 'status-indicator connected';
|
| 14 |
+
});
|
| 15 |
+
|
| 16 |
+
socket.on('disconnect', () => {
|
| 17 |
+
statusEl.textContent = 'Disconnected';
|
| 18 |
+
statusEl.className = 'status-indicator disconnected';
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
// Handle output from server
|
| 22 |
+
socket.on('output', (data) => {
|
| 23 |
+
outputEl.innerHTML += data + '\n';
|
| 24 |
+
outputEl.scrollTop = outputEl.scrollHeight;
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
// Handle command input
|
| 28 |
+
commandInput.addEventListener('keypress', (e) => {
|
| 29 |
+
if (e.key === 'Enter') {
|
| 30 |
+
const command = commandInput.value.trim();
|
| 31 |
+
if (command) {
|
| 32 |
+
outputEl.innerHTML += `$ ${command}\n`;
|
| 33 |
+
socket.emit('command', command);
|
| 34 |
+
commandInput.value = '';
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
// Clone repository
|
| 40 |
+
document.getElementById('cloneBtn').addEventListener('click', () => {
|
| 41 |
+
const repoUrl = document.getElementById('repoUrl').value.trim();
|
| 42 |
+
if (repoUrl) {
|
| 43 |
+
socket.emit('clone', repoUrl);
|
| 44 |
+
}
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
// Install dependencies
|
| 48 |
+
document.getElementById('installBtn').addEventListener('click', () => {
|
| 49 |
+
const packageManager = document.getElementById('packageManager').value;
|
| 50 |
+
socket.emit('install', packageManager);
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
// Start process
|
| 54 |
+
document.getElementById('startBtn').addEventListener('click', () => {
|
| 55 |
+
const command = document.getElementById('startCommand').value.trim();
|
| 56 |
+
if (command) {
|
| 57 |
+
socket.emit('start', command);
|
| 58 |
+
}
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
// Stop process
|
| 62 |
+
document.getElementById('stopBtn').addEventListener('click', () => {
|
| 63 |
+
socket.emit('stop');
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
// PM2 commands
|
| 67 |
+
document.getElementById('pm2Btn').addEventListener('click', () => {
|
| 68 |
+
const command = document.getElementById('pm2Command').value;
|
| 69 |
+
socket.emit('pm2', command);
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
// File browser functionality
|
| 73 |
+
function loadFileList() {
|
| 74 |
+
socket.emit('list-files', '', (response) => {
|
| 75 |
+
if (response.error) {
|
| 76 |
+
outputEl.innerHTML += `Error listing files: ${response.error}\n`;
|
| 77 |
+
return;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
fileListEl.innerHTML = '';
|
| 81 |
+
response.files.forEach(file => {
|
| 82 |
+
const fileEl = document.createElement('div');
|
| 83 |
+
fileEl.className = 'file-item';
|
| 84 |
+
fileEl.textContent = file;
|
| 85 |
+
fileEl.addEventListener('click', () => {
|
| 86 |
+
socket.emit('read-file', file, (fileResponse) => {
|
| 87 |
+
if (fileResponse.error) {
|
| 88 |
+
outputEl.innerHTML += `Error reading file: ${fileResponse.error}\n`;
|
| 89 |
+
return;
|
| 90 |
+
}
|
| 91 |
+
currentFile.path = file;
|
| 92 |
+
fileEditor.value = fileResponse.content;
|
| 93 |
+
});
|
| 94 |
+
});
|
| 95 |
+
fileListEl.appendChild(fileEl);
|
| 96 |
+
});
|
| 97 |
+
});
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
// Save file
|
| 101 |
+
document.getElementById('saveFileBtn').addEventListener('click', () => {
|
| 102 |
+
if (!currentFile.path) return;
|
| 103 |
+
|
| 104 |
+
socket.emit('write-file', {
|
| 105 |
+
filePath: currentFile.path,
|
| 106 |
+
content: fileEditor.value
|
| 107 |
+
}, (response) => {
|
| 108 |
+
if (response.error) {
|
| 109 |
+
outputEl.innerHTML += `Error saving file: ${response.error}\n`;
|
| 110 |
+
} else {
|
| 111 |
+
outputEl.innerHTML += `File ${currentFile.path} saved successfully\n`;
|
| 112 |
+
}
|
| 113 |
+
});
|
| 114 |
+
});
|
| 115 |
+
|
| 116 |
+
// Admin controls
|
| 117 |
+
document.getElementById('banBtn').addEventListener('click', () => {
|
| 118 |
+
const password = document.getElementById('adminPassword').value;
|
| 119 |
+
if (!password) return;
|
| 120 |
+
|
| 121 |
+
socket.emit('admin', {
|
| 122 |
+
password: password,
|
| 123 |
+
command: 'ban'
|
| 124 |
+
});
|
| 125 |
+
});
|
| 126 |
+
|
| 127 |
+
// Initial load
|
| 128 |
+
loadFileList();
|
| 129 |
+
|
| 130 |
+
// Focus command input on page load
|
| 131 |
+
commandInput.focus();
|
| 132 |
+
});
|