index.d.ts 6.2 KB

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