Project File Watcher (server/fileWatcher.ts)

JE4NVRG/hyperframes · packages/cli/src/server/fileWatcher.ts · typescript · Apache-2.0
dev-tools, files, cli, automation, fw:node

Monitors project directories for file changes and triggers callbacks with debouncing. Use this to implement hot-reloading or auto-build features in CLI tools.

// Source: https://github.com/JE4NVRG/hyperframes | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
import { watch, type FSWatcher } from "node:fs";

export type FileChangeListener = (relativePath: string) => void;

export interface ProjectWatcher {
  addListener(fn: FileChangeListener): void;
  removeListener(fn: FileChangeListener): void;
  close(): void;
}

const WATCHED_EXTENSIONS = new Set([".html", ".css", ".js", ".json"]);
const DEBOUNCE_MS = 300;

export function createProjectWatcher(projectDir: string): ProjectWatcher {
  const listeners = new Set<FileChangeListener>();
  let debounceTimer: ReturnType<typeof setTimeout> | null = null;
  let watcher: FSWatcher | null = null;

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key