• Stars
    star
    386
  • Rank 111,213 (Top 3 %)
  • Language
    JavaScript
  • Created over 10 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Try on repl.it CI

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 its 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.
  • watchmanPath: sets a custom path for watchman 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 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] [--watchman-path=<watchmanBinaryPath>] [--dot] [--wait=<seconds>]

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

    --ignored=<filePattern>
      A glob, regex, function, or array of any combination.

    --poll, -p
      Use polling mode.

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

    --watchman-path=<watchmanBinaryPath>
      Sets a custom path for watchman binary (if using this mode).

    --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.
    --quiet, -q
      Disables sane's console output

    --changes-only, -o
      Runs <command> only when a change occur. Skips running <command> at startup

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.

More Repositories

1

debug_utils

Advanced JavaScript Debugging Utility Library
JavaScript
642
star
2

debugjs

[Experimental] Lightweight JavaScript VM and stepping debugger in JavaScript
JavaScript
202
star
3

debugjs.com

Debug your JavaScript in the browser
JavaScript
98
star
4

learnable-programming-demo

Implementation of demos from Learnable Programming
JavaScript
52
star
5

DOMQL

Web development for DBAs
JavaScript
48
star
6

eHistory

A Google Chrome extensions that enhances the browser's history search and management in many ways.
JavaScript
41
star
7

soundofjs

Listen to your JavaScript code on an 808 drum machine
CSS
36
star
8

arabish

Arabic Transliteration in Python
Python
32
star
9

static-here

A command-line tool for creating a static file server from the current working directory.
JavaScript
21
star
10

pg-basic

JavaScript
19
star
11

context-eval

Simple execution context for node and the browser.
JavaScript
16
star
12

generator-supported

Check if JS harmony generators is supported in environment
JavaScript
7
star
13

simple-counter

dead simple stats counter that flushes to disk
JavaScript
6
star
14

SpaceTime

Chrome History Visualization
CoffeeScript
5
star
15

github-slideshow

A robot powered training repository πŸ€–
Ruby
4
star
16

build_your_own_js_infra_starter

Some unit tests and boilerplate
JavaScript
4
star
17

nextjs-template

for replit
JavaScript
4
star
18

component-app-seed

Seed project for Component apps
JavaScript
4
star
19

testing_with_zeb

HTML
2
star
20

test

EJS
2
star
21

amasad.github.com

gh page
HTML
2
star
22

esparse

Go
2
star
23

timer

Timer countdown component
JavaScript
2
star
24

jest-playground

Play around with jest
JavaScript
2
star
25

django-template

Python
2
star
26

test_repo

Python
1
star
27

console

textarea-based browser console, similar to Chrome dev tools console
JavaScript
1
star
28

enzyme-playground

JavaScript
1
star
29

hello_yc

Python
1
star
30

fdsa

1
star
31

biwascheme_clone

JavaScript
1
star
32

psut-demo

Python
1
star
33

bootstrap-css

Bootstrap css and font for Component
1
star
34

busygo

busybox docker image with golang installed.
1
star