npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

saw

v1.0.7

Published

actually working file tree watching library

Downloads

74

Readme

saw

actually working file tree watching library

build status

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.

Rationale

File/directory watching in node.js is notoriously bad. For example:

  1. 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.
  2. If you're lucky enough to get a filename from 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.
  3. fs.watch() does not detect adds, removes, or updates specifically. Just "change". Useless.
  4. 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.
  5. 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:

  1. Events aren't emitted in some cases, especially relating to subdirectories
  2. Events are duplicated in some cases (fs.watch() is also guilty of this)
  3. Poor error handling such as not accounting for ENOFILE (race condition from deletion) or EMFILE (large directory trees)
  4. Deletes of directories poorly handled -- you might get notified that a directory was deleted, but not notified of each file in that directory
  5. Lack of node 0.10 support
  6. May require you to add files, directories, or patterns manually to the watcher
  7. Getting fancy with globs/filters, when the best separation of concerns is for a watching library to just watch and tell you the filename and what happened.
  8. Wonky APIs which try to reinvent fs.Stats or are generally over-engineered
  9. Lack of documentation or commitment from author
  10. Being written in, ahem, coffee-script
  11. Et cetera!

saw takes a very simple and reliable approach which consists of:

  1. Recursing through the directory given
  2. Applying fs.watch() to all newly detected files
  3. Caching fs.Stats instances for all files
  4. Comparing the file tree with the last scan (if available) and emitting events based on the difference
  5. Performing steps 1-4 on the changed dir (or parent of the changed file) when fs.watch() triggers an event.

Usage

var saw = require('saw');

saw('path/to/dir', {options: 'are optional'})
  .on('ready', function (files) {
    // watcher is active. `files` is an array of file objects (details below).
  })
  .on('add', function (file) {
    // `file.path` = relative path from root dir
    // `file.fullPath` = absolute path
    // `file.name` = file name
    // `file.stat` = instance of `fs.Stats`
    // `file.parentDir` = relative parent dir
    // `file.fullParentDir` = absolute parent dir
  })
  .on('remove', function (file) {
    // file was removed
  })
  .on('update', function (file) {
    // file was updated
    // caveat: updates within a millisecond after the file was added or updated
    // can't be detected
  })
  .on('all', function (ev, file) {
    // catchall - `ev` is the event name.
  })
  // to unwatch all files, call close():
  .close()

Tips

  • You can also pass a glob pattern or array of glob patterns as the first argument.
  • You can omit the first argument to watch the current working directory.
  • The file object is the same as returned by readdirp.

Options

  • cwd (String, default: first argument or process.cwd() if first argument isn't a directory), the directory to consider as root for relative paths.
  • persistent (Boolean, default: true), whether or not to keep the process open when watching is active.
  • dot (Boolean, default: false), whether or not to watch dotfiles.
  • cache (Object), options to pass to lru-cache.

Developed by Terra Eclipse

Terra Eclipse, Inc. is a nationally recognized political technology and strategy firm located in Aptos, CA and Washington, D.C.


License: MIT

  • Copyright (C) 2013-14 Carlos Rodriguez (http://s8f.org/)
  • Copyright (C) 2013-14 Terra Eclipse, Inc. (http://www.terraeclipse.com/)

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.