{"id":13487473,"url":"https://github.com/eggjs/watcher","last_synced_at":"2025-10-14T14:17:33.648Z","repository":{"id":57220898,"uuid":"63418050","full_name":"eggjs/watcher","owner":"eggjs","description":"Watcher plugin for egg","archived":false,"fork":false,"pushed_at":"2025-01-11T02:07:11.000Z","size":82,"stargazers_count":27,"open_issues_count":0,"forks_count":3,"subscribers_count":9,"default_branch":"3.x","last_synced_at":"2025-09-29T01:07:44.665Z","etag":null,"topics":["egg","egg-plugin","egg-watcher"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eggjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-07-15T11:49:04.000Z","updated_at":"2025-04-22T07:42:26.000Z","dependencies_parsed_at":"2024-12-20T02:14:09.030Z","dependency_job_id":"c5e020d6-453c-4195-87bc-f53af9a792ea","html_url":"https://github.com/eggjs/watcher","commit_stats":null,"previous_names":["eggjs/watcher"],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/eggjs/watcher","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fwatcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fwatcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fwatcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fwatcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eggjs","download_url":"https://codeload.github.com/eggjs/watcher/tar.gz/refs/heads/3.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fwatcher/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279019175,"owners_count":26086576,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["egg","egg-plugin","egg-watcher"],"created_at":"2024-07-31T18:00:59.806Z","updated_at":"2025-10-14T14:17:33.642Z","avatar_url":"https://github.com/eggjs.png","language":"TypeScript","readme":"# egg-watcher\nFile watcher plugin for egg\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/egg-watcher.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/egg-watcher\n[travis-image]: https://img.shields.io/travis/eggjs/egg-watcher.svg?style=flat-square\n[travis-url]: https://travis-ci.org/eggjs/egg-watcher\n[codecov-image]: https://codecov.io/github/eggjs/egg-watcher/coverage.svg?branch=master\n[codecov-url]: https://codecov.io/github/eggjs/egg-watcher?branch=master\n[david-image]: https://img.shields.io/david/eggjs/egg-watcher.svg?style=flat-square\n[david-url]: https://david-dm.org/eggjs/egg-watcher\n[snyk-image]: https://snyk.io/test/npm/egg-watcher/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/egg-watcher\n[download-image]: https://img.shields.io/npm/dm/egg-watcher.svg?style=flat-square\n[download-url]: https://npmjs.org/package/egg-watcher\n\n## Usage\n\nIn worker process:\n\n### app.watcher.watch(path, listener)\nStart watching file(s).\n\n- path(String|Array): file path(s)\n- listener(Function): file change callback\n\n### app.watcher.unwatch(path[, listener])\nStop watching file(s).\n\n- path(String|Array): file path(s)\n- listener(Function): file change callback\n\nIn agent process:\n\n### agent.watcher.watch(path, listener)\nStart watching file(s).\n\n- path(String|Array): file path(s)\n- listener(Function): file change callback\n\n### agent.watcher.unwatch(path[, listener])\nStop watching file(s).\n\n- path(String|Array): file path(s)\n- listener(Function): file change callback\n\n## Watching mode\n\n### `development` Mode\n\nThere's a built-in [development mode](https://github.com/eggjs/egg-watcher/blob/master/lib/event-sources/development.js) which works in local(env is `local`). Once files on disk is modified it will emit a `change` event immediately.\n\n### Customize Watching Mode\n\nSay we want to build a custom event source plugin (package name: `egg-watcher-custom`, eggPlugin.name: `watcherCustom`).\n\nFirstly define our custom event source like this:\n\n```js\n// {plugin_root}/lib/custom_event_source.js\nconst Base = require('sdk-base');\n\nclass CustomEventSource extends Base {\n  // `opts` comes from app.config[${eventSourceName}]\n  // `eventSourceName` will be registered later in\n  // `config.watcher.eventSources` as the key shown below\n  constructor(opts) {\n    super(opts);\n    this.ready(true);\n  }\n\n  watch(path) {\n    // replace this with your desired way of watching,\n    // when aware of any change, emit a `change` event\n    // with an info object containing `path` property\n    // specifying the changed directory or file.\n    this._h = setInterval(() =\u003e {\n      this.emit('change', { path });\n    }, 1000);\n  }\n\n  unwatch() {\n    // replace this with your implementation\n    if (this._h) {\n      clearInterval(this._h);\n    }\n  }\n}\n\nmodule.exports = CustomEventSource;\n```\n\nEvent source implementations varies according to your running environment. When working with vagrant, docker, samba or such other non-standard way of development, you should use a different watch API specific to what you are working with.\n\nThen add your custom event source to config:\n\n```js\n// config.default.js\nexports.watcher = {\n  eventSources: {\n    custom: require('../lib/custom_event_source'),\n  },\n};\n```\n\nChoose to use your custom watching mode in your desired env.\n\n```js\n// config.${env}.js\nexports.watcher = {\n  type: 'custom',\n};\n\n// this will pass to your CustomEventSource constructor as opts\nexports.watcherCustom = {\n  // foo: 'bar',\n};\n```\n\nIf possible, plugins named like `egg-watcher-${customName}`(`egg-watcher-vagrant` eg.) are recommended.\n\n## Questions \u0026 Suggestions\n\nPlease open an issue [here](https://github.com/eggjs/egg/issues).\n\n## License\n\n[MIT](https://github.com/eggjs/egg-watcher/blob/master/LICENSE)\n","funding_links":[],"categories":["仓库"],"sub_categories":["[内置插件](https://eggjs.org/zh-cn/basics/plugin.html#%E6%8F%92%E4%BB%B6%E5%88%97%E8%A1%A8)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feggjs%2Fwatcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feggjs%2Fwatcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feggjs%2Fwatcher/lists"}