UNPKG

@nodedaemon/core

Version:

Production-ready Node.js process manager with zero external dependencies

187 lines â€ĸ 7.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Formatter = void 0; const helpers_1 = require("../utils/helpers"); class Formatter { static formatTable(data, columns) { if (data.length === 0) { return 'No data to display'; } // Calculate column widths const widths = {}; columns.forEach(col => { let maxWidth = col.length; data.forEach(row => { const value = row[col] || ''; const len = String(value).length; if (len > maxWidth) { maxWidth = len; } }); widths[col] = maxWidth; }); // Create header const header = columns.map(col => col.toUpperCase().padEnd(widths[col])).join(' '); const separator = columns.map(col => '-'.repeat(widths[col])).join(' '); // Create rows const rows = data.map(row => columns.map(col => String(row[col] || '').padEnd(widths[col])).join(' ')); return [header, separator, ...rows].join('\n'); } static formatProcessList(processes) { if (processes.length === 0) { return 'No processes running'; } const formatted = processes.map(proc => ({ NAME: proc.name, ID: proc.id.substring(0, 8), STATUS: this.formatStatus(proc.status), INSTANCES: proc.instances, RESTARTS: proc.restarts, UPTIME: (0, helpers_1.formatUptime)(proc.uptime), MEMORY: (0, helpers_1.formatMemory)(proc.memory), CPU: `${proc.cpu.toFixed(1)}%` })); return this.formatTable(formatted, [ 'NAME', 'ID', 'STATUS', 'INSTANCES', 'RESTARTS', 'UPTIME', 'MEMORY', 'CPU' ]); } static formatProcessStatus(process) { const lines = []; lines.push(`Process: ${process.name} (${process.id.substring(0, 8)})`); lines.push(`Script: ${process.script}`); lines.push(`Status: ${this.formatStatus(process.status)}`); lines.push(`Instances: ${process.instances.length}`); lines.push(`Restarts: ${process.restarts}`); lines.push(`Created: ${new Date(process.createdAt).toISOString()}`); lines.push(`Updated: ${new Date(process.updatedAt).toISOString()}`); if (process.instances.length > 0) { lines.push('\nInstances:'); process.instances.forEach((instance, index) => { lines.push(` ${index + 1}: PID ${instance.pid || 'N/A'} - ${this.formatStatus(instance.status)}`); if (instance.uptime) { lines.push(` Uptime: ${(0, helpers_1.formatUptime)(Date.now() - instance.uptime)}`); } if (instance.memory) { lines.push(` Memory: ${(0, helpers_1.formatMemory)(instance.memory)}`); } if (instance.restarts > 0) { lines.push(` Restarts: ${instance.restarts}`); } }); } lines.push('\nConfiguration:'); if (process.config.instances) { lines.push(` Instances: ${process.config.instances}`); } if (process.config.watch) { lines.push(` Watch: ${Array.isArray(process.config.watch) ? process.config.watch.join(', ') : 'true'}`); } if (process.config.env) { lines.push(` Environment: ${Object.keys(process.config.env).join(', ')}`); } if (process.config.cwd) { lines.push(` Working Dir: ${process.config.cwd}`); } if (process.config.args && process.config.args.length > 0) { lines.push(` Arguments: ${process.config.args.join(' ')}`); } if (process.config.maxMemory) { lines.push(` Max Memory: ${process.config.maxMemory}`); } if (process.config.maxRestarts) { lines.push(` Max Restarts: ${process.config.maxRestarts}`); } return lines.join('\n'); } static formatDaemonStatus(status) { const lines = []; lines.push(`NodeDaemon Status`); lines.push(`================`); lines.push(`PID: ${status.daemon.pid}`); lines.push(`Version: ${status.daemon.version}`); lines.push(`Uptime: ${(0, helpers_1.formatUptime)(status.daemon.uptime)}`); lines.push(`Processes: ${status.daemon.processCount}`); lines.push(` Running: ${status.daemon.runningProcesses}`); lines.push(` Stopped: ${status.daemon.stoppedProcesses}`); lines.push(` Errored: ${status.daemon.erroredProcesses}`); if (status.health && status.health.length > 0) { lines.push('\nHealth Checks:'); status.health.forEach((health) => { const healthStatus = health.healthy ? '✓ Healthy' : '✗ Unhealthy'; lines.push(` ${health.processId.substring(0, 8)}: ${healthStatus}`); if (health.memory) { lines.push(` Memory: ${(0, helpers_1.formatMemory)(health.memory)}`); } if (health.uptime) { lines.push(` Uptime: ${(0, helpers_1.formatUptime)(health.uptime)}`); } if (health.issues && health.issues.length > 0) { lines.push(` Issues: ${health.issues.join(', ')}`); } }); } return lines.join('\n'); } static formatLogs(logs) { if (logs.length === 0) { return 'No logs available'; } return logs .sort((a, b) => a.timestamp - b.timestamp) .map(log => { const timestamp = new Date(log.timestamp).toISOString(); const level = log.level.toUpperCase().padEnd(5); const processInfo = log.processId ? `[${log.processId.substring(0, 8)}]` : '[daemon]'; const data = log.data ? ` ${JSON.stringify(log.data)}` : ''; return `${timestamp} ${level} ${processInfo} ${log.message}${data}`; }) .join('\n'); } static formatStatus(status) { const colors = { running: 'đŸŸĸ', stopped: '🔴', starting: '🟡', stopping: '🟡', errored: '🔴', crashed: '🔴' }; const icon = colors[status] || 'âšĒ'; return `${icon} ${status.toUpperCase()}`; } static formatSuccess(message) { return `✅ ${message}`; } static formatError(message) { return `❌ ${message}`; } static formatWarning(message) { return `âš ī¸ ${message}`; } static formatInfo(message) { return `â„šī¸ ${message}`; } static colorize(text, color) { const colors = { red: '\x1b[31m', green: '\x1b[32m', yellow: '\x1b[33m', blue: '\x1b[34m', cyan: '\x1b[36m', white: '\x1b[37m' }; const reset = '\x1b[0m'; return `${colors[color]}${text}${reset}`; } static bold(text) { return `\x1b[1m${text}\x1b[0m`; } static dim(text) { return `\x1b[2m${text}\x1b[0m`; } // Re-export utility functions static formatMemory = helpers_1.formatMemory; static formatUptime = helpers_1.formatUptime; } exports.Formatter = Formatter; //# sourceMappingURL=Formatter.js.map