パッケージの詳細

watchr

bevry105.9kMIT6.11.0

Better file system watching for Node.js

esnext, fs, fswatcher, node

readme

watchr

Status of the GitHub Workflow: bevry NPM version NPM downloads Dependency Status Dev Dependency Status
GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

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.

Usage

Complete API Documentation.

There are two concepts in watchr, they are:

  • Watcher - this wraps the native file system watching, makes it reliable, and supports deep watching
  • Stalker - this wraps the watcher, such that for any given path, there can be many stalkers, but only one watcher

The simplest usage is:

// Import the watching library
var watchr = require('watchr')

// Define our watching parameters
var path = process.cwd()
function listener(changeType, fullPath, currentStat, previousStat) {
    switch (changeType) {
        case 'update':
            console.log(
                'the file',
                fullPath,
                'was updated',
                currentStat,
                previousStat
            )
            break
        case 'create':
            console.log('the file', fullPath, 'was created', currentStat)
            break
        case 'delete':
            console.log('the file', fullPath, 'was deleted', previousStat)
            break
    }
}
function next(err) {
    if (err) return console.log('watch failed on', path, 'with error', err)
    console.log('watch successful on', path)
}

// Watch the path with the change listener and completion callback
var stalker = watchr.open(path, listener, next)

// Close the stalker of the watcher
stalker.close()

More advanced usage is:

// Create the stalker for the path
var stalker = watchr.create(path)

// Listen to the events for the stalker/watcher
stalker.on('change', listener)
stalker.on('log', console.log)
stalker.once('close', function (reason) {
    console.log('closed', path, 'because', reason)
    stalker.removeAllListeners() // as it is closed, no need for our change or log listeners any more
})

// Set the default configuration for the stalker/watcher
stalker.setConfig({
    stat: null,
    interval: 5007,
    persistent: true,
    catchupDelay: 2000,
    preferredMethods: ['watch', 'watchFile'],
    followLinks: true,
    ignorePaths: false,
    ignoreHiddenFiles: false,
    ignoreCommonPatterns: true,
    ignoreCustomPatterns: null,
})

// Start watching
stalker.watch(next)

// Stop watching
stalker.close()

Install

npm

  • Install: npm install --save watchr
  • Import: import * as pkg from ('watchr')
  • Require: const pkg = require('watchr')

Editions

This package is published with the following editions:

  • watchr aliases watchr/source/index.js
  • watchr/source/index.js is ESNext source code for Node.js 10 || 12 || 14 || 16 with Require for modules

TypeScript

This project provides its type information via inline JSDoc Comments. To make use of this in TypeScript, set your maxNodeModuleJsDepth compiler option to 5 or thereabouts. You can accomlish this via your tsconfig.json file like so:

{
  "compilerOptions": {
    "maxNodeModuleJsDepth": 5
  }
}

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?

GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

Contributors

These amazing people have contributed code to this project:

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

License

Unless stated otherwise all works are:

and licensed under:

更新履歴

History

v6.11.0 2021 July 31

v6.10.0 2021 July 28

v6.9.0 2020 October 29

v6.8.0 2020 September 5

v6.7.0 2020 August 18

v6.6.0 2020 August 4

v6.5.0 2020 July 23

v6.4.0 2020 June 25

v6.3.0 2020 June 21

v6.2.0 2020 June 10

v6.1.0 2020 May 22

v6.0.0 2020 May 21

  • Updated dependencies, base files, and editions using boundation
  • Minimum required node version changed from node: >=8 to node: >=10 to keep up with mandatory ecosystem changes

v5.6.0 2019 December 10

v5.5.0 2019 December 1

v5.4.0 2019 December 1

v5.3.0 2019 November 18

v5.2.0 2019 November 18

v5.1.0 2019 November 13

v5.0.0 2019 November 10

  • Updated dependencies, base files and editions using boundation
  • Development dependency rimraf now requires node version 6 at minimum
    • As such, the minimum supported node version of watchr has changed from 0.12 to the latest LTS at the time of this release which is 8

v4.1.0 2018 December 7

v4.0.1 2018 January 24

v4.0.0 2018 January 24

  • This is an API backwards compatible release, however the underlying changes may introduce some problems, so a rolling adoption is warranted
  • Directory contents are now scanned in parallel
  • Directory scanning is now done by scandirectory v3 (instead of v2) which uses readdir-cluster
  • Updated dependencies
  • Updated base files

v3.0.1 2016 October 23

  • Fixed open not returning the stalker instance - Thanks to Davide Mancuso for issue #88
  • Fixed documentation on create not indicating that it returns the stalker instance
  • Updated base files

v3.0.0 2016 October 19

  • Rewrote for better stability, all issues should now be considered closed
  • Converted from CoffeeScript to JavaScript
  • Node v0.8 support added once again (before node v0.12 was the earliest supported version)
  • Added jsdoc
  • Added flow type annotations

v2.6.0 2016 July 15

v2.5.0 2016 July 15

  • Updated dependencies
  • Updated engines to be node >=0.12 as to align with safefs v4 - May still work with node 0.10, file a bug report if it doesn't

v2.4.13 2015 February 7

  • Updated dependencies

v2.4.12 2014 December 17

v2.4.11 2014 February 7

  • Fixed interval option not beeing passed on to child watchers (regression since v2.4.7) - Thanks to David Byrd for pull request #58

v2.4.10 2014 February 7

v2.4.9 2014 January 28

  • Fixed "me" is undefined errors (regression since v2.4.7)

v2.4.8 2013 December 30

  • You can now pass falsey values forcatchupDelay to disable it

v2.4.7 2013 December 19

v2.4.6 2013 November 18

  • Updated dependencies

v2.4.5 2013 November 17

  • Updated dependencies

v2.4.4 2013 October 10

  • Added the ability to turn off following links by setting followLinks to false - Thanks to Fredrik Noren for pull request #47
  • Prefer accuracy over speed - Use the watch method by default, but don't trust it at all, always double check everything

v2.4.3 2013 April 10

  • More work on swap file handling

v2.4.2 2013 April 10

  • File copies will now trigger events throughout the copy rather than just at the start of the copy - Close issue #35

v2.4.1 2013 April 10

  • Fixed bubblr events
  • Fixed swap file detection

v2.4.0 2013 April 5

  • Updated dependencies

v2.3.10 2013 April 1

  • Updated dependencies

v2.3.9 2013 March 17

  • Made it so if duplicateDelay is falsey we will not do a duplicate check

v2.3.8 2013 March 17

v2.3.7 2013 February 6

  • Changed the preferredMethod option into preferredMethods which accepts an array, defaults to ['watch','watchFile']
  • If the watch action fails at the eve level we will try again with the preferredMethods reversed - This solves issue #31 where watching of large files would fail
  • Changed the interval option to default to 5007 (recommended by node) instead of 100 as it was before - The watch method provides us with immediate notification of changes without utilising polling, however the watch method fails for large amounts of files, in which case we will fall back to the watchFile method that will use this option, if the option is too small we will be constantly polling the large amount of files for changes using up all the CPU and memory, hence the change into a larger increment which has no CPU and memory impact.

v2.3.6 2013 February 6

  • Fixed fallback when preferredMethod is watchFile

v2.3.5 2013 February 6

  • Fixed uncaught exceptions when intialising watchers under certain circumstances

v2.3.4 2013 February 5

  • Better handling and detection of failed watching operations
  • Better handling of duplicated events
  • Watching is now an atomic operation - If watching fails for a descendant, we will close everything related to that watch operation of the eve
  • We now prefer the watch method over the watchFile method - This offers great reliability and way less CPU and memory foot print - If you still wish to prefer watchFile, then set the new configuration option preferredMethod to watchFile
  • Closes issue #30 thanks to Howard Tyson

v2.3.3 2013 January 8

v2.3.2 2013 January 6

v2.3.1 2012 December 19

v2.3.0 2012 December 17

  • This is a backwards compatiblity break, however updating is easy, read the notes below.
  • We've updated the events we emit to be: - log for debugging, receives the arguments logLevel ,args... - 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 - error for gracefully listening to error events, receives the arguments err - read the README to learn how to bind to these new events
  • The changeType argument for change listeners has been changed for better clarity and consitency: - change is now update - new is now create - unlink is now delete
  • We've updated the return arguments for require('watchr').watch for better consitency: - if you send the paths option, you will receive the arguments err, results where results is an array of watcher instances - if you send the path option, you receive the arguments err, watcherInstance

v2.2.1 2012 December 16

  • Fixed sub directory scans ignoring our ignore patterns
  • Updated dependencies

v2.2.0 2012 December 15

v2.1.6 2012 November 6

v2.1.5 2012 September 29

  • Fixed completion callback not firing when trying to watch a path that doesn't exist

v2.1.4 2012 September 27

  • Fixed new listeners not being added for directories that have already been watched
  • Fixed completion callbacks happening too soon
  • Thanks to pull request #14 by Casey Foster

v2.1.3 2012 August 10

  • Re-added markdown files to npm distribution as they are required for the npm website

v2.1.2 2012 July 7

  • Fixed spelling of persistent
  • Explicitly set the defaults for the options ignoreHiddenFiles and ignorePatterns

v2.1.1 2012 July 7

  • Added support for interval and persistant options
  • Improved unlink detection
  • Optimised unlink handling

v2.1.0 2012 June 22

  • watchr.watchr changes - now only accepts one argument which is an object - added new paths property which is an array of multiple paths to watch - will only watch paths that actually exist (before it use to throw an error)
  • Fixed a few bugs
  • Added support for node v0.7/v0.8
  • Moved tests from Mocha to Joe

v2.0.3 2012 April 19

  • Fixed a bug with closing watchers
  • Now requires pre-compiled code

v2.0.0 2012 April 19

  • Big rewrite
  • Got rid of the delay
  • Now always fires events
  • Watcher instsances inherit from Node's EventEmitter
  • Events for change, unlink and new

v1.0.0 2012 February 11

  • Better support for ignoring hidden files
  • Improved documentation, readme
  • Added History.md file
  • Added unit tests using Mocha

v0.1.0 2012 November 13

  • Initial working version