:crystal_ball: A globbing fs.watch wrapper built from the best parts of other fine watch libs.
A globbing
fs.watchwrapper built from the best parts of other fine watch libs.
Install the module with:
npm install gazeor place into your
package.jsonand run
npm install.
const gaze = require('gaze');// Watch all .js files/dirs in process.cwd() gaze('*/.js', (err, watcher) => { // Files have all started watching
// Get all watched files const watched = watcher.watched();
// On file changed watcher.on('changed', filepath => { console.log(filepath + ' was changed'); });
// On file added watcher.on('added', filepath => { console.log(filepath + ' was added'); });
// On file deleted watcher.on('deleted', filepath => { console.log(filepath + ' was deleted'); });
// On changed/added/deleted watcher.on('all', (event, filepath) => { console.log(filepath + ' was ' + event); });
// Get watched files with relative paths const files = watcher.relative(); });
// Also accepts an array of patterns gaze(['stylesheets/.css', 'images/**/.png'], () => { // Add more patterns later to be watched watcher.add(['js/*.js']); });
const {Gaze} = require('gaze');const gaze = new Gaze('*/');
// Files have all started watching gaze.on('ready', watcher => { });
// A file has been added/changed/deleted has occurred gaze.on('all', (event, filepath) => { });
gaze('**/*', (error, watcher) => { if (error) { // Handle error if it occurred while starting up } });// Or with the alternative interface const gaze = new Gaze(); gaze.on('error', error => { // Handle error here }); gaze.add('*/');
See isaacs's
minimatchfor more information on glob patterns.
patterns{
String|
Array} File patterns to be matched
options{
Object}
callback{
Function}
err{
Error|
null}
watcher{
Object} Instance of the
Gazewatcher
gaze.Gaze
Create a
Gazeobject by instancing the
gaze.Gazeclass.
const Gaze = require('gaze').Gaze; const gaze = new Gaze(pattern, options, callback);
optionsThe options object passed in.
interval{integer} Interval to pass to
fs.watchFile
debounceDelay{integer} Delay for events called in succession for the same file/event in milliseconds
mode{string} Force the watch mode. Either
'auto'(default),
'watch'(force native events), or
'poll'(force stat polling).
cwd{string} The current working directory to base file patterns from. Default is
process.cwd().
ready(watcher)When files have been globbed and watching has begun.
all(event, filepath)When an
added,
changed,
renamed, or
deletedevent occurs.
added(filepath)When a file has been added to a watch directory.
changed(filepath)When a file has been changed.
deleted(filepath)When a file has been deleted.
renamed(newPath, oldPath)When a file has been renamed.
end()When the watcher is closed and watches have been removed.
error(err)When an error occurs.
nomatchWhen no files have been matched.
emit(event, [...])Wrapper for
EventEmitter.emit.
added|
changed|
renamed|
deletedevents will also trigger the
allevent.
close()Unwatch all files and reset the watch instance.
add(patterns, callback)Adds file(s)
patternsto be watched.
remove(filepath)Removes a file or directory from being watched. Does not recurse directories.
watched()Returns the currently watched files.
relative([dir, unixify])Returns the currently watched files with relative paths.
dir{string} Only return relative files for this directory.
unixify{boolean} Return paths with
/instead of
\\if on Windows.
Other great watch libraries to try are:
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
ENOENTerrors from escaping (@alexgorbatchev).
fs.watcherrors from escaping error handler (@rosen-vladimirov). Fix
_addToWatchedwithout
path.sep(@wyicwx).
[email protected]with
minimatch >= 3.0.0.
maxListeners. Update
globuleto
0.2.0.
errorfrom
readdir(@oconnore). Fix for
0 maxListeners. Use
graceful-fsto avoid
EMFILEerrors in other places
fsis used. Better method to determine if
pathwatcherwas built. Fix keeping process alive too much, only init
pathwatcherif a file is being watched. Set min required to Windows Vista when building on Windows (@pvolok).
watched(). Fix for erroneous
addedevents on folders. Ignore
msvsbuild error 4244.
pathwatcher) but can fall back to stat polling. Everything is async to avoid blocking, including
relative()and
watched(). Better error handling. Update to
[email protected]. No longer watches
cwdby default. Added
modeoption. Better
EMFILEmessage. Avoids
ENOENTerrors with symlinks. All constructor arguments are optional.
ENOENTerror with non-existent symlinks [BACKPORTED].
setImmediate(
process.nextTickfor Node.js v0.8) to defer
ready/
nomatchevents (@amasad).
nomatchevent when no files are matching.
.remove()method to remove a single file in a directory (@kaelzhang). Fixing “
Cannot call method 'call' of undefined” (@krasimir). Track new file additions within folders (@brett-shwom).
watchDirnot respecting close in race condition (@chrisirhc).
globulefor file matching. Avoid Node.js v0.10
path.resolve/
joinerrors. Register new files when added to non-existent folder. Multiple instances can now poll the same files (@jpommerening).
path must be strings” errors (@groner). Fix incorrect
addedevents (@groner).
endbefore
removeAllListeners.
addedevents within subfolder patterns.
forceWatchMethodoption removed, bug fixes and watch optimizations (@rgaskill).
addcalls dont get watched (@samcday).
removeAllListenerson
close.
addedevents in current working dir.
path.sep. Add
forceWatchMethodoption. Support
renamedevents.
cwdoption properly
too many open file” errors
Copyright (c) 2018 Kyle Robinson Young
Licensed under the MIT license.