Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
A globbing fs.watch wrapper built from the best parts of other fine watch libs.
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.
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());
});
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.
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 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.
A globbing fs.watch
wrapper built from the best parts of other fine watch libs.
Compatible with Node.js >= 4.x, Windows, macOS, and Linux.
Install the module with: npm install gaze
or place into your package.json
and run npm install
.
var gaze = require('gaze');
// Watch all .js files/dirs in process.cwd()
gaze('**/*.js', function(err, watcher) {
// Files have all started watching
// watcher === this
// Get all watched files
var watched = this.watched();
// On file changed
this.on('changed', function(filepath) {
console.log(filepath + ' was changed');
});
// On file added
this.on('added', function(filepath) {
console.log(filepath + ' was added');
});
// On file deleted
this.on('deleted', function(filepath) {
console.log(filepath + ' was deleted');
});
// On changed/added/deleted
this.on('all', function(event, filepath) {
console.log(filepath + ' was ' + event);
});
// Get watched files with relative paths
var files = this.relative();
});
// Also accepts an array of patterns
gaze(['stylesheets/*.css', 'images/**/*.png'], function() {
// Add more patterns later to be watched
this.add(['js/*.js']);
});
var Gaze = require('gaze').Gaze;
var gaze = new Gaze('**/*');
// Files have all started watching
gaze.on('ready', function(watcher) { });
// A file has been added/changed/deleted has occurred
gaze.on('all', function(event, filepath) { });
gaze('**/*', function(error, watcher) {
if (error) {
// Handle error if it occurred while starting up
}
});
// Or with the alternative interface
var gaze = new Gaze();
gaze.on('error', function(error) {
// Handle error here
});
gaze.add('**/*');
See isaacs's minimatch
for more
information on glob patterns.
patterns
{String
|Array
} File patterns to be matchedoptions
{Object
}callback
{Function
}
err
{Error
| null
}watcher
{Object
} Instance of the Gaze
watchergaze.Gaze
Create a Gaze
object by instancing the gaze.Gaze
class.
var Gaze = require('gaze').Gaze;
var gaze = new Gaze(pattern, options, callback);
options
The 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 millisecondsmode
{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 deleted
event 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.nomatch
When no files have been matched.emit(event, [...])
Wrapper for EventEmitter.emit
.
added
|changed
|renamed
|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([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.
ENOENT
errors from escaping (@alexgorbatchev).fs.watch
errors from escaping error handler (@rosen-vladimirov). Fix _addToWatched
without path.sep
(@wyicwx).globule@1.0.0
with minimatch >= 3.0.0
.maxListeners
. Update globule
to 0.2.0
.error
from readdir
(@oconnore). Fix for 0 maxListeners
. Use graceful-fs
to avoid EMFILE
errors in other places fs
is used. Better method to determine if pathwatcher
was built. Fix keeping process alive too much, only init pathwatcher
if a file is being watched. Set min required to Windows Vista when building on Windows (@pvolok).watched()
. Fix for erroneous added
events on folders. Ignore msvs
build 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 globule@0.2.0
. No longer watches cwd
by default. Added mode
option. Better EMFILE
message. Avoids ENOENT
errors with symlinks. All constructor arguments are optional.ENOENT
error with non-existent symlinks [BACKPORTED].setImmediate
(process.nextTick
for Node.js v0.8) to defer ready
/nomatch
events (@amasad).nomatch
event 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).watchDir
not respecting close in race condition (@chrisirhc).globule
for file matching. Avoid Node.js v0.10 path.resolve
/join
errors. 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 added
events (@groner).end
before removeAllListeners
.added
events within subfolder patterns.forceWatchMethod
option removed, bug fixes and watch optimizations (@rgaskill).add
calls dont get watched (@samcday). removeAllListeners
on close
.added
events in current working dir.path.sep
. Add forceWatchMethod
option. Support renamed
events.cwd
option properlytoo many open file
” errorsCopyright (c) 2018 Kyle Robinson Young
Licensed under the MIT license.
FAQs
A globbing fs.watch wrapper built from the best parts of other fine watch libs.
The npm package gaze receives a total of 2,107,883 weekly downloads. As such, gaze popularity was classified as popular.
We found that gaze demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.