Package Exports
- watchr
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (watchr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
watchr
Better file system watching for Node.js
Install
- Install:
npm install --save watchr
- Executable:
watchr
- Module:
require('watchr')
Editions
This package is published with the following editions:
watchr/src/lib/getmac.coffee
is Source + CoffeeScript + Requirewatchr/es5/lib/getmac.js
is CoffeeScript Compiled JavaScript + ES5 + Require
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 watchpaths
an array of paths to watchlistener
a single change listener to fire when a change occurslisteners
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 tonull
) 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,...]
- when using the
stat
(optional, defaults tonull
) a file stat object to use for the path, instead of fetching a new oneinterval
(optional, defaults to5007
) for systems that poll to detect file changes, how often should it poll in millsecondspersistent
(optional, defaults totrue
) whether or not we should keep the node process alive for as long as files are still being watchedcatchupDelay
(optional, defaults to2000
) because swap files delete the original file, then rename a temporary file over-top of the original file, to ensure the change is reported correctly we must have a delay in place that waits until all change events for that file have finished, before starting the detection of what changedpreferredMethods
(optional, defaults to['watch','watchFile']
) which order should we prefer our watching methods to be tried?followLinks
(optional, defaults totrue
) follow symlinks, i.e. use stat rather than lstatignorePaths
(optional, defaults tofalse
) an array of full paths to ignoreignoreHiddenFiles
(optional, defaults tofalse
) whether or not to ignored files which filename starts with a.
ignoreCommonPatterns
(optional, defaults totrue
) whether or not to ignore common undesirable file patterns (e.g..svn
,.git
,.DS_Store
,thumbs.db
, etc)ignoreCustomPatterns
(optional, defaults tonull
) 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 argumentslogLevel ,args...
error
for gracefully listening to error events, receives the argumentserr
- you should always have an error listener, otherwise node.js's behavior is to throw the error and possibly crash your application, see #40
watching
for when watching of the path has completed, receives the argumentserr, isWatching
change
for listening to change events, receives the argumentschangeType, 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
- for updated files:
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 () {
console.log('Stop watching our paths')
for ( var i = 0; i < watchers.length; i++ ) {
watchers[i].close()
}
}, 60 * 1000)
}
})
You can test the above code snippet by running the following:
npm install -g watchr
watchr
History
Discover the release history by heading on over to the HISTORY.md
file.
Contribute
Discover how you can contribute by heading on over to the CONTRIBUTING.md
file.
Backers
Maintainers
These amazing people are maintaining this project:
Sponsors
No sponsors yet! Will you be the first?
Contributors
These amazing people have contributed code to this project:
- Benjamin Lupton — view contributions
- Aaron O'Mullan — view contributions
- Adam Sanderson — view contributions
- Casey Foster — view contributions
- Fredrik Norén — view contributions
- Robson Roberto Souza Peixoto — view contributions
- Stuart Knightley — view contributions
- David Byrd — view contributions
Discover how you can contribute by heading on over to the CONTRIBUTING.md
file.
License
Unless stated otherwise all works are:
- Copyright © 2012+ Bevry Pty Ltd
- Copyright © 2011 Benjamin Lupton
and licensed under: