@nasriya/overwatch
Advanced tools
| export interface WatchOptions { | ||
| include?: (RegExp | string)[]; | ||
| exclude?: (RegExp | string)[]; | ||
| onChange?: onWatchedDataChangeHandler; | ||
| onUpdate?: OnWatchedDataUpdateHandler; | ||
| onRemove?: onWatchedDataRemoveHandler; | ||
| onRename?: onWatchedDataRenameHandler; | ||
| onAdd?: onWatchedDataAddHandler; | ||
| onRootRemoved?: () => any | Promise<any>; | ||
| } | ||
@@ -5,0 +11,0 @@ export type OnWatchedDataUpdateHandler = (event: UpdateEvent) => any | Promise<any>; |
@@ -7,10 +7,8 @@ import Watcher from "./watcher/Watcher"; | ||
| /** | ||
| * Watches a specific file or folder for changes. This method is internally used by | ||
| * `watchFile` and `watchFolder` and handles all registration logic with the VOM. | ||
| * Registers a watcher for the specified path, which can be a file or directory. | ||
| * | ||
| * @param path_ - The absolute or relative path to the file or folder to watch. | ||
| * @param path_ - The absolute or relative path to the file or directory to watch. | ||
| * The path will be resolved and normalized before being tracked. | ||
| * @param options - Optional filtering options to include or exclude specific files or folders. | ||
| * These only take effect if the path points to a directory. | ||
| * All filter rules (both strings and RegExp) are matched against the **absolute normalized paths** of files and subfolders. | ||
| * @param options - Optional configuration options for the watcher. | ||
| * If watching a directory, you can specify filters to include or exclude specific paths. | ||
| * @returns A `Watcher` instance for the specified path. | ||
@@ -25,17 +23,16 @@ * @throws Will throw an error if the path does not exist or cannot be resolved. | ||
| * The path will be resolved and normalized before being tracked. | ||
| * @returns A `Watcher` instance for the file. | ||
| * @throws Will throw an error if the path is invalid or not a file. | ||
| * @param options - Optional configuration options excluding `exclude` and `include` filters. | ||
| * These filters are irrelevant since a file is not a directory. | ||
| * @returns A `Watcher` instance for the specified file. | ||
| * @throws Will throw an error if the path does not exist or cannot be resolved. | ||
| */ | ||
| watchFile(path_: string): Promise<Watcher>; | ||
| watchFile(path_: string, options?: Exclude<WatchOptions, 'exclude' | 'include'>): Promise<Watcher>; | ||
| /** | ||
| * Registers a watcher for the specified folder path, with optional filtering rules | ||
| * to control which files or folders inside it should trigger change events. | ||
| * Registers a watcher for the specified directory path. | ||
| * | ||
| * @param path_ - The absolute or relative path to the folder to watch. | ||
| * @param path_ - The absolute or relative path to the directory to watch. | ||
| * The path will be resolved and normalized before being tracked. | ||
| * @param options - Filtering options to include or exclude specific paths. | ||
| * Filter rules may be exact strings or regular expressions, | ||
| * and are matched against the **absolute normalized path** of each affected file or folder. | ||
| * @returns A `Watcher` instance for the folder. | ||
| * @throws Will throw an error if the path is invalid or not a directory. | ||
| * @param options - Optional configuration options including filters to restrict which files and folders are watched. | ||
| * @returns A `Watcher` instance for the specified directory. | ||
| * @throws Will throw an error if the path does not exist or cannot be resolved. | ||
| */ | ||
@@ -42,0 +39,0 @@ watchFolder(path_: string, options?: WatchOptions): Promise<Watcher>; |
+14
-17
@@ -153,10 +153,8 @@ "use strict"; | ||
| /** | ||
| * Watches a specific file or folder for changes. This method is internally used by | ||
| * `watchFile` and `watchFolder` and handles all registration logic with the VOM. | ||
| * Registers a watcher for the specified path, which can be a file or directory. | ||
| * | ||
| * @param path_ - The absolute or relative path to the file or folder to watch. | ||
| * @param path_ - The absolute or relative path to the file or directory to watch. | ||
| * The path will be resolved and normalized before being tracked. | ||
| * @param options - Optional filtering options to include or exclude specific files or folders. | ||
| * These only take effect if the path points to a directory. | ||
| * All filter rules (both strings and RegExp) are matched against the **absolute normalized paths** of files and subfolders. | ||
| * @param options - Optional configuration options for the watcher. | ||
| * If watching a directory, you can specify filters to include or exclude specific paths. | ||
| * @returns A `Watcher` instance for the specified path. | ||
@@ -193,6 +191,8 @@ * @throws Will throw an error if the path does not exist or cannot be resolved. | ||
| * The path will be resolved and normalized before being tracked. | ||
| * @returns A `Watcher` instance for the file. | ||
| * @throws Will throw an error if the path is invalid or not a file. | ||
| * @param options - Optional configuration options excluding `exclude` and `include` filters. | ||
| * These filters are irrelevant since a file is not a directory. | ||
| * @returns A `Watcher` instance for the specified file. | ||
| * @throws Will throw an error if the path does not exist or cannot be resolved. | ||
| */ | ||
| async watchFile(path_) { | ||
| async watchFile(path_, options) { | ||
| try { | ||
@@ -210,12 +210,9 @@ const watcher = await this.watch(path_); | ||
| /** | ||
| * Registers a watcher for the specified folder path, with optional filtering rules | ||
| * to control which files or folders inside it should trigger change events. | ||
| * Registers a watcher for the specified directory path. | ||
| * | ||
| * @param path_ - The absolute or relative path to the folder to watch. | ||
| * @param path_ - The absolute or relative path to the directory to watch. | ||
| * The path will be resolved and normalized before being tracked. | ||
| * @param options - Filtering options to include or exclude specific paths. | ||
| * Filter rules may be exact strings or regular expressions, | ||
| * and are matched against the **absolute normalized path** of each affected file or folder. | ||
| * @returns A `Watcher` instance for the folder. | ||
| * @throws Will throw an error if the path is invalid or not a directory. | ||
| * @param options - Optional configuration options including filters to restrict which files and folders are watched. | ||
| * @returns A `Watcher` instance for the specified directory. | ||
| * @throws Will throw an error if the path does not exist or cannot be resolved. | ||
| */ | ||
@@ -222,0 +219,0 @@ async watchFolder(path_, options) { |
@@ -46,2 +46,20 @@ "use strict"; | ||
| this.#_filters.exclude = this.#_filters.exclude.map(rule => this.#_helpers.normalizeFilterRule(rule)); | ||
| if (utils_1.default.hasOwnProperty(watchOptions, 'onChange')) { | ||
| this.onChange(watchOptions?.onChange); | ||
| } | ||
| if (utils_1.default.hasOwnProperty(watchOptions, 'onUpdate')) { | ||
| this.onUpdate(watchOptions?.onUpdate); | ||
| } | ||
| if (utils_1.default.hasOwnProperty(watchOptions, 'onRemove')) { | ||
| this.onRemove(watchOptions?.onRemove); | ||
| } | ||
| if (utils_1.default.hasOwnProperty(watchOptions, 'onRename')) { | ||
| this.onRename(watchOptions?.onRename); | ||
| } | ||
| if (utils_1.default.hasOwnProperty(watchOptions, 'onAdd')) { | ||
| this.onAdd(watchOptions?.onAdd); | ||
| } | ||
| if (utils_1.default.hasOwnProperty(watchOptions, 'onRootRemoved')) { | ||
| this.onRootRemoved(watchOptions?.onRootRemoved); | ||
| } | ||
| } | ||
@@ -48,0 +66,0 @@ /** |
+14
-17
@@ -148,10 +148,8 @@ import VirtualObjectManager from "./vom/vom.js"; | ||
| /** | ||
| * Watches a specific file or folder for changes. This method is internally used by | ||
| * `watchFile` and `watchFolder` and handles all registration logic with the VOM. | ||
| * Registers a watcher for the specified path, which can be a file or directory. | ||
| * | ||
| * @param path_ - The absolute or relative path to the file or folder to watch. | ||
| * @param path_ - The absolute or relative path to the file or directory to watch. | ||
| * The path will be resolved and normalized before being tracked. | ||
| * @param options - Optional filtering options to include or exclude specific files or folders. | ||
| * These only take effect if the path points to a directory. | ||
| * All filter rules (both strings and RegExp) are matched against the **absolute normalized paths** of files and subfolders. | ||
| * @param options - Optional configuration options for the watcher. | ||
| * If watching a directory, you can specify filters to include or exclude specific paths. | ||
| * @returns A `Watcher` instance for the specified path. | ||
@@ -188,6 +186,8 @@ * @throws Will throw an error if the path does not exist or cannot be resolved. | ||
| * The path will be resolved and normalized before being tracked. | ||
| * @returns A `Watcher` instance for the file. | ||
| * @throws Will throw an error if the path is invalid or not a file. | ||
| * @param options - Optional configuration options excluding `exclude` and `include` filters. | ||
| * These filters are irrelevant since a file is not a directory. | ||
| * @returns A `Watcher` instance for the specified file. | ||
| * @throws Will throw an error if the path does not exist or cannot be resolved. | ||
| */ | ||
| async watchFile(path_) { | ||
| async watchFile(path_, options) { | ||
| try { | ||
@@ -205,12 +205,9 @@ const watcher = await this.watch(path_); | ||
| /** | ||
| * Registers a watcher for the specified folder path, with optional filtering rules | ||
| * to control which files or folders inside it should trigger change events. | ||
| * Registers a watcher for the specified directory path. | ||
| * | ||
| * @param path_ - The absolute or relative path to the folder to watch. | ||
| * @param path_ - The absolute or relative path to the directory to watch. | ||
| * The path will be resolved and normalized before being tracked. | ||
| * @param options - Filtering options to include or exclude specific paths. | ||
| * Filter rules may be exact strings or regular expressions, | ||
| * and are matched against the **absolute normalized path** of each affected file or folder. | ||
| * @returns A `Watcher` instance for the folder. | ||
| * @throws Will throw an error if the path is invalid or not a directory. | ||
| * @param options - Optional configuration options including filters to restrict which files and folders are watched. | ||
| * @returns A `Watcher` instance for the specified directory. | ||
| * @throws Will throw an error if the path does not exist or cannot be resolved. | ||
| */ | ||
@@ -217,0 +214,0 @@ async watchFolder(path_, options) { |
@@ -41,2 +41,20 @@ import utils from "../utils/utils.js"; | ||
| this.#_filters.exclude = this.#_filters.exclude.map(rule => this.#_helpers.normalizeFilterRule(rule)); | ||
| if (utils.hasOwnProperty(watchOptions, 'onChange')) { | ||
| this.onChange(watchOptions?.onChange); | ||
| } | ||
| if (utils.hasOwnProperty(watchOptions, 'onUpdate')) { | ||
| this.onUpdate(watchOptions?.onUpdate); | ||
| } | ||
| if (utils.hasOwnProperty(watchOptions, 'onRemove')) { | ||
| this.onRemove(watchOptions?.onRemove); | ||
| } | ||
| if (utils.hasOwnProperty(watchOptions, 'onRename')) { | ||
| this.onRename(watchOptions?.onRename); | ||
| } | ||
| if (utils.hasOwnProperty(watchOptions, 'onAdd')) { | ||
| this.onAdd(watchOptions?.onAdd); | ||
| } | ||
| if (utils.hasOwnProperty(watchOptions, 'onRootRemoved')) { | ||
| this.onRootRemoved(watchOptions?.onRootRemoved); | ||
| } | ||
| } | ||
@@ -43,0 +61,0 @@ /** |
+1
-1
| { | ||
| "name": "@nasriya/overwatch", | ||
| "version": "1.0.4", | ||
| "version": "1.0.5", | ||
| "description": "A high-performance, dependency-free file system watcher that monitors file and directory changes efficiently across platforms.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
+11
-0
@@ -115,2 +115,13 @@ [](https://nasriya.net) | ||
| You can also pass the handlers when you create the watcher: | ||
| ```ts | ||
| const projectWatcher = await overwatch.watchFolder(process.cwd(), { | ||
| include: ['**/*.ts'], // Accepts globs, regex, or absolute paths | ||
| exclude: [/node_modules/, '**/*.test.ts'], | ||
| onRename: (event) => { | ||
| console.log(`${event.type} renamed: ${event.oldPath} -> ${event.newPath}`); | ||
| } | ||
| }); | ||
| ``` | ||
| ### 3. Handling Root Deletion | ||
@@ -117,0 +128,0 @@ If the root directory being watched is deleted, a special event is triggered: |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Unidentified License
LicenseSomething that seems like a license was found, but its contents could not be matched with a known license.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Unidentified License
LicenseSomething that seems like a license was found, but its contents could not be matched with a known license.
Found 2 instances in 1 package
115845
1.27%2543
1.31%150
7.91%