UNPKG

6.34 kBTypeScriptView Raw
1// TypeScript Version: 3.0
2
3/// <reference types="node" />
4
5import * as fs from "fs";
6import { EventEmitter } from "events";
7import { Matcher } from 'anymatch';
8
9export class FSWatcher extends EventEmitter implements fs.FSWatcher {
10 options: WatchOptions;
11
12 /**
13 * Constructs a new FSWatcher instance with optional WatchOptions parameter.
14 */
15 constructor(options?: WatchOptions);
16
17 /**
18 * Add files, directories, or glob patterns for tracking. Takes an array of strings or just one
19 * string.
20 */
21 add(paths: string | ReadonlyArray<string>): this;
22
23 /**
24 * Stop watching files, directories, or glob patterns. Takes an array of strings or just one
25 * string.
26 */
27 unwatch(paths: string | ReadonlyArray<string>): this;
28
29 /**
30 * Returns an object representing all the paths on the file system being watched by this
31 * `FSWatcher` instance. The object's keys are all the directories (using absolute paths unless
32 * the `cwd` option was used), and the values are arrays of the names of the items contained in
33 * each directory.
34 */
35 getWatched(): {
36 [directory: string]: string[];
37 };
38
39 /**
40 * Removes all listeners from watched files.
41 */
42 close(): Promise<void>;
43
44 on(event: 'add'|'addDir'|'change', listener: (path: string, stats?: fs.Stats) => void): this;
45
46 on(event: 'all', listener: (eventName: 'add'|'addDir'|'change'|'unlink'|'unlinkDir', path: string, stats?: fs.Stats) => void): this;
47
48 /**
49 * Error occurred
50 */
51 on(event: 'error', listener: (error: Error) => void): this;
52
53 /**
54 * Exposes the native Node `fs.FSWatcher events`
55 */
56 on(event: 'raw', listener: (eventName: string, path: string, details: any) => void): this;
57
58 /**
59 * Fires when the initial scan is complete
60 */
61 on(event: 'ready', listener: () => void): this;
62
63 on(event: 'unlink'|'unlinkDir', listener: (path: string) => void): this;
64
65 on(event: string, listener: (...args: any[]) => void): this;
66
67 ref(): this;
68
69 unref(): this;
70}
71
72export interface WatchOptions {
73 /**
74 * Indicates whether the process should continue to run as long as files are being watched. If
75 * set to `false` when using `fsevents` to watch, no more events will be emitted after `ready`,
76 * even if the process continues to run.
77 */
78 persistent?: boolean;
79
80 /**
81 * ([anymatch](https://github.com/micromatch/anymatch)-compatible definition) Defines files/paths to
82 * be ignored. The whole relative or absolute path is tested, not just filename. If a function
83 * with two arguments is provided, it gets called twice per path - once with a single argument
84 * (the path), second time with two arguments (the path and the
85 * [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object of that path).
86 */
87 ignored?: Matcher;
88
89 /**
90 * If set to `false` then `add`/`addDir` events are also emitted for matching paths while
91 * instantiating the watching as chokidar discovers these file paths (before the `ready` event).
92 */
93 ignoreInitial?: boolean;
94
95 /**
96 * When `false`, only the symlinks themselves will be watched for changes instead of following
97 * the link references and bubbling events through the link's path.
98 */
99 followSymlinks?: boolean;
100
101 /**
102 * The base directory from which watch `paths` are to be derived. Paths emitted with events will
103 * be relative to this.
104 */
105 cwd?: string;
106
107 /**
108 * If set to true then the strings passed to .watch() and .add() are treated as literal path
109 * names, even if they look like globs. Default: false.
110 */
111 disableGlobbing?: boolean;
112
113 /**
114 * Whether to use fs.watchFile (backed by polling), or fs.watch. If polling leads to high CPU
115 * utilization, consider setting this to `false`. It is typically necessary to **set this to
116 * `true` to successfully watch files over a network**, and it may be necessary to successfully
117 * watch files in other non-standard situations. Setting to `true` explicitly on OS X overrides
118 * the `useFsEvents` default.
119 */
120 usePolling?: boolean;
121
122 /**
123 * Whether to use the `fsevents` watching interface if available. When set to `true` explicitly
124 * and `fsevents` is available this supercedes the `usePolling` setting. When set to `false` on
125 * OS X, `usePolling: true` becomes the default.
126 */
127 useFsEvents?: boolean;
128
129 /**
130 * If relying upon the [`fs.Stats`](https://nodejs.org/api/fs.html#fs_class_fs_stats) object that
131 * may get passed with `add`, `addDir`, and `change` events, set this to `true` to ensure it is
132 * provided even in cases where it wasn't already available from the underlying watch events.
133 */
134 alwaysStat?: boolean;
135
136 /**
137 * If set, limits how many levels of subdirectories will be traversed.
138 */
139 depth?: number;
140
141 /**
142 * Interval of file system polling.
143 */
144 interval?: number;
145
146 /**
147 * Interval of file system polling for binary files. ([see list of binary extensions](https://gi
148 * thub.com/sindresorhus/binary-extensions/blob/master/binary-extensions.json))
149 */
150 binaryInterval?: number;
151
152 /**
153 * Indicates whether to watch files that don't have read permissions if possible. If watching
154 * fails due to `EPERM` or `EACCES` with this set to `true`, the errors will be suppressed
155 * silently.
156 */
157 ignorePermissionErrors?: boolean;
158
159 /**
160 * `true` if `useFsEvents` and `usePolling` are `false`). Automatically filters out artifacts
161 * that occur when using editors that use "atomic writes" instead of writing directly to the
162 * source file. If a file is re-added within 100 ms of being deleted, Chokidar emits a `change`
163 * event rather than `unlink` then `add`. If the default of 100 ms does not work well for you,
164 * you can override it by setting `atomic` to a custom value, in milliseconds.
165 */
166 atomic?: boolean | number;
167
168 /**
169 * can be set to an object in order to adjust timing params:
170 */
171 awaitWriteFinish?: AwaitWriteFinishOptions | boolean;
172}
173
174export interface AwaitWriteFinishOptions {
175 /**
176 * Amount of time in milliseconds for a file size to remain constant before emitting its event.
177 */
178 stabilityThreshold?: number;
179
180 /**
181 * File size polling interval.
182 */
183 pollInterval?: number;
184}
185
186/**
187 * produces an instance of `FSWatcher`.
188 */
189export function watch(
190 paths: string | ReadonlyArray<string>,
191 options?: WatchOptions
192): FSWatcher;