numux 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ import { type FSWatcher, watch } from 'node:fs'
2
+ import { log } from './logger'
3
+
4
+ const DEBOUNCE_MS = 300
5
+ const IGNORED_SEGMENTS = new Set(['node_modules', '.git'])
6
+
7
+ export class FileWatcher {
8
+ private watchers: FSWatcher[] = []
9
+ private debounceTimers = new Map<string, ReturnType<typeof setTimeout>>()
10
+
11
+ watch(name: string, patterns: string[], cwd: string, onChanged: (path: string) => void): void {
12
+ const globs = patterns.map(p => new Bun.Glob(p))
13
+
14
+ try {
15
+ const watcher = watch(cwd, { recursive: true }, (_event, filename) => {
16
+ if (!filename) return
17
+
18
+ // Skip node_modules, .git, etc.
19
+ const segments = filename.split('/')
20
+ if (segments.some(s => IGNORED_SEGMENTS.has(s))) return
21
+
22
+ // Check if changed file matches any pattern
23
+ if (!globs.some(g => g.match(filename))) return
24
+
25
+ // Debounce per process
26
+ const existing = this.debounceTimers.get(name)
27
+ if (existing) clearTimeout(existing)
28
+ this.debounceTimers.set(
29
+ name,
30
+ setTimeout(() => {
31
+ this.debounceTimers.delete(name)
32
+ onChanged(filename)
33
+ }, DEBOUNCE_MS)
34
+ )
35
+ })
36
+ this.watchers.push(watcher)
37
+ log(`[${name}] Watching: ${patterns.join(', ')}`)
38
+ } catch (err) {
39
+ log(`[${name}] Failed to set up file watcher: ${err}`)
40
+ }
41
+ }
42
+
43
+ close(): void {
44
+ for (const timer of this.debounceTimers.values()) {
45
+ clearTimeout(timer)
46
+ }
47
+ this.debounceTimers.clear()
48
+ for (const watcher of this.watchers) {
49
+ watcher.close()
50
+ }
51
+ this.watchers = []
52
+ }
53
+ }