Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"scripts": {
"dev": "vite",
"server": "bun run server/index.ts",
"server:debug": "bun run server/index.ts --debug",
"start": "concurrently \"bun run server\" \"bun run dev\"",
"start:debug": "concurrently \"bun run server:debug\" \"bun run dev\"",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
Expand Down
20 changes: 20 additions & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import { promisify } from "util";

const execAsync = promisify(exec);

// Parse command-line flags
const args = process.argv.slice(2);
const DEBUG = args.includes("--debug");

function debug(...messages: unknown[]) {
if (DEBUG) {
console.log(`[DEBUG ${new Date().toISOString()}]`, ...messages);
}
}

if (DEBUG) {
console.log("🐛 Debug mode enabled");
}

interface ProcessInfo {
pid: number;
user: string;
Expand Down Expand Up @@ -48,6 +62,7 @@ interface SystemMetrics {
let prevCpuTimes: { user: number; nice: number; sys: number; idle: number; irq: number }[] = [];

function getCpuUsage(): CpuUsage[] {
debug("Collecting CPU usage");
const cpuInfo = cpus();
const usage: CpuUsage[] = [];

Expand Down Expand Up @@ -92,6 +107,7 @@ function getCpuUsage(): CpuUsage[] {
}

async function getProcesses(): Promise<ProcessInfo[]> {
debug("Fetching process list");
const os = platform();
let command: string;

Expand Down Expand Up @@ -128,6 +144,7 @@ async function getProcesses(): Promise<ProcessInfo[]> {
}
}

debug(`Found ${processes.length} processes`);
return processes;
} catch (error) {
console.error("Error getting processes:", error);
Expand Down Expand Up @@ -229,6 +246,7 @@ async function getMemoryInfo(): Promise<{ total: number; free: number; used: num
}

async function getSystemMetrics(): Promise<SystemMetrics> {
debug("Collecting system metrics");
const cpuInfo = cpus();
const memInfo = await getMemoryInfo();
const processes = await getProcesses();
Expand Down Expand Up @@ -264,6 +282,8 @@ const server = Bun.serve({
"Access-Control-Allow-Headers": "Content-Type",
};

debug(`${req.method} ${url.pathname}`);

if (req.method === "OPTIONS") {
return new Response(null, { headers: corsHeaders });
}
Expand Down