-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-tui.js
More file actions
196 lines (172 loc) · 6.27 KB
/
server-tui.js
File metadata and controls
196 lines (172 loc) · 6.27 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
const readline = require('readline');
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec, spawn } = require('child_process');
const url = require('url');
let serverProcess = null;
let config = {
port: 8080,
rootDir: process.cwd()
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function clearScreen() {
console.clear();
}
function showHeader() {
console.log('\x1b[36m╔══════════════════════════════════════════╗\x1b[0m');
console.log('\x1b[36m║\x1b[0m \x1b[1;37mLocal Server Manager\x1b[0m \x1b[36m║\x1b[0m');
console.log('\x1b[36m╚══════════════════════════════════════════╝\x1b[0m');
}
function showMenu() {
console.log('\n\x1b[33m[1]\x1b[0m Start Server');
console.log('\x1b[33m[2]\x1b[0m Stop Server');
console.log('\x1b[33m[3]\x1b[0m Change Port');
console.log('\x1b[33m[4]\x1b[0m Change Directory');
console.log('\x1b[33m[5]\x1b[0m Open in Browser');
console.log('\x1b[33m[6]\x1b[0m Server Status');
console.log('\x1b[33m[7]\x1b[0m Settings');
console.log('\x1b[33m[0]\x1b[0m Exit\n');
console.log('\x1b[90m──────────────────────────────────────────\x1b[0m');
console.log(`\x1b[32mPort:\x1b[0m ${config.port} \x1b[32mDirectory:\x1b[0m ${config.rootDir}`);
console.log('\x1b[90m──────────────────────────────────────────\x1b[0m');
console.log(`\x1b[37mServer: ${serverProcess ? '\x1b[32mRunning\x1b[0m' : '\x1b[31mStopped\x1b[0m'}\x1b[0m\n`);
rl.question('\x1b[36mSelect option > \x1b[0m', handleInput);
}
function handleInput(answer) {
switch(answer.trim()) {
case '1':
startServer();
break;
case '2':
stopServer();
break;
case '3':
changePort();
break;
case '4':
changeDirectory();
break;
case '5':
openBrowser();
break;
case '6':
showStatus();
break;
case '7':
showSettings();
break;
case '0':
exitApp();
break;
default:
showMenu();
}
}
function startServer() {
if (serverProcess) {
console.log('\x1b[31mServer is already running!\x1b[0m\n');
return showMenu();
}
console.log(`\n\x1b[32mStarting server on port ${config.port}...\x1b[0m`);
console.log(`\x1b[90mServing from: ${config.rootDir}\x1b[0m\n`);
serverProcess = spawn('python', ['-m', 'http.server', config.port], {
cwd: config.rootDir,
stdio: ['ignore', 'pipe', 'pipe']
});
serverProcess.stdout.on('data', (data) => {
console.log(`\x1b[90m${data}\x1b[0m`);
});
serverProcess.stderr.on('data', (data) => {
console.log(`\x1b[90m${data}\x1b[0m`);
});
serverProcess.on('close', (code) => {
serverProcess = null;
console.log(`\x1b[31mServer stopped\x1b[0m\n`);
});
setTimeout(() => {
console.log(`\x1b[32m✓ Server running at http://localhost:${config.port}\x1b[0m\n`);
showMenu();
}, 500);
}
function stopServer() {
if (!serverProcess) {
console.log('\x1b[31mServer is not running!\x1b[0m\n');
return showMenu();
}
serverProcess.kill();
serverProcess = null;
console.log('\x1b[31m✓ Server stopped\x1b[0m\n');
showMenu();
}
function changePort() {
rl.question('\x1b[36mEnter port number > \x1b[0m', (port) => {
const p = parseInt(port);
if (p && p > 0 && p < 65536) {
config.port = p;
console.log(`\x1b[32mPort changed to ${p}\x1b[0m\n`);
} else {
console.log('\x1b[31mInvalid port number!\x1b[0m\n');
}
showMenu();
});
}
function changeDirectory() {
rl.question('\x1b[36mEnter directory path > \x1b[0m', (dir) => {
if (fs.existsSync(dir)) {
config.rootDir = dir;
console.log(`\x1b[32mDirectory changed to ${dir}\x1b[0m\n`);
} else {
console.log('\x1b[31mDirectory does not exist!\x1b[0m\n');
}
showMenu();
});
}
function openBrowser() {
const url = `http://localhost:${config.port}`;
if (process.platform === 'win32') {
exec(`start ${url}`);
} else if (process.platform === 'darwin') {
exec(`open ${url}`);
} else {
exec(`xdg-open ${url}`);
}
console.log(`\x1b[32mOpening ${url}...\x1b[0m\n`);
showMenu();
}
function showStatus() {
console.log('\n\x1b[37m╔══════════════ Server Status ══════════════╗\x1b[0m');
console.log(`\x1b[37m║\x1b[0m Status: ${serverProcess ? '\x1b[32mRunning\x1b[0m' : '\x1b[31mStopped\x1b[0m'} \x1b[37m║\x1b[0m`);
console.log(`\x1b[37m║\x1b[0m Port: ${config.port} \x1b[37m║\x1b[0m`);
console.log(`\x1b[37m║\x1b[0m Directory: ${config.rootDir.slice(0, 30)}... \x1b[37m║\x1b[0m`);
console.log(`\x1b[37m║\x1b[0m URL: http://localhost:${config.port} \x1b[37m║\x1b[0m`);
console.log('\x1b[37m╚══════════════════════════════════════════════╝\x1b[0m\n');
showMenu();
}
function showSettings() {
console.log('\n\x1b[37mCurrent Settings:\x1b[0m');
console.log(` Port: ${config.port}`);
console.log(` Directory: ${config.rootDir}`);
console.log(` Platform: ${process.platform}\n`);
showMenu();
}
function exitApp() {
if (serverProcess) {
serverProcess.kill();
}
console.log('\x1b[36mGoodbye!\x1b[0m\n');
process.exit(0);
}
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
exitApp();
}
});
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
clearScreen();
showHeader();
showMenu();