import * as fs from 'node:fs'; import { resolveInput } from './input-resolver.js'; import { log } from './logger.js'; export function startWatcher(options) { const { inputPath, onManifestUpdate } = options; let debounceTimer = null; const watcher = fs.watch(inputPath, { recursive: true }, (eventType, filename) => { // Ignore non-.json file changes if (!filename || !filename.endsWith('.json')) { return; } // Debounce: clear previous timer and set a new one if (debounceTimer !== null) { clearTimeout(debounceTimer); } debounceTimer = setTimeout(() => { debounceTimer = null; const result = resolveInput(inputPath); if (result.success) { const { manifest, fileMapping } = result; const projectCount = manifest.length; const graphFileCount = manifest.reduce((sum, entry) => sum + entry.graphFiles.length, 0); onManifestUpdate(manifest, fileMapping); log('info', `Manifest updated — ${projectCount} projects, ${graphFileCount} graph files`); } else { log('warn', `Failed to regenerate manifest: ${result.error}`); } }, 100); }); return { close() { if (debounceTimer !== null) { clearTimeout(debounceTimer); debounceTimer = null; } watcher.close(); }, }; }