UNPKG

7.69 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 'fs'
32import { EventEmitter } from '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. Default: false.
145 */
146 disableGlobbing?: boolean
147
148 /**
149 * Whether to use fs.watchFile (backed by polling), or fs.watch. If polling leads to high CPU
150 * utilization, consider setting this to `false`. It is typically necessary to **set this to
151 * `true` to successfully watch files over a network**, and it may be necessary to successfully
152 * watch files in other non-standard situations. Setting to `true` explicitly on OS X overrides
153 * the `useFsEvents` default.
154 */
155 usePolling?: boolean
156
157 /**
158 * Whether to use the `fsevents` watching interface if available. When set to `true` explicitly
159 * and `fsevents` is available this supercedes the `usePolling` setting. When set to `false` on
160 * OS X, `usePolling: true` becomes the default.
161 */
162 useFsEvents?: boolean
163
164 /**
165 * If relying upon the [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object that
166 * may get passed with `add`, `addDir`, and `change` events, set this to `true` to ensure it is
167 * provided even in cases where it wasn't already available from the underlying watch events.
168 */
169 alwaysStat?: boolean
170
171 /**
172 * If set, limits how many levels of subdirectories will be traversed.
173 */
174 depth?: number
175
176 /**
177 * Interval of file system polling.
178 */
179 interval?: number
180
181 /**
182 * Interval of file system polling for binary files. ([see list of binary extensions](https://gi
183 * thub.com/sindresorhus/binary-extensions/blob/master/binary-extensions.json))
184 */
185 binaryInterval?: number
186
187 /**
188 * Indicates whether to watch files that don't have read permissions if possible. If watching
189 * fails due to `EPERM` or `EACCES` with this set to `true`, the errors will be suppressed
190 * silently.
191 */
192 ignorePermissionErrors?: boolean
193
194 /**
195 * `true` if `useFsEvents` and `usePolling` are `false`). Automatically filters out artifacts
196 * that occur when using editors that use "atomic writes" instead of writing directly to the
197 * source file. If a file is re-added within 100 ms of being deleted, Chokidar emits a `change`
198 * event rather than `unlink` then `add`. If the default of 100 ms does not work well for you,
199 * you can override it by setting `atomic` to a custom value, in milliseconds.
200 */
201 atomic?: boolean | number
202
203 /**
204 * can be set to an object in order to adjust timing params:
205 */
206 awaitWriteFinish?: AwaitWriteFinishOptions | boolean
207}
208
209export interface AwaitWriteFinishOptions {
210 /**
211 * Amount of time in milliseconds for a file size to remain constant before emitting its event.
212 */
213 stabilityThreshold?: number
214
215 /**
216 * File size polling interval.
217 */
218 pollInterval?: number
219}
220
221/**
222 * produces an instance of `FSWatcher`.
223 */
224export function watch(
225 paths: string | ReadonlyArray<string>,
226 options?: WatchOptions
227): FSWatcher
228
\No newline at end of file