UNPKG

@hitomihiumi/filewatcher

Version:

A simple module that allows you to track changes in files of certain directories and certain extensions.

89 lines (88 loc) 3.19 kB
/// <reference types="node" /> import { EventEmitter } from "node:events"; /** * Class representing a file watcher. * @extends EventEmitter */ export declare class FileWatcher extends EventEmitter { private processId; private baseDir; private watchers; private dirHandlers; private ignoredDirectories; private allowedExtensions; private monitoredDirectories; private initialFiles; /** * Create a FileWatcher. * @param {number} [processId] - The process ID. */ constructor(processId?: number); /** * Watch a directory for changes. * @private * @param {string} directory - The directory to watch. */ private watchDirectory; /** * Find a handler for a specific event type in a directory. * @private * @param {string} directory - The directory to find the handler for. * @param {"add" | "change" | "unlink"} eventType - The event type. * @returns {Function | undefined} The handler function or undefined if not found. */ private findHandler; /** * Handle a file system event. * @private * @param {"add" | "change" | "unlink"} eventType - The event type. * @param {string} directory - The directory where the event occurred. * @param {string} filePath - The path of the file that triggered the event. */ private handleEvent; /** * Capture the initial files in a directory. * @private * @param {string} directory - The directory to capture initial files from. */ private captureInitialFiles; /** * Start watching the directories. */ startWatching(): this; /** * Stop watching all directories. */ stopWatching(): this; /** * Set a handler for a specific event type in a directory. * @param {string} directory - The directory to set the handler for. * @param {"add" | "change" | "unlink"} eventType - The event type. * @param {Function} callback - The handler function. */ setHandler(directory: string, eventType: "add" | "change" | "unlink", callback: (directory: string, filename: string, relativePath: string, eventType: "add" | "change" | "unlink") => void): this; /** * Ignore specific directories. * @param {...string} directory - The directories to ignore. * @returns {FileWatcher} The current FileWatcher instance. */ ignoreDirectory(...directory: string[]): this; /** * Unignore specific directories. * @param {...string} directory - The directories to unignore. * @returns {FileWatcher} The current FileWatcher instance. */ unignoreDirectory(...directory: string[]): this; /** * Set the allowed file extensions to watch. * @param {...string} extensions - The file extensions to allow. * @returns {FileWatcher} The current FileWatcher instance. */ setAllowedExtensions(...extensions: string[]): this; /** * Set the directories to monitor. * @param {...string} directories - The directories to monitor. * @returns {FileWatcher} The current FileWatcher instance. */ setMonitoredDirectories(...directories: string[]): this; }