()
| 469 | } |
| 470 | |
| 471 | function refreshWatchers(): void { |
| 472 | if (!running) return; |
| 473 | const desiredDirs = new Set<string>(); |
| 474 | |
| 475 | for (const target of watchTargets) { |
| 476 | const root = toWatchDirectory(target); |
| 477 | if (containsIgnoredDir(root)) continue; |
| 478 | const dirs = collectWatchDirectories(root); |
| 479 | for (const dir of dirs) { |
| 480 | desiredDirs.add(dir); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | for (const watchedDir of watchers.keys()) { |
| 485 | if (desiredDirs.has(watchedDir)) continue; |
| 486 | const watcher = watchers.get(watchedDir); |
| 487 | watchers.delete(watchedDir); |
| 488 | try { |
| 489 | watcher?.close(); |
| 490 | } catch { |
| 491 | // ignore watcher close races |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | for (const dir of desiredDirs) { |
| 496 | if (watchers.has(dir)) continue; |
| 497 | try { |
| 498 | const watcher = watch(dir, { persistent: true }, (eventType, filename) => { |
| 499 | onWatchEvent(dir, eventType, filename ?? null); |
| 500 | }); |
| 501 | watcher.on("error", (error: unknown) => { |
| 502 | emitError(error, { phase: "watch", changedPath: dir }); |
| 503 | log({ level: "warn", message: "HSR watcher reported an error", changedPath: dir }); |
| 504 | scheduleWatchRefresh(); |
| 505 | }); |
| 506 | watchers.set(dir, watcher); |
| 507 | } catch (error: unknown) { |
| 508 | emitError(error, { phase: "watch", changedPath: dir }); |
| 509 | log({ level: "warn", message: "HSR failed to watch path", changedPath: dir }); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | async function start(): Promise<void> { |
| 515 | if (running) return; |
no test coverage detected