Wrapper library for directory and file watching.
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.
DirectoryWatchers
from a WatcherManager
, which ensures that only a single DirectoryWatcher
per directory is created.Watcher
can be obtained from a DirectoryWatcher
and provides a filtered view on the DirectoryWatcher
.DirectoryWatcher
and Watcher
to decide when to close them.DirectoryWatcher
.```javascript var Watchpack = require("watchpack");
var wp = new Watchpack({ // options: aggregateTimeout: 1000, // fire "aggregated" event when after a change for 1000ms no additional change occurred // aggregated defaults to undefined, which doesn't fire an "aggregated" event
poll: true,
// poll: true - use polling with the default interval
// poll: 10000 - use polling with an interval of 10s
// poll defaults to undefined, which prefer native watching methods
// Note: enable polling when watching on a network path
// When WATCHPACK_POLLING environment variable is set it will override this option
followSymlinks: true,
// true: follows symlinks and watches symlinks and real files
// (This makes sense when symlinks has not been resolved yet, comes with a performance hit)
// false (default): watches only specified item they may be real files or symlinks
// (This makes sense when symlinks has already been resolved)
ignored: "**/.git"
// ignored: "string" - a glob pattern for files or folders that should not be watched
// ignored: ["string", "string"] - multiple glob patterns that should be ignored
// ignored: /regexp/ - a regular expression for files or folders that should not be watched
// ignored: (entry) => boolean - an arbitrary function which must return truthy to ignore an entry
// For all cases expect the arbitrary function the path will have path separator normalized to '/'.
// All subdirectories are ignored too
});
// Watchpack.prototype.watch({
// files: Iterable
wp.on("change", function(filePath, mtime, explanation) { // filePath: the changed file // mtime: last modified time for the changed file // explanation: textual information how this change was detected });
wp.on("remove", function(filePath, explanation) { // filePath: the removed file or directory // explanation: textual information how this change was detected });
wp.on("aggregated", function(changes, removals) { // changes: a Set of all changed files // removals: a Set of all removed files // watchpack gives up ownership on these Sets. });
// Watchpack.prototype.pause() wp.pause(); // stops emitting events, but keeps watchers open // next "watch" call can reuse the watchers // The watcher will keep aggregating events // which can be received with getAggregated()
// Watchpack.prototype.close() wp.close(); // stops emitting events and closes all watchers
// Watchpack.prototype.getAggregated(): { changes: Set
// Watchpack.prototype.collectTimeInfoEntries(fileInfoEntries: Map
// Watchpack.prototype.getTimeInfoEntries() var fileTimes = wp.getTimeInfoEntries(); // returns a Map with all known time info objects for files and directories // similar to collectTimeInfoEntries but returns a single map with all entries
// (deprecated) // Watchpack.prototype.getTimes() var fileTimes = wp.getTimes(); // returns an object with all known change times for files // this include timestamps from files not directly watched // key: absolute path, value: timestamp as number ```