index.d.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*! chokidar - MIT License (c) 2012 Paul Miller (paulmillr.com) */
  2. import { EventEmitter } from 'node:events';
  3. import { Stats } from 'node:fs';
  4. import { type EntryInfo, type ReaddirpOptions, ReaddirpStream } from 'readdirp';
  5. import { EVENTS as EV, type EventName, NodeFsHandler, type Path, type WatchHandlers } from './handler.js';
  6. export type AWF = {
  7. stabilityThreshold: number;
  8. pollInterval: number;
  9. };
  10. type BasicOpts = {
  11. persistent: boolean;
  12. ignoreInitial: boolean;
  13. followSymlinks: boolean;
  14. cwd?: string;
  15. usePolling: boolean;
  16. interval: number;
  17. binaryInterval: number;
  18. alwaysStat?: boolean;
  19. depth?: number;
  20. ignorePermissionErrors: boolean;
  21. atomic: boolean | number;
  22. };
  23. export type Throttler = {
  24. timeoutObject: NodeJS.Timeout;
  25. clear: () => void;
  26. count: number;
  27. };
  28. export type ChokidarOptions = Partial<BasicOpts & {
  29. ignored: Matcher | Matcher[];
  30. awaitWriteFinish: boolean | Partial<AWF>;
  31. }>;
  32. export type FSWInstanceOptions = BasicOpts & {
  33. ignored: Matcher[];
  34. awaitWriteFinish: false | AWF;
  35. };
  36. export type ThrottleType = 'readdir' | 'watch' | 'add' | 'remove' | 'change';
  37. export type EmitArgs = [path: Path, stats?: Stats];
  38. export type EmitErrorArgs = [error: Error, stats?: Stats];
  39. export type EmitArgsWithName = [event: EventName, ...EmitArgs];
  40. export type MatchFunction = (val: string, stats?: Stats) => boolean;
  41. export interface MatcherObject {
  42. path: string;
  43. recursive?: boolean;
  44. }
  45. export type Matcher = string | RegExp | MatchFunction | MatcherObject;
  46. /**
  47. * Directory entry.
  48. */
  49. declare class DirEntry {
  50. path: Path;
  51. _removeWatcher: (dir: string, base: string) => void;
  52. items: Set<Path>;
  53. constructor(dir: Path, removeWatcher: (dir: string, base: string) => void);
  54. add(item: string): void;
  55. remove(item: string): Promise<void>;
  56. has(item: string): boolean | undefined;
  57. getChildren(): string[];
  58. dispose(): void;
  59. }
  60. export declare class WatchHelper {
  61. fsw: FSWatcher;
  62. path: string;
  63. watchPath: string;
  64. fullWatchPath: string;
  65. dirParts: string[][];
  66. followSymlinks: boolean;
  67. statMethod: 'stat' | 'lstat';
  68. constructor(path: string, follow: boolean, fsw: FSWatcher);
  69. entryPath(entry: EntryInfo): Path;
  70. filterPath(entry: EntryInfo): boolean;
  71. filterDir(entry: EntryInfo): boolean;
  72. }
  73. export interface FSWatcherEventMap {
  74. [EV.READY]: [];
  75. [EV.RAW]: Parameters<WatchHandlers['rawEmitter']>;
  76. [EV.ERROR]: Parameters<WatchHandlers['errHandler']>;
  77. [EV.ALL]: [event: EventName, ...EmitArgs];
  78. [EV.ADD]: EmitArgs;
  79. [EV.CHANGE]: EmitArgs;
  80. [EV.ADD_DIR]: EmitArgs;
  81. [EV.UNLINK]: EmitArgs;
  82. [EV.UNLINK_DIR]: EmitArgs;
  83. }
  84. /**
  85. * Watches files & directories for changes. Emitted events:
  86. * `add`, `addDir`, `change`, `unlink`, `unlinkDir`, `all`, `error`
  87. *
  88. * new FSWatcher()
  89. * .add(directories)
  90. * .on('add', path => log('File', path, 'was added'))
  91. */
  92. export declare class FSWatcher extends EventEmitter<FSWatcherEventMap> {
  93. closed: boolean;
  94. options: FSWInstanceOptions;
  95. _closers: Map<string, Array<any>>;
  96. _ignoredPaths: Set<Matcher>;
  97. _throttled: Map<ThrottleType, Map<any, any>>;
  98. _streams: Set<ReaddirpStream>;
  99. _symlinkPaths: Map<Path, string | boolean>;
  100. _watched: Map<string, DirEntry>;
  101. _pendingWrites: Map<string, any>;
  102. _pendingUnlinks: Map<string, EmitArgsWithName>;
  103. _readyCount: number;
  104. _emitReady: () => void;
  105. _closePromise?: Promise<void>;
  106. _userIgnored?: MatchFunction;
  107. _readyEmitted: boolean;
  108. _emitRaw: WatchHandlers['rawEmitter'];
  109. _boundRemove: (dir: string, item: string) => void;
  110. _nodeFsHandler: NodeFsHandler;
  111. constructor(_opts?: ChokidarOptions);
  112. _addIgnoredPath(matcher: Matcher): void;
  113. _removeIgnoredPath(matcher: Matcher): void;
  114. /**
  115. * Adds paths to be watched on an existing FSWatcher instance.
  116. * @param paths_ file or file list. Other arguments are unused
  117. */
  118. add(paths_: Path | Path[], _origAdd?: string, _internal?: boolean): FSWatcher;
  119. /**
  120. * Close watchers or start ignoring events from specified paths.
  121. */
  122. unwatch(paths_: Path | Path[]): FSWatcher;
  123. /**
  124. * Close watchers and remove all listeners from watched paths.
  125. */
  126. close(): Promise<void>;
  127. /**
  128. * Expose list of watched paths
  129. * @returns for chaining
  130. */
  131. getWatched(): Record<string, string[]>;
  132. emitWithAll(event: EventName, args: EmitArgs): void;
  133. /**
  134. * Normalize and emit events.
  135. * Calling _emit DOES NOT MEAN emit() would be called!
  136. * @param event Type of event
  137. * @param path File or directory path
  138. * @param stats arguments to be passed with event
  139. * @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  140. */
  141. _emit(event: EventName, path: Path, stats?: Stats): Promise<this | undefined>;
  142. /**
  143. * Common handler for errors
  144. * @returns The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  145. */
  146. _handleError(error: Error): Error | boolean;
  147. /**
  148. * Helper utility for throttling
  149. * @param actionType type being throttled
  150. * @param path being acted upon
  151. * @param timeout duration of time to suppress duplicate actions
  152. * @returns tracking object or false if action should be suppressed
  153. */
  154. _throttle(actionType: ThrottleType, path: Path, timeout: number): Throttler | false;
  155. _incrReadyCount(): number;
  156. /**
  157. * Awaits write operation to finish.
  158. * Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
  159. * @param path being acted upon
  160. * @param threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
  161. * @param event
  162. * @param awfEmit Callback to be called when ready for event to be emitted.
  163. */
  164. _awaitWriteFinish(path: Path, threshold: number, event: EventName, awfEmit: (err?: Error, stat?: Stats) => void): void;
  165. /**
  166. * Determines whether user has asked to ignore this path.
  167. */
  168. _isIgnored(path: Path, stats?: Stats): boolean;
  169. _isntIgnored(path: Path, stat?: Stats): boolean;
  170. /**
  171. * Provides a set of common helpers and properties relating to symlink handling.
  172. * @param path file or directory pattern being watched
  173. */
  174. _getWatchHelpers(path: Path): WatchHelper;
  175. /**
  176. * Provides directory tracking objects
  177. * @param directory path of the directory
  178. */
  179. _getWatchedDir(directory: string): DirEntry;
  180. /**
  181. * Check for read permissions: https://stackoverflow.com/a/11781404/1358405
  182. */
  183. _hasReadPermissions(stats: Stats): boolean;
  184. /**
  185. * Handles emitting unlink events for
  186. * files and directories, and via recursion, for
  187. * files and directories within directories that are unlinked
  188. * @param directory within which the following item is located
  189. * @param item base path of item/directory
  190. */
  191. _remove(directory: string, item: string, isDirectory?: boolean): void;
  192. /**
  193. * Closes all watchers for a path
  194. */
  195. _closePath(path: Path): void;
  196. /**
  197. * Closes only file-specific watchers
  198. */
  199. _closeFile(path: Path): void;
  200. _addPathCloser(path: Path, closer: () => void): void;
  201. _readdirp(root: Path, opts?: Partial<ReaddirpOptions>): ReaddirpStream | undefined;
  202. }
  203. /**
  204. * Instantiates watcher with paths to be tracked.
  205. * @param paths file / directory paths
  206. * @param options opts, such as `atomic`, `awaitWriteFinish`, `ignored`, and others
  207. * @returns an instance of FSWatcher for chaining.
  208. * @example
  209. * const watcher = watch('.').on('all', (event, path) => { console.log(event, path); });
  210. * watch('.', { atomic: true, awaitWriteFinish: true, ignored: (f, stats) => stats?.isFile() && !f.endsWith('.js') })
  211. */
  212. export declare function watch(paths: string | string[], options?: ChokidarOptions): FSWatcher;
  213. declare const _default: {
  214. watch: typeof watch;
  215. FSWatcher: typeof FSWatcher;
  216. };
  217. export default _default;