{"description":"A wrapper and enhancements for fs.watch","license":"MIT","name":"node-watch","repository":{"url":"git://github.com/yuanchuan/node-watch.git","type":"git"},"keywords":["fs.watch","watch","watchfile"],"version":"0.7.0","bugs":{"url":"https://github.com/yuanchuan/node-watch/issues"},"url":"https://github.com/yuanchuan/node-watch","author":{"name":"yuanchuan","email":"yuanchuan23@gmail.com","url":"http://yuanchuan.name"},"main":"./lib/watch","types":"./lib/watch.d.ts","homepage":"https://github.com/yuanchuan/node-watch#readme","scripts":{"test":"mocha test/test.js --exit --slow 500"},"engines":{"node":">=6"},"devDependencies":{"fs-extra":"^7.0.1","mocha":"^5.2.0"},"gitHead":"5de15b1020fb5d2447a5cc44b72847f512ab6fc3","_id":"node-watch@0.7.0","_nodeVersion":"11.7.0","_npmVersion":"6.13.4","dist":{"integrity":"sha512-OOBiglke5SlRQT5WYfwXTmYqTfXjcTNBHpalyHLtLxDpQYVpVRkJqabcch1kmwJsjV/J4OZuzEafeb4soqtFZA==","shasum":"033c0c04239d9348f3402b6b6f9c1e689a7edbe1","tarball":"http://registry-npm.ywwl.com/node-watch/download/node-watch-0.7.0.tgz","fileCount":14,"unpackedSize":59783,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfe0BGCRA9TVsSAnZWagAAlUsP/jQjA/Mi+ayY7Fr8QFY7\nNQIc2q53i7JMqzGjbhY3a4//3ex0HKyTt5VknwuLhr88ymcvGazgYkPsfS6/\nbJyuo2QypRb2Ai7YHZKKAK6rkytWle0J3miFxOZfov0ifV39QowRNTW8SnEC\n1mEA8LXdsdK3KQXeS3+skWhdsg4vmZAov+XajSh5DuLgeR/ZK+ea/JoeRHli\nCJq0llqZMh6ahbjtNnywieQ0KqFNy2W4sT113lD1fQrQPtMJvG4H9xVTLG8l\niUEEovolr+qXKmtrpuWjdI1rl9lEaUaYWnYMalhOdwLzo1Vawmhe29uVuf89\n1Y0cViX+ryLTHv2Q6OSvzGg1KafoTTrCKiGbzAFkaCK6DlZCwT0WAUSPT9H+\n34vhISF/B+7KxEfU1RRWzX+e5dArbqCpRY+wVaaJwFJUnuZrBHg4m9GV6kbw\n3AaIgeoW9VEHvRet9y2hHJTbhb09c/mMsGUpfS+odT8qYz2rPWPUNisrJbTH\nYanBkTBjH2iNuEODV8wTGYbq4YH2664T1le+6PvxINZQBT84j6TMqLuZz3JP\nwR1gNaexXMu0RF0WD41MDxswACTPDR72SsH3ojmlqA0LO0sIrOxB6BM6YcYH\nkghEc6NQyjgpDpAZkRiQgLjaF5+1AAp9MrnV6k2f3PtiIzLuv3x1jz1vMQE1\nmlzu\r\n=oTQq\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD9cVf3JDpyKUXUIM19c2NqaqnITMBvxHHd5/7Df21dGwIgNAoNpTrb7s4+fapsUrVFGwxlA3xC1D7PZ57xpblfV6g="}],"size":15226,"noattachment":false,"key":"/node-watch/-/node-watch-0.7.0.tgz"},"maintainers":[{"name":"yuanchuan","email":"yuanchuan23@gmail.com"},{"name":"intervalia","email":"intervalia@gmail.com"}],"_npmUser":{"name":"yuanchuan","email":"yuanchuan23@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/node-watch_0.7.0_1601912901456_0.9131355753103296"},"_hasShrinkwrap":false,"readme":"# node-watch [![Status](https://github.com/yuanchuan/node-watch/actions/workflows/ci.yml/badge.svg)](https://github.com/yuanchuan/node-watch/actions/workflows/ci.yml/badge.svg)\n\nA wrapper and enhancements for [fs.watch](http://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener).\n\n[![NPM](https://nodei.co/npm/node-watch.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/node-watch.png/)\n\n\n## Installation\n\n```bash\nnpm install node-watch\n```\n\n## Example\n\n```js\nvar watch = require('node-watch');\n\nwatch('file_or_dir', { recursive: true }, function(evt, name) {\n  console.log('%s changed.', name);\n});\n```\n\nNow it's fast to watch **deep** directories on macOS and Windows, since the `recursive` option is natively supported except on Linux.\n\n```js\n// watch the whole disk\nwatch('/', { recursive: true }, console.log);\n```\n\n\n## Why?\n\n* Some editors will generate temporary files which will cause the callback function to be triggered multiple times.\n* The callback function will only be triggered once on watching a single file.\n* <del>Missing an option to watch a directory recursively.</del>\n* Recursive watch is not supported on Linux or in older versions of nodejs.\n* Keep it simple, stupid.\n\n\n## Options\n\nThe usage and options of `node-watch` are compatible with [fs.watch](https://nodejs.org/dist/latest-v7.x/docs/api/fs.html#fs_fs_watch_filename_options_listener).\n* `persistent: Boolean` (default **true**)\n* `recursive: Boolean` (default **false**)\n* `encoding: String` (default **'utf8'**)\n\n**Extra options**\n\n* `filter: RegExp | Function`\n\n   Return that matches the filter expression.\n\n    ```js\n    // filter with regular expression\n    watch('./', { filter: /\\.json$/ });\n\n    // filter with custom function\n    watch('./', { filter: f => !/node_modules/.test(f) });\n\n    ```\n\n   Each file and directory will be passed to the filter to determine whether\n   it will then be passed to the callback function. Like `Array.filter` does in `JavaScript`.\n   There are three kinds of return values for filter function:\n\n     * **`true`**: Will be passed to callback.\n     * **`false`**: Will not be passed to callback.\n     * **`skip`**: Same with `false`, and skip to watch all its subdirectories.\n\n   On Linux, where the `recursive` option is not natively supported,\n   it is more efficient to skip ignored directories by returning the `skip` flag:\n\n    ```js\n    watch('./', {\n      recursive: true,\n      filter(f, skip) {\n        // skip node_modules\n        if (/\\/node_modules/.test(f)) return skip;\n        // skip .git folder\n        if (/\\.git/.test(f)) return skip;\n        // only watch for js files\n        return /\\.js$/.test(f);\n      }\n    });\n\n    ```\n\n    If you prefer glob patterns you can use [minimatch](https://www.npmjs.com/package/minimatch) or [picomatch](https://www.npmjs.com/package/picomatch)\n    together with filter:\n\n     ```js\n     const pm = require('picomatch');\n     let isMatch = pm('*.js');\n\n     watch('./', {\n       filter: f => isMatch(f)\n     });\n     ```\n\n* `delay: Number` (in ms, default **200**)\n\n   Delay time of the callback function.\n\n    ```js\n    // log after 5 seconds\n    watch('./', { delay: 5000 }, console.log);\n    ```\n\n## Events\n\nThe events provided by the callback function is either `update` or `remove`, which is less confusing to `fs.watch`'s `rename` or `change`.\n\n```js\nwatch('./', function(evt, name) {\n\n  if (evt == 'update') {\n    // on create or modify\n  }\n\n  if (evt == 'remove') {\n    // on delete\n  }\n\n});\n```\n\n\n## Watcher object\n\nThe watch function returns a [fs.FSWatcher](https://nodejs.org/api/fs.html#fs_class_fs_fswatcher) like object as the same as `fs.watch` (>= v0.4.0).\n\n#### Watcher events\n\n```js\nlet watcher = watch('./', { recursive: true });\n\nwatcher.on('change', function(evt, name) {\n  // callback\n});\n\nwatcher.on('error', function(err) {\n  // handle error\n});\n\nwatcher.on('ready', function() {\n  // the watcher is ready to respond to changes\n});\n```\n\n#### Close\n\n```js\n// close\nwatcher.close();\n\n// is closed?\nwatcher.isClosed()\n```\n\n#### List of methods\n\n* `.on`\n* `.once`\n* `.emit`\n* `.close`\n* `.listeners`\n* `.setMaxListeners`\n* `.getMaxListeners`\n\n##### Extra methods\n* `.isClosed` detect if the watcher is closed\n* `.getWatchedPaths` get all the watched paths\n\n\n## Known issues\n\n**Windows, node < v4.2.5**\n\n  * Failed to detect `remove` event\n  * Failed to get deleted filename or directory name\n\n**MacOS, node 0.10.x**\n  * Will emit double event if the directory name is of one single character.\n\n\n## Misc\n\n#### 1. Watch multiple files or directories in one place\n```js\nwatch(['file1', 'file2'], console.log);\n```\n\n#### 2. Customize watch command line tool\n```js\n#!/usr/bin/env node\n\n// https://github.com/nodejs/node-v0.x-archive/issues/3211\nrequire('epipebomb')();\n\nlet watcher = require('node-watch')(\n  process.argv[2] || './', { recursive: true }, console.log\n);\n\nprocess.on('SIGINT', watcher.close);\n```\nMonitoring chrome from disk:\n```bash\n$ watch / | grep -i chrome\n```\n\n#### 3. Got ENOSPC error?\n\nIf you get ENOSPC error, but you actually have free disk space - it means that your OS watcher limit is too low and you probably want to recursively watch a big tree of files.\n\nFollow this description to increase the limit:\n[https://confluence.jetbrains.com/display/IDEADEV/Inotify+Watches+Limit](https://confluence.jetbrains.com/display/IDEADEV/Inotify+Watches+Limit)\n\n\n## Alternatives\n\n* [chokidar](https://github.com/paulmillr/chokidar)\n* [gaze](https://github.com/shama/gaze)\n* [mikeal/watch](https://github.com/mikeal/watch)\n\n## Contributors\n\nThanks goes to [all wonderful people](https://github.com/yuanchuan/node-watch/graphs/contributors) who have helped this project.\n\n## License\nMIT\n\nCopyright (c) 2012-2021 [yuanchuan](https://github.com/yuanchuan)\n","publish_time":1601912901637,"_cnpm_publish_time":1601912901637,"dist-tags":{"latest":"0.7.4"}}