I've been driven to insanity by node filesystem watcher wrappers. Sane aims to be fast, small, and reliable file system watcher. It does that by:
- Always use fs.watch (unless polling is forced) and sensibly workaround the various issues with it
- Sane is all JavaScript, no native components
- Stay away from polling because it's very slow and cpu intensive
- Support polling for environments like Vagrant shared directory where there are no native filesystem events
$ npm install sane
If you're using node < v0.10.0 then make sure to start sane with poll: true
.
Watches a directory and all it's descendant directorys for changes, deletions, and additions on files and directories.
Shortcut for new sane.Watcher(dir, {glob: globs, ..options})
.
var watcher = sane('path/to/dir', ['**/*.js', '**/*.css']);
watcher.on('ready', function () { console.log('ready') });
watcher.on('change', function (filepath, root, stat) { console.log('file changed', filepath); });
watcher.on('add', function (filepath, root, stat) { console.log('file added', filepath); });
watcher.on('delete', function (filepath, root) { console.log('file deleted', filepath); });
// close
watcher.close();
For options
see sane.Watcher
.
options:
persistent
: boolean indicating that the process shouldn't die while we're watching files.glob
: a single string glob pattern or an array of them.poll
: puts the watcher in polling mode. Under the hood that meansfs.watchFile
.interval
: indicates how often the files should be polled. (passed tofs.watchFile
)
For the glob pattern documentation, see minimatch.
Stops watching.
Emits the following events:
All events are passed the file/dir path relative to the root directory
ready
when the program is ready to detect events in the directorychange
when a file changesadd
when a file or directory has been addeddelete
when a file or directory has been deleted
MIT