GithubHelp home page GithubHelp logo

rlugojr / sane Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amasad/sane

0.0 0.0 0.0 139 KB

sane aims to be fast, small, and reliable filesystem watcher. No bells and whistles, just change events.

JavaScript 100.00%

sane's Introduction

CircleCI

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 issues
  • Maintains a consistent API across different platforms
  • Where fs.watch is not reliable you have the choice of using the following alternatives:

Install

$ npm install sane

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 mode
  • If 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 nodejs/node-v0.x-archive#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 it's descendant directories for changes, deletions, and additions on files and directories.

var watcher = sane('path/to/dir', {glob: ['**/*.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();

options:

  • glob: a single string glob pattern or an array of them.
  • poll: puts the watcher in polling mode. Under the hood that means fs.watchFile.
  • watchman: makes the watcher use watchman.
  • 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 minimatch. If you choose to use watchman you'll have to install watchman 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(options, dir).

sane.WatchmanWatcher(dir, options)

The watchman watcher class. Takes the same options as sane(options, dir).

sane.PollWatcher(dir, options)

The polling watcher class. Takes the same options as sane(options, dir) with the addition of:

  • interval: indicates how often the files should be polled. (passed to fs.watchFile)

sane.{Node|Watchman|Poll}Watcher#close

Stops watching.

sane.{Node|Watchman|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 directory
  • change when a file changes
  • add when a file or directory has been added
  • delete 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.

Usage: sane <command> [...directory] [--glob=<filePattern>] [--poll] [--watchman] [--dot] [--wait=<seconds>]

OPTIONS:
    --glob=<filePattern>
        A single string glob pattern or an array of them.

    --poll, -p
      Use polling mode.

    --watchman, -w
      Use watchman (if available).

    --dot, -d
      Enables watching files/directories that start with a dot.

    --wait=<seconds>
        Duration, in seconds, that watching will be disabled
        after running <command>. Setting this option will
        throttle calls to <command> for the specified duration.

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"' --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.

sane's People

Contributors

amasad avatar bcardarella avatar bguiz avatar callumlocke avatar ccheever avatar chadhietala avatar chmanie avatar dguettler avatar ixtli avatar jonathanong avatar kelonye avatar m-allanson avatar mndvns avatar moudy avatar ro-savage avatar stefanpenner avatar tcoopman avatar tomccabe avatar wez avatar wtgtybhertgeghgtwtg avatar xdissent avatar yungsters avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.