{"name":"node-watch","version":"0.2.1","description":"NodeJS file.watch wrapper","url":"https://github.com/yuanchuan/node-watch","author":{"name":"yuanchuan","email":"yuanchuan23@gmail.com","url":"http://yuanchuan.name"},"main":"./lib/watch.js","keywords":["nodewatch","watch","watchfile"],"repository":{"type":"git","url":"git://github.com/yuanchuan/node-watch.git"},"_npmUser":{"name":"yuanchuan","email":"yuanchuan23@gmail.com"},"_id":"node-watch@0.2.1","dependencies":{},"devDependencies":{},"optionalDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.1.24","_nodeVersion":"v0.8.1","_defaultsLoaded":true,"dist":{"shasum":"5095767cb97ce2c3c6ce1824f1d30bbb3fd0af05","tarball":"http://registry-npm.ywwl.com/node-watch/download/node-watch-0.2.1.tgz","integrity":"sha512-BOQs6Pqt1SDKxZe1iQy2rh+8qdaZVRBLemv9dufaR8UZZF9jJa/wGBTGxmTBBRF/p6sp860dEMwEg6J/DqYoSQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDWmh9pkOIVq9Pcih8NHSPupJTQ1xIusnpzDWhay1+hggIgPnLadB87wxS/7YlEmxYOgtlMitFkjjKSqQAx9kYsnSs="}],"size":2574,"noattachment":false,"key":"/node-watch/-/node-watch-0.2.1.tgz"},"maintainers":[{"name":"yuanchuan","email":"yuanchuan23@gmail.com"}],"directories":{},"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":1341384995228,"_cnpm_publish_time":1341384995228,"dist-tags":{"latest":"0.7.4"}}