[79a0317] | 1 | # watchpack
|
---|
| 2 |
|
---|
| 3 | Wrapper library for directory and file watching.
|
---|
| 4 |
|
---|
| 5 | [![Test](https://github.com/webpack/watchpack/actions/workflows/test.yml/badge.svg)](https://github.com/webpack/watchpack/actions/workflows/test.yml)
|
---|
| 6 | [![Codecov](https://codecov.io/gh/webpack/watchpack/graph/badge.svg?token=8xk2OrrxWm)](https://codecov.io/gh/webpack/watchpack)
|
---|
| 7 | [![Downloads](https://img.shields.io/npm/dm/watchpack.svg)](https://www.npmjs.com/package/watchpack)
|
---|
| 8 |
|
---|
| 9 | ## Concept
|
---|
| 10 |
|
---|
| 11 | watchpack high level API doesn't map directly to watchers. Instead a three level architecture ensures that for each directory only a single watcher exists.
|
---|
| 12 |
|
---|
| 13 | - The high level API requests `DirectoryWatchers` from a `WatcherManager`, which ensures that only a single `DirectoryWatcher` per directory is created.
|
---|
| 14 | - A user-faced `Watcher` can be obtained from a `DirectoryWatcher` and provides a filtered view on the `DirectoryWatcher`.
|
---|
| 15 | - Reference-counting is used on the `DirectoryWatcher` and `Watcher` to decide when to close them.
|
---|
| 16 | - The real watchers are created by the `DirectoryWatcher`.
|
---|
| 17 | - Files are never watched directly. This should keep the watcher count low.
|
---|
| 18 | - Watching can be started in the past. This way watching can start after file reading.
|
---|
| 19 | - Symlinks are not followed, instead the symlink is watched.
|
---|
| 20 |
|
---|
| 21 | ## API
|
---|
| 22 |
|
---|
| 23 | ```javascript
|
---|
| 24 | var Watchpack = require("watchpack");
|
---|
| 25 |
|
---|
| 26 | var wp = new Watchpack({
|
---|
| 27 | // options:
|
---|
| 28 | aggregateTimeout: 1000,
|
---|
| 29 | // fire "aggregated" event when after a change for 1000ms no additional change occurred
|
---|
| 30 | // aggregated defaults to undefined, which doesn't fire an "aggregated" event
|
---|
| 31 |
|
---|
| 32 | poll: true,
|
---|
| 33 | // poll: true - use polling with the default interval
|
---|
| 34 | // poll: 10000 - use polling with an interval of 10s
|
---|
| 35 | // poll defaults to undefined, which prefer native watching methods
|
---|
| 36 | // Note: enable polling when watching on a network path
|
---|
| 37 | // When WATCHPACK_POLLING environment variable is set it will override this option
|
---|
| 38 |
|
---|
| 39 | followSymlinks: true,
|
---|
| 40 | // true: follows symlinks and watches symlinks and real files
|
---|
| 41 | // (This makes sense when symlinks has not been resolved yet, comes with a performance hit)
|
---|
| 42 | // false (default): watches only specified item they may be real files or symlinks
|
---|
| 43 | // (This makes sense when symlinks has already been resolved)
|
---|
| 44 |
|
---|
| 45 | ignored: "**/.git"
|
---|
| 46 | // ignored: "string" - a glob pattern for files or folders that should not be watched
|
---|
| 47 | // ignored: ["string", "string"] - multiple glob patterns that should be ignored
|
---|
| 48 | // ignored: /regexp/ - a regular expression for files or folders that should not be watched
|
---|
| 49 | // ignored: (entry) => boolean - an arbitrary function which must return truthy to ignore an entry
|
---|
| 50 | // For all cases expect the arbitrary function the path will have path separator normalized to '/'.
|
---|
| 51 | // All subdirectories are ignored too
|
---|
| 52 | });
|
---|
| 53 |
|
---|
| 54 | // Watchpack.prototype.watch({
|
---|
| 55 | // files: Iterable<string>,
|
---|
| 56 | // directories: Iterable<string>,
|
---|
| 57 | // missing: Iterable<string>,
|
---|
| 58 | // startTime?: number
|
---|
| 59 | // })
|
---|
| 60 | wp.watch({
|
---|
| 61 | files: listOfFiles,
|
---|
| 62 | directories: listOfDirectories,
|
---|
| 63 | missing: listOfNotExistingItems,
|
---|
| 64 | startTime: Date.now() - 10000
|
---|
| 65 | });
|
---|
| 66 | // starts watching these files and directories
|
---|
| 67 | // calling this again will override the files and directories
|
---|
| 68 | // files: can be files or directories, for files: content and existence changes are tracked
|
---|
| 69 | // for directories: only existence and timestamp changes are tracked
|
---|
| 70 | // directories: only directories, directory content (and content of children, ...) and
|
---|
| 71 | // existence changes are tracked.
|
---|
| 72 | // assumed to exist, when directory is not found without further information a remove event is emitted
|
---|
| 73 | // missing: can be files or directorees,
|
---|
| 74 | // only existence changes are tracked
|
---|
| 75 | // expected to not exist, no remove event is emitted when not found initially
|
---|
| 76 | // files and directories are assumed to exist, when they are not found without further information a remove event is emitted
|
---|
| 77 | // missing is assumed to not exist and no remove event is emitted
|
---|
| 78 |
|
---|
| 79 | wp.on("change", function(filePath, mtime, explanation) {
|
---|
| 80 | // filePath: the changed file
|
---|
| 81 | // mtime: last modified time for the changed file
|
---|
| 82 | // explanation: textual information how this change was detected
|
---|
| 83 | });
|
---|
| 84 |
|
---|
| 85 | wp.on("remove", function(filePath, explanation) {
|
---|
| 86 | // filePath: the removed file or directory
|
---|
| 87 | // explanation: textual information how this change was detected
|
---|
| 88 | });
|
---|
| 89 |
|
---|
| 90 | wp.on("aggregated", function(changes, removals) {
|
---|
| 91 | // changes: a Set of all changed files
|
---|
| 92 | // removals: a Set of all removed files
|
---|
| 93 | // watchpack gives up ownership on these Sets.
|
---|
| 94 | });
|
---|
| 95 |
|
---|
| 96 | // Watchpack.prototype.pause()
|
---|
| 97 | wp.pause();
|
---|
| 98 | // stops emitting events, but keeps watchers open
|
---|
| 99 | // next "watch" call can reuse the watchers
|
---|
| 100 | // The watcher will keep aggregating events
|
---|
| 101 | // which can be received with getAggregated()
|
---|
| 102 |
|
---|
| 103 | // Watchpack.prototype.close()
|
---|
| 104 | wp.close();
|
---|
| 105 | // stops emitting events and closes all watchers
|
---|
| 106 |
|
---|
| 107 | // Watchpack.prototype.getAggregated(): { changes: Set<string>, removals: Set<string> }
|
---|
| 108 | const { changes, removals } = wp.getAggregated();
|
---|
| 109 | // returns the current aggregated info and removes that from the watcher
|
---|
| 110 | // The next aggregated event won't include that info and will only emitted
|
---|
| 111 | // when futher changes happen
|
---|
| 112 | // Can also be used when paused.
|
---|
| 113 |
|
---|
| 114 | // Watchpack.prototype.collectTimeInfoEntries(fileInfoEntries: Map<string, Entry>, directoryInfoEntries: Map<string, Entry>)
|
---|
| 115 | wp.collectTimeInfoEntries(fileInfoEntries, directoryInfoEntries);
|
---|
| 116 | // collects time info objects for all known files and directories
|
---|
| 117 | // this include info from files not directly watched
|
---|
| 118 | // key: absolute path, value: object with { safeTime, timestamp }
|
---|
| 119 | // safeTime: a point in time at which it is safe to say all changes happened before that
|
---|
| 120 | // timestamp: only for files, the mtime timestamp of the file
|
---|
| 121 |
|
---|
| 122 | // Watchpack.prototype.getTimeInfoEntries()
|
---|
| 123 | var fileTimes = wp.getTimeInfoEntries();
|
---|
| 124 | // returns a Map with all known time info objects for files and directories
|
---|
| 125 | // similar to collectTimeInfoEntries but returns a single map with all entries
|
---|
| 126 |
|
---|
| 127 | // (deprecated)
|
---|
| 128 | // Watchpack.prototype.getTimes()
|
---|
| 129 | var fileTimes = wp.getTimes();
|
---|
| 130 | // returns an object with all known change times for files
|
---|
| 131 | // this include timestamps from files not directly watched
|
---|
| 132 | // key: absolute path, value: timestamp as number
|
---|
| 133 | ```
|
---|