@eggjs/watcher
file watcher plugin for egg
Last updated a year ago by GitHub Actions .
Repository · Bugs · Original npm · Tarball · package.json
$ npm install @eggjs/watcher 
SYNC missed versions from official npm registry.

@eggjs/watcher

NPM version Known Vulnerabilities npm download Node.js Version

File watcher plugin for egg

Usage

In worker process:

app.watcher.watch(path, listener)

Start watching file(s).

  • path(String|Array): file path(s)
  • listener(Function): file change callback

app.watcher.unwatch(path[, listener])

Stop watching file(s).

  • path(String|Array): file path(s)
  • listener(Function): file change callback

In agent process:

agent.watcher.watch(path, listener)

Start watching file(s).

  • path(String|Array): file path(s)
  • listener(Function): file change callback

agent.watcher.unwatch(path[, listener])

Stop watching file(s).

  • path(String|Array): file path(s)
  • listener(Function): file change callback

Watching mode

development Mode

There's a built-in development mode which works in local(env is local). Once files on disk is modified it will emit a change event immediately.

Customize Watching Mode

Say we want to build a custom event source plugin (package name: egg-watcher-custom, eggPlugin.name: watcherCustom).

Firstly define our custom event source like this:

// {plugin_root}/lib/custom_event_source.js
import { Base } from 'sdk-base';

export default class CustomEventSource extends Base {
  // `opts` comes from app.config[${eventSourceName}]
  // `eventSourceName` will be registered later in
  // `config.watcher.eventSources` as the key shown below
  constructor(opts) {
    super(opts);
    this.ready(true);
  }

  watch(path) {
    // replace this with your desired way of watching,
    // when aware of any change, emit a `change` event
    // with an info object containing `path` property
    // specifying the changed directory or file.
    this._h = setInterval(() => {
      this.emit('change', { path });
    }, 1000);
  }

  unwatch() {
    // replace this with your implementation
    if (this._h) {
      clearInterval(this._h);
    }
  }
}

Event 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.

Then add your custom event source to config:

// config/config.default.js
import CustomEventSource from '../lib/custom_event_source';

export default {
  watcher: {
    eventSources: {
      custom: CustomEventSource,
    },
  },
};

Choose to use your custom watching mode in your desired env.

// config/config.${env}.js

export default {
  watcher: {
    type: 'custom',
  },

  // this will pass to your CustomEventSource constructor as opts
  watcherCustom: {
    // foo: 'bar',
  },
};

If possible, plugins named like egg-watcher-${customName}(egg-watcher-vagrant eg.) are recommended.

Questions & Suggestions

Please open an issue here.

License

MIT

Contributors

Contributors

Made with contributors-img.

Current Tags

  • 5.0.2-beta.27                                ...           beta (2 months ago)
  • 5.1.0                                ...           latest (2 months ago)
  • 5.0.2-rc.1                                ...           rc (2 months ago)

64 Versions

  • 5.1.0                                ...           2 months ago
  • 5.0.2                                ...           2 months ago
  • 5.0.2-rc.1                                ...           2 months ago
  • 5.0.2-rc.0                                ...           2 months ago
  • 5.0.2-beta.27                                ...           2 months ago
  • 5.0.2-beta.26                                ...           4 months ago
  • 5.0.2-beta.25                                ...           4 months ago
  • 5.0.2-beta.24                                ...           4 months ago
  • 5.0.2-beta.23                                ...           5 months ago
  • 5.0.2-beta.22                                ...           5 months ago
  • 5.0.2-beta.20                                ...           5 months ago
  • 5.0.2-beta.19                                ...           5 months ago
  • 5.0.2-beta.17                                ...           5 months ago
  • 5.0.2-beta.16                                ...           5 months ago
  • 5.0.2-beta.15                                ...           5 months ago
  • 5.0.2-beta.14                                ...           5 months ago
  • 5.0.2-beta.13                                ...           5 months ago
  • 5.0.2-beta.12                                ...           5 months ago
  • 5.0.2-beta.11                                ...           5 months ago
  • 5.0.2-beta.10                                ...           5 months ago
  • 5.0.2-beta.9                                ...           8 months ago
  • 5.0.2-beta.8                                ...           8 months ago
  • 5.0.2-beta.7                                ...           8 months ago
  • 5.0.2-beta.6                                ...           8 months ago
  • 5.0.2-beta.5                                ...           8 months ago
  • 5.0.2-beta.4                                ...           8 months ago
  • 5.0.2-beta.3                                ...           9 months ago
  • 5.0.2-beta.2                                ...           9 months ago
  • 5.0.2-beta.1                                ...           9 months ago
  • 5.0.2-beta.0                                ...           9 months ago
  • 5.0.1-beta.0                                ...           9 months ago
  • 5.0.0-beta.36                                ...           a year ago
  • 5.0.0-beta.35                                ...           a year ago
  • 5.0.0-beta.34                                ...           a year ago
  • 5.0.0-beta.33                                ...           a year ago
  • 5.0.0-beta.32                                ...           a year ago
  • 5.0.0-beta.31                                ...           a year ago
  • 5.0.0-beta.30                                ...           a year ago
  • 5.0.0-beta.29                                ...           a year ago
  • 5.0.0-beta.28                                ...           a year ago
  • 5.0.0-beta.27                                ...           a year ago
  • 5.0.0-beta.26                                ...           a year ago
  • 5.0.0-beta.25                                ...           a year ago
  • 5.0.0-beta.24                                ...           a year ago
  • 5.0.0-beta.23                                ...           a year ago
  • 5.0.0-beta.22                                ...           a year ago
  • 5.0.0-beta.21                                ...           a year ago
  • 5.0.0-beta.20                                ...           a year ago
  • 5.0.0-beta.19                                ...           a year ago
  • 5.0.0-beta.18                                ...           a year ago
  • 5.0.0-beta.17                                ...           a year ago
  • 5.0.0-beta.15                                ...           a year ago
  • 5.0.0-beta.13                                ...           a year ago
  • 4.1.0-beta.11                                ...           a year ago
  • 4.1.0-beta.10                                ...           a year ago
  • 4.0.5-beta.3                                ...           a year ago
  • 4.0.5-beta.2                                ...           a year ago
  • 4.0.5-beta.1                                ...           a year ago
  • 4.0.4                                ...           2 years ago
  • 4.0.3                                ...           2 years ago
  • 4.0.2                                ...           2 years ago
  • 4.0.1                                ...           2 years ago
  • 4.0.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Downloads
Total 199
Today 0
This Week 2
This Month 76
Last Day 0
Last Week 67
Last Month 123
Dependencies (3)
Dev Dependencies (10)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE