index.d.ts 7.3 KB

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