0.3.1 • Published 5 years ago

more-or-less-simple-plugin-system v0.3.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

more-or-less-simple-plugin-system

A tiny plugin system based on node-modules for adding a bit of modularity in nodejs applications. (Using only one dependency! 😎)

Only use this when you have time to fiddle around. (It's very stupid and -in some parts- lacks error handling)

Example 📄

Code 💻

const {SingleProcessPluginLoader} = require("more-or-less-simple-plugin-system");

// with this object you can set the needed functions/values in your plugin
const scheme = { 
  name: "string",   // String name
  exec: "function", // Function exec

  // when one of those is not given will throw error as seen below
};

var pl = new SingleProcessPluginLoader(__dirname + "/node_modules", scheme);

var res = await pl.reload();
var {ok, failed} = pl.sortPlugins(res); // ok are working, failed are not

for (var p of ok) {
  p.instance // (<- your plugin) this is how you can access the plugin itself
}

for (var p of failed) {
  p.errors // (<- is an array) the errors are written out in human readable strings
}

Walkthrough 🏃‍

  1. creating your plugin scheme. in this case we assume that we need a string name and a function named exec.
  2. after that, we instantiate our PluginLoader with the plugin root folder. Here we are using the node_modules folder (as that works great). And hand over our handcrafted PluginScheme.
  3. after that we call the async function reload on our PluginLoader instance and store the returned values in an variable.
  4. now we let the PluginLoader sort our plugins by working and failed
  5. after that we are ready to use them! 👍

Important Note 🚨

PluginLoader will search in the root folder you give it. It needs a folder structure like this:

  • \<your root folder>
    • \<your plugin's folder>
      • package.json

The loader will read the package.json for the following values: 1. name 2. main <- just like a normal node module 3. tx_plugin <- has to be true (if you want it to be loaded)

"Wow cool! Where can I see more?"

You could look at the "./example" folder. There you find one example for singleprocess use and one for multiprocess use! Keep in mind that in multiprocess you cannot access variables directly and functions have to be called asynchronously --even when you didn't declare them that way!

Older, simpler version

The branch "first-version-no-packagejson" is a much simpler and more stupid version of this system. But I recommend using the new one... due to more functionality and support!

Missing:

  • extensive documentation
  • author's experience in terms of open sourcing stuff

PS. this is the first tiny helper-project I publish ever (please be nice 😅 and share feedback)

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago