Giter Club home page Giter Club logo

filewatcher's Introduction

filewatcher

Build Status

Simple wrapper around fs.watch that works around the various issues you have to deal with when using the Node API directly.

More precisely filewatcher โ€ฆ

  • always reports file names (for all events on all OSes)
  • works well with editors that perform atomic writes (save & rename) like Sublime Text or vim
  • doesn't fire twice when files are saved
  • falls back to fs.watchFile when running out of file handles
  • has no native dependencies
  • uses Node's async APIs under the hood

This module is used by node-dev and instant.

Usage

var filewatcher = require('filewatcher');

var watcher = filewatcher();

// watch a file
watcher.add(__filename);

// ... or a directory
watcher.add(__dirname);

watcher.on('change', function(file, stat) {
  console.log('File modified: %s', file);
  if (!stat) console.log('deleted');
});

To stop watching, you can remove either a single file or all watched files at once:

watcher.remove(file)
watcher.removeAll()

Notify users when falling back to polling

When the process runs out of file handles, filewatcher closes all watchers and transparently switches to fs.watchFile polling. You can notify your users by listening to the fallback event:

watcher.on('fallback', function(limit) {
  console.log('Ran out of file handles after watching %s files.', limit);
  console.log('Falling back to polling which uses more CPU.');
  console.log('Run ulimit -n 10000 to increase the limit for open files.');
});

Options

You can pass options to filewatcher() in order to tweak its internal settings. These are the defaults:

// the default options
var opts = {
  forcePolling: false,  // try event-based watching first
  debounce: 10,         // debounce events in non-polling mode by 10ms
  interval: 1000,       // if we need to poll, do it every 1000ms
  persistent: true      // don't end the process while files are watched
};

var watcher = filewatcher(opts)

The MIT License (MIT)

Copyright (c) 2013-2016 Felix Gnass

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.

filewatcher's People

Contributors

fgnass avatar juliangruber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

filewatcher's Issues

fs.watch with recursion doesn't work on Linux

Hello, I'm having a problem running a project which uses filewatcher as a dependency, I'm running node v14.1.0 on the latest version of Manjaro, I've encountered this error:

> [email protected] start /home/onlurking/Documents/band-aid
> ts-node-dev -r ./src/env.ts --respawn src/server.ts --errorOnMissing=true

Using ts-node version 8.6.2, typescript version 3.8.3
TypeError [ERR_FEATURE_UNAVAILABLE_ON_PLATFORM]: The feature watch recursively is unavailable on the current platform, which is being used to run Node.js
    at Object.watch (fs.js:1441:11)
    at add (/home/onlurking/Documents/band-aid/node_modules/filewatcher/index.js:74:34)
    at /home/onlurking/Documents/band-aid/node_modules/filewatcher/index.js:93:5
    at FSReqCallback.oncomplete (fs.js:176:5)

The recursion option is not avalaible on Linux systems according to the node.js fs.watch documentation:

The recursive option is only supported on macOS and Windows. An ERR_FEATURE_UNAVAILABLE_ON_PLATFORM exception will be thrown when the option is used on a platform that does not support it.

I'm working on a fix.

How to identify event types

Hi,

Could you please tell how to identify event type like : create, change, delete, rename and etc.

Thanks

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.