plugged-in
A plugin system for node js.
Last updated 3 years ago by rawphp .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install plugged-in 
SYNC missed versions from official npm registry.

Plugged In

Build Status npm version Code Climate Test Coverage Issue Count

Plugin system for Node Js.

Installation

  npm install plugged-in

Usage

Let's imagine that you want to create an application where it can be extended via plugin modules. To accomplish this you need to have your application class extend the PluginManager class.

Application.js

  export default class Application extends PluginManager {
    constructor(config) {
      super(config);
    }
  }

Also, add the plugged-in configuration to your package.json and set your application context.

package.json

  "plugged-in": {
    "context": "plugged-in-test"
  },

That's it. You're now ready to have your plugin modules get plugged-in.

Now, for your plugins. Your plugin modules need to export and advertise their supported functionality.

package.json

  "plugged-in": {
    "context": "plugged-in-test",
    "provides": {
      "getTitle": "getTitle",
      "getBody": [
        "getBody",
        "removeLinks"
      ]
    }
  },

In this example, the plugin tells the PluginManager that it provides the getTitle and getBody features in the plugged-in-test context.

Only plugins that match the PluginManager context will be plugged in.

For getTitle it provides one event callback, the getTitle(event) callback and for get body, it provides two, getBody(event) and removeLinks(event).

These callbacks will be called in the order they are defined.

Once your application has extended PluginManager and defined the application context. You need to call the init() method to generate the .plugged-in.json config and load the plugins.

If the config file already exists, it will not be regenerated. The application will load the configured plugins.

  const app = new Application();
  await app.init();
  app. ...

Invoke Plugin Functionality

To invoke the plugin's functions, you need to dispatch an event from the PluginManager (or your Application).

  const context = { body: 'current body' };
  await this.dispatch('getBody', context);
  const body = context.body; // resulting value

To get a return value from an event execution, set the desired data on the context property of the event. This is what get's returned from the dispatch call.

More on Usage

See the examples directory for usage example. The examples show the manager project and one plugin project.

NOTE:

If you have plugins installed globally, you need to run the link command to make them available to the plugin manager. Running init() will find both local and global modules, but only local modules can be used during execution.

  npm link plugged-in-plugin

Add additional Plugins on init

When initialising the PluginManager, you can optionally pass your own handlers to the manager. If it matches an existing function name already registered it will replace it. This allows you to locally override plugin functionality. On the other hand, if you want to allow duplicated function names, pass override = false to the constructor in the options.

  const addInitialisedAt = (event) => {
    event.context.initialisedAt = new Date();
  };

  await app.init({
    postInit: addInitialisedAt,
  });

API

Actual api docs generated by esdoc and available at https://rawphp.github.io/plugged-in.

Current Tags

  • 1.1.3                                ...           latest (3 years ago)

34 Versions

  • 1.1.3                                ...           3 years ago
  • 1.1.2                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.4.5                                ...           3 years ago
  • 0.4.4                                ...           3 years ago
  • 0.4.3                                ...           3 years ago
  • 0.4.2                                ...           3 years ago
  • 0.4.1                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.2                                ...           3 years ago
  • 0.2.1                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.1.2                                ...           3 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
  • 0.0.13                                ...           3 years ago
  • 0.0.12                                ...           3 years ago
  • 0.0.11                                ...           3 years ago
  • 0.0.10                                ...           3 years ago
  • 0.0.9                                ...           3 years ago
  • 0.0.8                                ...           3 years ago
  • 0.0.7                                ...           3 years ago
  • 0.0.6                                ...           3 years ago
  • 0.0.5                                ...           3 years ago
  • 0.0.4                                ...           3 years ago
  • 0.0.3                                ...           3 years ago
  • 0.0.2                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (13)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |