actually working file tree watching library
Watch for changes in a file tree. I wrote this because in my search for an actually reliable/working watcher library on npm, I kept coming up short. This implementation is brief and accomplishes its goal reliably.
File/directory watching in node.js is notoriously bad. For example:
fs.watch()when used on a directory, might not tell you what file changed. Just that "something" in the directory changed. Depending on the platform. Useless.
fs.watch(), you're in the dark on whether that file is actually a directory. Or any other details on the file in question. Useless.
fs.watch()does not detect adds, removes, or updates specifically. Just "change". Useless.
fs.watch()when used on a directory, only watches one level deep. It's up to you to create more watch instances for subdirectories. Useless.
fs.watchFile(), the alternative to
fs.watch(), when used on a directory, doesn't give you the filename of the change either. Also it uses polling which is obviously hacky and defeats the goal of an evented watcher. Useless.
Moreover, npm-level watching libraries that attempt to remedy the above have annoying caveats, such as:
fs.watch()is also guilty of this)
fs.Statsor are generally over-engineered
saw takes a very simple and reliable approach which consists of:
fs.watch()to all newly detected files
fs.Statsinstances for all files
fs.watch()triggers an event.
var saw = ;// to unwatch all files, call close():
fileobject is the same as returned by readdirp.
cwd(String, default: first argument or
process.cwd()if first argument isn't a directory), the directory to consider as root for relative paths.
true), whether or not to keep the process open when watching is active.
false), whether or not to watch dotfiles.
cache(Object), options to pass to lru-cache.
Terra Eclipse, Inc. is a nationally recognized political technology and strategy firm located in Aptos, CA and Washington, D.C.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.