What is gaze?
The 'gaze' npm package is a file watcher that uses native OS events to notify you of changes to files and directories. It is useful for tasks such as automatically reloading a server, rebuilding assets, or running tests when files change.
What are gaze's main functionalities?
Watch Files
This feature allows you to watch for changes in JavaScript files in the current directory. When a change is detected, it logs the event type and the file path.
const gaze = require('gaze');
gaze('*.js', function(err, watcher) {
if (err) throw err;
console.log('Watching files...');
this.on('all', function(event, filepath) {
console.log(event + ': ' + filepath);
});
});
Watch Directories
This feature allows you to watch for changes in JavaScript files within the 'lib' directory and its subdirectories. It logs the event type and the file path when a change is detected.
const gaze = require('gaze');
gaze('lib/**/*.js', function(err, watcher) {
if (err) throw err;
console.log('Watching directories...');
this.on('all', function(event, filepath) {
console.log(event + ': ' + filepath);
});
});
Get Watched Files
This feature allows you to retrieve a list of files currently being watched. It logs the list of watched files to the console.
const gaze = require('gaze');
gaze('*.js', function(err, watcher) {
if (err) throw err;
console.log('Watching files...');
console.log(this.watched());
});
Other packages similar to gaze
chokidar
Chokidar is a highly efficient and reliable file watcher that uses native OS events. It offers more features and better performance compared to 'gaze', including support for recursive watching, symlink following, and more robust handling of edge cases.
watch
The 'watch' package is a simple file watcher that uses polling to detect changes. It is less efficient than 'gaze' and 'chokidar' but can be useful for simpler use cases where native OS events are not required.
node-watch
Node-watch is another file watcher that uses native OS events. It is similar to 'gaze' but offers a simpler API and fewer features. It is suitable for basic file watching tasks.
gaze
A globbing fs.watch wrapper built from the best parts of other fine watch libs.
Compatible with NodeJS v0.8/0.6, Windows, OSX and Linux.
Usage
Install the module with: npm install gaze
or place into your package.json
and run npm install
.
var gaze = require('gaze');
gaze('**/*.js', function(err, watcher) {
console.log(this.watched());
this.on('changed', function(filepath) {
console.log(filepath + ' was changed');
});
this.on('added', function(filepath) {
console.log(filepath + ' was added');
});
this.on('deleted', function(filepath) {
console.log(filepath + ' was deleted');
});
this.on('all', function(event, filepath) {
console.log(filepath + ' was ' + event);
});
console.log(this.relative());
});
gaze(['stylesheets/*.css', 'images/**/*.png'], function() {
this.add(['js/*.js']);
});
Alternate Interface
var Gaze = require('gaze').Gaze;
var gaze = new Gaze('**/*');
gaze.on('ready', function(watcher) { });
gaze.on('all', function(event, filepath) { });
Errors
gaze('**/*', function() {
this.on('error', function(err) {
});
});
Minimatch / Glob
See isaacs's minimatch for more
information on glob patterns.
Documentation
gaze(patterns, [options], callback)
patterns
{String|Array} File patterns to be matchedoptions
{Object}callback
{Function}
err
{Error | null}watcher
{Object} Instance of the Gaze watcher
Class: gaze.Gaze
Create a Gaze object by instanting the gaze.Gaze
class.
var Gaze = require('gaze').Gaze;
var gaze = new Gaze(pattern, options, callback);
Properties
options
The options object passed in.
Events
ready(watcher)
When files have been globbed and watching has begun.all(event, filepath)
When an added
, changed
or deleted
event occurs.added(filepath)
When a file has been added to a watch directory.changed(filepath)
When a file or directory has been changed.deleted(filepath)
When a file or directory has been deleted.end()
When the watcher is closed and watches have been removed.error(err)
When an error occurs.
Methods
emit(event, [...])
Wrapper for the EventEmitter.emit.
added
|changed
|deleted
events will also trigger the all
event.close()
Unwatch all files and reset the watch instance.add(patterns, callback)
Adds file(s) patterns to be watched.remove(filepath)
removes a file or directory from being watched. Does not
recurse directories.watched()
Returns the currently watched files.relative()
Returns the currently watched files with relative paths.
Why Another fs.watch
Wrapper?
I liked parts of other fs.watch
wrappers but none had all the features I
needed. This lib combines the features I needed from other fine watch libs:
Speedy data behavior from
paulmillr's chokidar, API interface
from mikeal's watch and file globbing using
isaacs's glob which is also used by
cowboy's Grunt.
Contributing
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.
Release History
- 0.1.1 - Minor fixes
- 0.1.0 - Initial release
License
Copyright (c) 2012 Kyle Robinson Young
Licensed under the MIT license.