UNPKG

7.71 kBTypeScriptView Raw
1// Inlined to avoid extra dependency (chokidar is bundled in the published build)
2
3// https://github.com/paulmillr/chokidar/blob/master/types/index.d.ts
4// MIT Licensed https://github.com/paulmillr/chokidar/blob/master/LICENSE
5
6/**
7The MIT License (MIT)
8
9Copyright (c) 2012-2019 Paul Miller (https://paulmillr.com), Elan Shanker
10
11Permission is hereby granted, free of charge, to any person obtaining a copy
12of this software and associated documentation files (the “Software”), to deal
13in the Software without restriction, including without limitation the rights
14to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15copies of the Software, and to permit persons to whom the Software is
16furnished to do so, subject to the following conditions:
17
18The above copyright notice and this permission notice shall be included in
19all copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27THE SOFTWARE.
28*/
29/// <reference types="node" />
30
31import type * as fs from 'node:fs'
32import { EventEmitter } from 'node:events'
33import type { Matcher } from './anymatch'
34
35export class FSWatcher extends EventEmitter implements fs.FSWatcher {
36 options: WatchOptions
37
38 /**
39 * Constructs a new FSWatcher instance with optional WatchOptions parameter.
40 */
41 constructor(options?: WatchOptions)
42
43 /**
44 * Add files, directories, or glob patterns for tracking. Takes an array of strings or just one
45 * string.
46 */
47 add(paths: string | ReadonlyArray<string>): this
48
49 /**
50 * Stop watching files, directories, or glob patterns. Takes an array of strings or just one
51 * string.
52 */
53 unwatch(paths: string | ReadonlyArray<string>): this
54
55 /**
56 * Returns an object representing all the paths on the file system being watched by this
57 * `FSWatcher` instance. The object's keys are all the directories (using absolute paths unless
58 * the `cwd` option was used), and the values are arrays of the names of the items contained in
59 * each directory.
60 */
61 getWatched(): {
62 [directory: string]: string[]
63 }
64
65 /**
66 * Removes all listeners from watched files.
67 */
68 close(): Promise<void>
69
70 on(
71 event: 'add' | 'addDir' | 'change',
72 listener: (path: string, stats?: fs.Stats) => void
73 ): this
74
75 on(
76 event: 'all',
77 listener: (
78 eventName: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir',
79 path: string,
80 stats?: fs.Stats
81 ) => void
82 ): this
83
84 /**
85 * Error occurred
86 */
87 on(event: 'error', listener: (error: Error) => void): this
88
89 /**
90 * Exposes the native Node `fs.FSWatcher events`
91 */
92 on(
93 event: 'raw',
94 listener: (eventName: string, path: string, details: any) => void
95 ): this
96
97 /**
98 * Fires when the initial scan is complete
99 */
100 on(event: 'ready', listener: () => void): this
101
102 on(event: 'unlink' | 'unlinkDir', listener: (path: string) => void): this
103
104 on(event: string, listener: (...args: any[]) => void): this
105}
106
107export interface WatchOptions {
108 /**
109 * Indicates whether the process should continue to run as long as files are being watched. If
110 * set to `false` when using `fsevents` to watch, no more events will be emitted after `ready`,
111 * even if the process continues to run.
112 */
113 persistent?: boolean
114
115 /**
116 * ([anymatch](https://github.com/micromatch/anymatch)-compatible definition) Defines files/paths to
117 * be ignored. The whole relative or absolute path is tested, not just filename. If a function
118 * with two arguments is provided, it gets called twice per path - once with a single argument
119 * (the path), second time with two arguments (the path and the
120 * [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object of that path).
121 */
122 ignored?: Matcher
123
124 /**
125 * If set to `false` then `add`/`addDir` events are also emitted for matching paths while
126 * instantiating the watching as chokidar discovers these file paths (before the `ready` event).
127 */
128 ignoreInitial?: boolean
129
130 /**
131 * When `false`, only the symlinks themselves will be watched for changes instead of following
132 * the link references and bubbling events through the link's path.
133 */
134 followSymlinks?: boolean
135
136 /**
137 * The base directory from which watch `paths` are to be derived. Paths emitted with events will
138 * be relative to this.
139 */
140 cwd?: string
141
142 /**
143 * If set to true then the strings passed to .watch() and .add() are treated as literal path
144 * names, even if they look like globs.
145 *
146 * @default false
147 */
148 disableGlobbing?: boolean
149
150 /**
151 * Whether to use fs.watchFile (backed by polling), or fs.watch. If polling leads to high CPU
152 * utilization, consider setting this to `false`. It is typically necessary to **set this to
153 * `true` to successfully watch files over a network**, and it may be necessary to successfully
154 * watch files in other non-standard situations. Setting to `true` explicitly on OS X overrides
155 * the `useFsEvents` default.
156 */
157 usePolling?: boolean
158
159 /**
160 * Whether to use the `fsevents` watching interface if available. When set to `true` explicitly
161 * and `fsevents` is available this supercedes the `usePolling` setting. When set to `false` on
162 * OS X, `usePolling: true` becomes the default.
163 */
164 useFsEvents?: boolean
165
166 /**
167 * If relying upon the [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object that
168 * may get passed with `add`, `addDir`, and `change` events, set this to `true` to ensure it is
169 * provided even in cases where it wasn't already available from the underlying watch events.
170 */
171 alwaysStat?: boolean
172
173 /**
174 * If set, limits how many levels of subdirectories will be traversed.
175 */
176 depth?: number
177
178 /**
179 * Interval of file system polling.
180 */
181 interval?: number
182
183 /**
184 * Interval of file system polling for binary files. ([see list of binary extensions](https://gi
185 * thub.com/sindresorhus/binary-extensions/blob/master/binary-extensions.json))
186 */
187 binaryInterval?: number
188
189 /**
190 * Indicates whether to watch files that don't have read permissions if possible. If watching
191 * fails due to `EPERM` or `EACCES` with this set to `true`, the errors will be suppressed
192 * silently.
193 */
194 ignorePermissionErrors?: boolean
195
196 /**
197 * `true` if `useFsEvents` and `usePolling` are `false`. Automatically filters out artifacts
198 * that occur when using editors that use "atomic writes" instead of writing directly to the
199 * source file. If a file is re-added within 100 ms of being deleted, Chokidar emits a `change`
200 * event rather than `unlink` then `add`. If the default of 100 ms does not work well for you,
201 * you can override it by setting `atomic` to a custom value, in milliseconds.
202 */
203 atomic?: boolean | number
204
205 /**
206 * can be set to an object in order to adjust timing params:
207 */
208 awaitWriteFinish?: AwaitWriteFinishOptions | boolean
209}
210
211export interface AwaitWriteFinishOptions {
212 /**
213 * Amount of time in milliseconds for a file size to remain constant before emitting its event.
214 */
215 stabilityThreshold?: number
216
217 /**
218 * File size polling interval.
219 */
220 pollInterval?: number
221}
222
223/**
224 * produces an instance of `FSWatcher`.
225 */
226export function watch(
227 paths: string | ReadonlyArray<string>,
228 options?: WatchOptions
229): FSWatcher
230
\No newline at end of file