inotifywait-spawn
v1.0.3
Published
A zero dependencies, 100% code covered, inotifywait wrap based on inotify-tools.
Downloads
18
Maintainers
Readme
inotifywait-spawn
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 accessIN_MODIFY
, to be notified on changesIN_CLOSE_WRITE
, to be notified on end writingIN_CLOSE_NOWRITE
, to be notified on end readingIN_OPEN
, to be notified on file openedIN_MOVED_FROM
, to be notified on files moved fromIN_MOVED_TO
, to be notified on files move toIN_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 pathIN_MOVE_SELF
, to be notified when watched path is movedIN_UNMOUNT
, to be notified on patsh unmountedIN_CLOSE
, to be notified on eitherIN_CLOSE_WRITE
orIN_CLOSE_NOWRITE
IN_MOVE
, to be notified on eitherIN_MOVED_FROM
orIN_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.