sane
sane
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:
By default stays away from fs polling because it's very slow and cpu intensive
Uses
fs.watch
by default and sensibly works around the various issuesMaintains a consistent API across different platforms
Where
fs.watch
is not reliable you have the choice of using the following alternatives:polling
Install
How to choose a mode
Don't worry too much about choosing the correct mode upfront because sane maintains the same API across all modes and will be easy to switch.
If you're only supporting Linux and OS X,
watchman
would be the most reliable modeIf you're using node > v0.10.0 use the default mode
If you're running OS X and you're watching a lot of directories and you're running into https://github.com/joyent/node/issues/5463, use
watchman
If you're in an environment where native file system events aren't available (like Vagrant), you should use polling
Otherwise, the default mode should work well for you
API
sane(dir, options)
Watches a directory and all its descendant directories for changes, deletions, and additions on files and directories.
options:
glob
: a single string glob pattern or an array of them.poll
: puts the watcher in polling mode. Under the hood that meansfs.watchFile
.watchman
: makes the watcher use watchman.watchmanPath
: sets a custom path forwatchman
binary.watchexec
: makes the watcher use watchexec.dot
: enables watching files/directories that start with a dot.ignored
: a glob, regex, function, or array of any combination.
For the glob pattern documentation, see micromatch. If you choose to use watchman
you'll have to install watchman yourself). If you choose to use watchexec
you'll have to install watchexec yourself). For the ignored options, see anymatch.
sane.NodeWatcher(dir, options)
The default watcher class. Uses fs.watch
under the hood, and takes the same options as sane(dir, options)
.
sane.WatchmanWatcher(dir, options)
The watchman watcher class. Takes the same options as sane(dir, options)
.
sane.Watchexec(dir, options)
The watchexec watcher class. Takes the same options as sane(dir, options)
.
sane.PollWatcher(dir, options)
The polling watcher class. Takes the same options as sane(dir, options)
with the addition of:
interval: indicates how often the files should be polled. (passed to fs.watchFile)
sane.{Node|Watchman|Watchexec|Poll}Watcher#close
Stops watching.
sane.{Node|Watchman|Watchexec|Poll}Watcher events
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
CLI
This module includes a simple command line interface, which you can install with npm install sane -g
.
It will watch the given directory
and run the given every time a file changes.
CLI example usage
sane 'echo "A command ran"'
sane 'echo "A command ran"' --glob='**/*.css'
sane 'echo "A command ran"' site/assets/css --glob='**/*.css'
sane 'echo "A command ran"' --glob='**/*.css' --ignored='**/ignore.css'
sane 'echo "A command ran"' --wait=3
sane 'echo "A command ran"' -p
License
MIT
Credits
The CLI was originally based on the watch CLI. Watch is licensed under the Apache License Version 2.0.
Last updated