Git Product home page Git Product logo

inotifywait-spawn's Introduction

inotifywait-spawn

Build Status Coverage Status

Social Media Photo by Omid Kashmari on Unsplash

A zero dependencies, 100% code covered, inotifywait wrap based on inotify-tools and spawn.

API

// listen to events via:
// inw.on(INotifyWait.IN_CLOSE, ({type, path, entry}) => {});
class INotifyWait extends EventEmitter {

  constructor(
    path,               // a single path, or a list of paths, each
                        // representing either a file or a folder
    options = {
      exclude: null,    // a RegExp to exclude files (passed as shell argument)
      include: null,    // a RegExp to include files (passed as shell argument)
                        // Please note `include` option requires inotifywait 3.20+
      persistent: true, // keep watching until .stop()
      recursive: false, // recursive within folders
      events: 0         // one or more events to listen for
                        // if omitted, all events are listened
    }
  ) {}

  // kill the subprocess and stop listening to any event
  stop() {}
}

For RegExp properties, use the /*\.txt/i flag to make it case insensitive.

Please read inotifywait man page to know more about the underlying features offered by include and exclude.

Python 3 Module

In order to run this module in python too, please be sure you have installed the following pip dependency.

sudo pip3 install inotify_simple

Why Yet Another inotify Project ?

Because every other project has either problems compiling code in this or that version of NodeJS, or it's been unmaintained for months or years, with growing amount of bugs.

This project wants to keep it both simple and portable, relying on system inotifywait, avoiding any present or future issue with native/compiled code, making it easy to bring and work with on ARM and other IoT devices too.

Where there is NodeJS, and there is inotifywait, this project might be all you need.

Example

const INotifyWait = require('inotifywait-spawn');
const {IN_CLOSE_WRITE, IN_DELETE, IN_MODIFY} = INotifyWait;

// single file
const inw = new INotifyWait('test.txt', {events: IN_DELETE | IN_CLOSE_WRITE});
inw.on('error', console.error);
inw.on(
  IN_DELETE,
  ({type, path}) => console.log(`${path} removed`)
);
inw.on(
  IN_CLOSE_WRITE,
  ({type, path}) => console.log(`${path} ready to be read`)
);

// folder
const inw = new INotifyWait('.', {recursive: true, events: IN_MODIFY});
inw.on('error', console.error);
inw.on(IN_MODIFY, ({type, path, entry}) => {
  type === IN_MODIFY; // the event type is always available
  console.log(`${entry} modified in ${path}`);
});

// multiple files/folders (still one spawned process only)
const inw = new INotifyWait(
  ['test', 'node_modules', 'build'],
  {recursive: true, events: IN_MODIFY}
);
inw.on('error', console.error);
inw.on(IN_MODIFY, ({type, path, entry}) => {
  console.log(`${entry} modified in ${path}`);
});

Please check test/index.js to see or know more.

Events

Following the list of events supported and available via inotifywait.

These events are better described in the inotify documentation.

  • IN_ACCESS, to be notified on file access
  • IN_MODIFY, to be notified on changes
  • IN_CLOSE_WRITE, to be notified on end writing
  • IN_CLOSE_NOWRITE, to be notified on end reading
  • IN_OPEN, to be notified on file opened
  • IN_MOVED_FROM, to be notified on files moved from
  • IN_MOVED_TO, to be notified on files move to
  • IN_CREATE, to be notified on files creation (folder)
  • IN_DELETE, to be notified on deletion (folder)
  • IN_DELETE_SELF, to be notified on deletion of the watched path
  • IN_MOVE_SELF, to be notified when watched path is moved
  • IN_UNMOUNT, to be notified on patsh unmounted
  • IN_CLOSE, to be notified on either IN_CLOSE_WRITE or IN_CLOSE_NOWRITE
  • IN_MOVE, to be notified on either IN_MOVED_FROM or IN_MOVED_TO

Compatibility

Fully tested on ArchLinux from NodeJS 6 to latest, this should work with every other Linux distribution that offers inotify-tools and inotifywait with it.

Please note that some options might not be available with older versions of inotifywait, like it is for include in current Ubuntu and inotifywait version < 3.20.

inotifywait-spawn's People

Contributors

webreflection avatar

Watchers

James Cloos 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.