Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/damonoehlman/plug
Simple Node.js plugin helper
https://github.com/damonoehlman/plug
Last synced: 7 days ago
JSON representation
Simple Node.js plugin helper
- Host: GitHub
- URL: https://github.com/damonoehlman/plug
- Owner: DamonOehlman
- Created: 2011-12-06T00:23:19.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2013-10-07T22:07:28.000Z (about 11 years ago)
- Last Synced: 2024-10-25T20:46:16.847Z (21 days ago)
- Language: JavaScript
- Homepage:
- Size: 109 KB
- Stars: 7
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# plug - lightweight and simple plugin system
Plug is a very simple plugin system for Node.js. It has minimal dependencies and is should make loading plugins a very simple affair.
## Design Principles
- Plugins are node modules that export a `connect` and `drop` function.
- Each `Plugger` manages a list of active plugins, which are unique by name.
- In the event that a plugin with the same name as an existing plugin is loaded into a Plugger scope, the old plugin is __dropped__ before the new plugin is __connected__.## Installing
The simplest way to install plug is via npm:
```
npm install plug
```## Plugin Connection
When a new plugin is found, the `connect` function for the plugin is called with arguments that were passed when a new `Plugger` instance was created. This sounds a little confusing at first, but makes plug quite powerful.
In the following example, for instance, a Plugger is created taking a name and age argument:
`examples/simple-loader.js`:
```js
var path = require('path'),
plugger = require('plug').create('Bob', 36);// handle plugin connection
plugger.on('connect', function(pluginName, pluginData, modulePath) {
console.log('loaded plugin "' + pluginName + '", with data: ', pluginData);
});plugger.find(path.resolve(__dirname, 'plugins/a'));
```When plugins are later connected, these arguments are passed through to the plugin's connect function along with a callback. The callback is responsible for returning _pluginData_ to the plugger, and all of this information is passed through when a `connect` event is emitted:
`plugins/a/first.js`:
```
exports.connect = function(name, age, callback) {
console.log('I belong to ' + name);
callback({
sport: 'Fishing'
});
});
```Running, the above example yields the following output:
```
I belong to Bob
loaded plugin "first", with data: { sport: 'Fishing' }
```## Plugin Drop (or Disconnection)
To be completed.
### Using Drop Actions
To be completed.
## Other Node Plugin Systems
- [haba](https://github.com/crcn/haba)
- [broadway](https://github.com/flatiron/broadway)