Git Product home page Git Product logo

caseywebdev / watchr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bevry/watchr

0.0 3.0 0.0 171 KB

Better file system watching for Node.js. Provides a normalised API the file watching APIs of different node versions, nested/recursive file and directory watching, and accurate detailed events for file/directory changes, deletions and creations.

License: MIT License

Shell 0.12% CoffeeScript 96.23% JavaScript 3.65%

watchr's Introduction

Watchr: better file system watching for Node.js Build Status

Watchr provides a normalised API the file watching APIs of different node versions, nested/recursive file and directory watching, and accurate detailed events for file/directory creations, updates, and deletions.

You install it via npm install watchr and use it via require('watchr').watch(config). Available configuration options are:

  • path a single path to watch
  • paths an array of paths to watch
  • listener a single change listener to fire when a change occurs
  • listeners an array of listeners to fire when a change occurs, overloaded to accept the following values:
    • changeListener a single change listener
    • [changeListener] an array of change listeners
    • {eventName:eventListener} an object keyed with the event names and valued with a single event listener
    • {eventName:[eventListener]} an object keyed with the event names and valued with an array of event listeners
  • next (optional, defaults to null) a completion callback to fire once the watchers have been setup, arguments are:
    • when using the path configuration option: err, watcherInstance
    • when using the paths configuration option: err, [watcherInstance,...]
  • stat (optional, defaults to null) a file stat object to use for the path, instead of fetching a new one
  • interval (optional, defaults to 100) for systems that poll to detect file changes, how often should it poll in millseconds
  • persistent (optional, defaults to true) whether or not we should keep the node process alive for as long as files are still being watched
  • duplicateDelay (optional, defaults to 1000) sometimes events will fire really fast, this delay is set in place so we don't fire the same event within the timespan
  • preferredMethods (optional, defaults to ['watch','watchFile']) which order should we prefer our watching methods to be tried?
  • ignorePaths (optional, defaults to false) an array of full paths to ignore
  • ignoreHiddenFiles (optional, defaults to false) whether or not to ignored files which filename starts with a .
  • ignoreCommonPatterns (optional, defaults to true) whether or not to ignore common undesirable file patterns (e.g. .svn, .git, .DS_Store, thumbs.db, etc)
  • ignoreCustomPatterns (optional, defaults to null) any custom ignore patterns that you would also like to ignore along with the common patterns

The following events are available to your via the listeners:

  • log for debugging, receives the arguments logLevel ,args...
  • error for gracefully listening to error events, receives the arguments err
  • watching for when watching of the path has completed, receives the arguments err, isWatching
  • change for listening to change events, receives the arguments changeType, fullPath, currentStat, previousStat, received arguments will be:
    • for updated files: 'update', fullPath, currentStat, previousStat
    • for created files: 'create', fullPath, currentStat, null
    • for deleted files: 'delete', fullPath, null, previousStat

To wrap it all together, it would look like this:

// Require
var watchr = require('watchr');

// Watch a directory or file
console.log('Watch our paths');
watchr.watch({
	paths: ['path1','path2','path3'],
	listeners: {
		log: function(logLevel){
			console.log('a log message occured:', arguments);
		},
		error: function(err){
			console.log('an error occured:', err);
		},
		watching: function(err,watcherInstance,isWatching){
			if (err) {
				console.log("watching the path " + watcherInstance.path + " failed with error", err);
			} else {
				console.log("watching the path " + watcherInstance.path + " completed");
			}
		},
		change: function(changeType,filePath,fileCurrentStat,filePreviousStat){
			console.log('a change event occured:',arguments);
		}
	},
	next: function(err,watchers){
		if (err) {
			return console.log("watching everything failed with error", err);
		} else {
			console.log('watching everything completed', watchers);
		}

		// Close watchers after 60 seconds
		setTimeout(function(){
			var i;
			console.log('Stop watching our paths');
			for ( i=0;  i<watchers.length; i++ ) {
				watchers[i].close();
			}
		},60*1000);
	}
});

You can test the above code snippet by running the following:

npm install watchr
./node_modules/.bin/watchr

Support

Support can be found in the GitHub Issue Tracker

History

You can discover the history inside the History.md file

License

Licensed under the incredibly permissive MIT License
Copyright © 2012+ Bevry Pty Ltd
Copyright © 2011 Benjamin Lupton

watchr's People

Contributors

adamsanderson avatar balupton avatar caseywebdev avatar robsonpeixoto avatar

Watchers

 avatar  avatar  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.