UNPKG

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