/** * ============================================ * 🔧 utils.js — Utilidades compartidas * ============================================ */ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); export function readConfig() { const raw = fs.readFileSync(path.join(__dirname, '../config.json'), 'utf-8'); return JSON.parse(raw); } export function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } export function formatDuration(ms) { const s = Math.floor(ms / 1000); const m = Math.floor(s / 60); const h = Math.floor(m / 60); if (h > 0) return `${h}h ${m % 60}m ${s % 60}s`; if (m > 0) return `${m}m ${s % 60}s`; return `${s}s`; } export function truncate(str, max = 200) { if (!str) return ''; return str.length > max ? str.substring(0, max) + '...' : str; } export function generateId() { return `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; } // Formatear timestamp legible export function formatDate(isoString) { if (!isoString) return 'desconocido'; return new Date(isoString).toLocaleDateString('es-ES', { day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit', }); }