import { watch } from 'chokidar'; import { existsSync } from 'fs'; import { PATHS } from '../config/constants.js'; import { readJSON } from '../utils/fileUtils.js'; import { broadcast } from './websocket.js'; let watcher = null; // Inicializar file watcher export function initFileWatcher() { // Watch files for changes const filesToWatch = [PATHS.WORKERS].filter(p => existsSync(p)); if (filesToWatch.length === 0) { return; } watcher = watch(filesToWatch, { persistent: true, ignoreInitial: true, }); watcher.on('change', async (path) => { console.log(`Archivo cambiado: ${path}`); if (path === PATHS.WORKERS) { const workers = readJSON(PATHS.WORKERS); broadcast({ type: 'workers_updated', data: workers }); } }); console.log('✅ File watcher inicializado'); } // Detener file watcher export function stopFileWatcher() { if (watcher) { watcher.close(); watcher = null; } }