Repository navigation
Expand file tree
/
Copy pathdev.js
More file actions
168 lines (145 loc) · 6.55 KB
/
Copy pathdev.js
File metadata and controls
168 lines (145 loc) · 6.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
const { spawn } = require('child_process');
const readline = require('readline');
// Configuration for your services
const services = {
frontend: { path: './frontend', command: 'pnpm', args: ['dev'], process: null, logs: [], newLine: true },
backend: { path: './backend', command: 'pnpm', args: ['dev'], process: null, logs: [], newLine: true }
};
const MAX_LOGS = 250; // Increased buffer for better history
let currentView = 'menu'; // 'menu', 'frontend', 'backend'
// Setup standard input for keyboard interactions
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
function renderUI() {
console.clear();
if (currentView === 'menu') {
console.log('\x1b[36m=================================================\x1b[0m');
console.log('\x1b[1m\x1b[36m DEV ENVIRONMENT MANAGER \x1b[0m');
console.log('\x1b[36m=================================================\x1b[0m');
console.log(' \x1b[1m\x1b[33m[f]\x1b[0m View Frontend Logs');
console.log(' \x1b[1m\x1b[33m[b]\x1b[0m View Backend Logs');
console.log(' \x1b[1m\x1b[33m[r]\x1b[0m Restart All Services');
console.log(' \x1b[1m\x1b[33m[q]\x1b[0m Quit All & Exit');
console.log('\x1b[36m=================================================\x1b[0m');
console.log('\x1b[1mStatus:\x1b[0m');
console.log(` \x1b[1mFrontend:\x1b[0m ${services.frontend.process ? '\x1b[32mRunning ●\x1b[0m' : '\x1b[31mStopped ○\x1b[0m'}`);
console.log(` \x1b[1mBackend:\x1b[0m ${services.backend.process ? '\x1b[32mRunning ●\x1b[0m' : '\x1b[31mStopped ○\x1b[0m'}`);
console.log('\x1b[36m=================================================\x1b[0m');
} else {
// Render specific service logs
const color = currentView === 'frontend' ? '\x1b[36m' : '\x1b[35m'; // Cyan for frontend, Magenta for backend
console.log(`\n${color}=== Viewing ${currentView.toUpperCase()} Logs ===\x1b[0m`);
console.log(' \x1b[1m\x1b[33m[m]\x1b[0m Back to Menu | \x1b[1m\x1b[33m[c]\x1b[0m Clear Logs | \x1b[1m\x1b[33m[r]\x1b[0m Restart this service | \x1b[1m\x1b[33m[q]\x1b[0m Quit');
console.log(`${color}-------------------------------------------------\x1b[0m\n`);
// Print all stored logs
services[currentView].logs.forEach(log => process.stdout.write(log));
}
}
function handleLog(serviceName, data, isError = false) {
const service = services[serviceName];
let text = data.toString();
const lines = text.split('\n');
let output = '';
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (service.newLine && line.length > 0) {
const timestamp = new Date().toLocaleTimeString('en-US', { hour12: false });
const prefixColor = serviceName === 'frontend' ? '\x1b[1m\x1b[36m' : '\x1b[1m\x1b[35m';
const prefix = `${prefixColor}[${serviceName.toUpperCase()}]\x1b[0m \x1b[90m${timestamp}\x1b[0m | `;
// If the line has no ANSI colors, we apply our own formatting based on keywords
let cleanLine = line;
if (!/\x1b\[/.test(line)) {
const lower = line.toLowerCase();
if (isError || lower.includes('error') || lower.includes('fail') || lower.includes('exception')) {
cleanLine = `\x1b[31m${line}\x1b[0m`; // Red
} else if (lower.includes('warn')) {
cleanLine = `\x1b[33m${line}\x1b[0m`; // Yellow
} else if (lower.includes('success') || lower.includes('ready') || lower.includes('compiled') || lower.includes('listening on')) {
cleanLine = `\x1b[32m${line}\x1b[0m`; // Green
} else if (lower.includes('info')) {
cleanLine = `\x1b[36m${line}\x1b[0m`; // Cyan
}
}
output += prefix + cleanLine;
} else {
// For lines that are continuation of a previous chunk without newline, just append them
// or if it is an empty line, just output it.
output += line;
}
if (i < lines.length - 1) {
output += '\n';
service.newLine = true;
} else {
service.newLine = (line === '');
}
}
service.logs.push(output);
if (service.logs.length > MAX_LOGS) service.logs.shift();
if (currentView === serviceName) {
process.stdout.write(output);
}
}
function startService(name) {
const svc = services[name];
if (svc.process) return;
svc.process = spawn(svc.command, svc.args, {
cwd: svc.path,
shell: true,
stdio: 'pipe'
});
svc.process.stdout.on('data', (data) => handleLog(name, data, false));
svc.process.stderr.on('data', (data) => handleLog(name, data, true));
svc.process.on('close', () => {
svc.process = null;
handleLog(name, `\n\x1b[1m\x1b[31m[SYSTEM] Service ${name} exited.\x1b[0m\n`, true);
if (currentView === name || currentView === 'menu') renderUI();
});
}
function restartService(name) {
const svc = services[name];
if (svc.process) {
svc.process.kill();
setTimeout(() => {
handleLog(name, `\n\x1b[1m\x1b[33m[SYSTEM] Restarting ${name}...\x1b[0m\n`);
startService(name);
if (currentView === name) renderUI();
}, 1000);
} else {
startService(name);
}
}
function killAll() {
Object.values(services).forEach(svc => {
if (svc.process) svc.process.kill();
});
console.clear();
console.log('\x1b[1m\x1b[32mShutting down services gracefully...\x1b[0m');
process.exit(0);
}
// Input routing logic
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') killAll();
if (currentView === 'menu') {
switch (key.name) {
case 'f': currentView = 'frontend'; renderUI(); break;
case 'b': currentView = 'backend'; renderUI(); break;
case 'r': restartService('frontend'); restartService('backend'); renderUI(); break;
case 'q': killAll(); break;
}
} else {
switch (key.name) {
case 'm': currentView = 'menu'; renderUI(); break;
case 'c':
services[currentView].logs = [];
services[currentView].newLine = true;
renderUI();
break;
case 'r': restartService(currentView); break;
case 'q': killAll(); break;
}
}
});
// Boot sequence
startService('frontend');
startService('backend');
renderUI();