P
P
Philipp2017-01-10 09:08:23
JavaScript
Philipp, 2017-01-10 09:08:23

How to create a plugin for Node.JS?

The idea is pretty simple. Make an interface for plugins, which would be NPM packages.
For example, I can declare something like this inside package.json:

{
  plugins: ['fire', 'strike', 'ymz']
}

In turn I have 3 npm: myproject-plugin-fire, myproject-plugin-strike, myproject-plugin-ymz.
They will be automatically installed and initiated within the application.
I was thinking of making plugins in style
function callback(eventData) {
  // тут плагин что-то делает с данными в eventData
  return eventData;
}
module.exports = function () {
  return {eventName: 'preRender', callback: callback};
}

In this case, it seems to me that it will be easy to register them, plus you can make call chains. The list of available event names can be declared somewhere or even some kind of microrouting, like 'pre*'.
In general, it's not entirely clear to me how to do automatic npm installation and their registration.
It might even be better to just do it through dependencies and search by prefix, but I don't really like the idea of ​​editing package.json. Ideally, I would like to manage all this through some kind of visual interface later.
But I can do this. At the moment, you need something like npm list, npm installand a program call require().
Theoretically, I can make system()calls, but somehow it looks inorganic, plus I need to somehow update the plugins.
I've looked at https://github.com/npm/npm-registry-client and https://github.com/maxleiko/npmi/ Looks promising so far.
About registration while anything except EventEmitter'a did not come to mind. Maybe you can advise something else.
How to install npm packages and declare them in an already running application?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
index0h, 2017-01-10
@index0h

You want what you really don't need)) For post-installation and similar things, https://docs.npmjs.com/misc/scripts
was invented long ago As for setting up plugins, this is the task of an engineer, not a package management system.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question