npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

superplug

v1.1.1

Published

A generic purpose plugin loading system to be used in node project. Leverages npm and package.json to easily set up a project with plugin support.

Downloads

20

Readme

A generic purpose plugin loading system to be used in node projects. It uses npm modules and annotations in package.json to easily set up a project with plugin support. It is fully Promise-based.

Usage

SuperPlug is built to load npm modules as extensions from a package.json file, in a folder separate from the project it is used in.

Setup

First add SuperPlug as a dependency to the node project you want to add plugin support to.

npm install --save superplug

Secondly determine the location from which you to load the dependencies from. This must be an absolute path. The path must be absolute because the require() function in the SuperPlug module has a different relative path than the project it is included in.

You can use a separate folder outside your project to hold your plugins. Create a package.json file in the folder, and install the desired plugins via npm install. The other option is to install the plugins into the project itself, then the location to use is __dirname, which is the absolute path to the script initializing SuperPlug. Update the path to have it point to the folder the projects' package.json is in.

In the code where the plugins are to be initialized, use the SuperPlug class:

  const { SuperPlug } = require('superplug')
  const superPlug = new SuperPlug({location: __dirname}) // See configuration section for more details on configuration.

  const foundPlugins = await superPlug.getPlugins();
  //do something with the returned plugins
  foundPlugins.forEach((plugin => {
    // In this example a plugin is expected to return an object that exposes a method called start.
    const pluginModule = await plugin.getPlugin();
    pluginModule.start()
  })

In the example above, foundPlugins is an array with Plugin classes. Each Plugin class has the following attributes/methods:

  1. name - The name of the plugin, the name in the superplug package attribute, or the module name if that is not defined.
  2. location - The path that points to the folder where the package.json of that plugin was read.
  3. rawProperty - Get the object with the plugin metadata as defined in the plugins' package.json.
  4. getPlugin() - Returns a Promise which will resolve to the plugins module main file as defined in package.json. If there is no main attribute defined in the package.json, this function will throw a NoPluginMainDefinedError when invoked.
  5. mainAvailable - is true when the package.json defines a main attribute. Can be false if this is not the case, and requirePackageMain is set to false in the configuration.

Plugins

A plugin must define an attribute in its package.json that will indicate to SuperPlug that this is a plugin that must be loaded, the default name is superPlug. The name of the attribute to search for is defined in the configuration. You are advised to select a unique property name specific to your project, so that it is not possible to inadvertently include plugins from other projects.

The script file referenced in the package.json main attribute of the module is returned by the getPlugin() function. You are responsible for defining a format or standard for the implementation that the plugin should return: an object, functions, properties, etc.

Example

{
  "name": "superplug-example-plugin",
  "version": "1.0.0",
  "description": "An example plugin.",
  "main": "index.js",
  "superPlug": {
    "name": "superplug-plugin",
    "foo": "bar"
  }
}

Configuration

The SuperPlug constructor expects an options object argument.

| Property | Required | Description | Default Value | | ------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | | location | Yes | The path to look for the package.json file. This must be an absolute path. | | | packageProperty | No | The name of the property to search for in the package.json file. | superplug | | requirePackageMain | No | Set to false to allow packages that do not have a "main" attribute in the package.json defined to be returned as a plugin. The default value is true |

Roadmap

The following items are on the roadmap to be added:

  1. Definition of implemented API version by the plugin, to be able to check if a plugin is outdated.
  2. Validator function in the configuration to check if the implementation of the plugin is correct.
  3. Examples of functions to install plugins, using npm install.

Contributing

Contributions are welcome. This does not necessarily have to be code, it can also be updated documentation, tutorials, bug reports or pull requests.

Please create an Issue to propose a feature you want to implement, so that the details can be discussed in advance.