File size: 1,522 Bytes
fd8cdf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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();
        },
    };
}