Git Product home page Git Product logo

plugin-discovery's Introduction

plugin-discovery

Discover plugins for your (CLI) tool.

This module will scan locations on the file system in an optimal manner, in search of your plugins.

Installation

npm i --save plugin-discovery

Usage

This plugin can be used in instance form, or statically, both async (recommended) and sync. There's an example in the example directory of this project.

const {PluginDiscovery} = require('plugin-discovery');

// Sync
let plugins = PluginDiscovery.discoverSync(config);

// Async
PluginDiscovery.discover(discoveryConfig).then(plugins => {
  // Go nuts! :)
})

Config

These options are available to configure.

const configOptions = {

  // Prefix of your plugins, e.g. my-prefix-foo
  prefix: 'my-prefix',

  // Strategy to use for the plugin's key. Read on for the options.
  dictionaryKeyStrategy: PluginDiscovery.constants.STRATEGY_PROPERTY,

  // Name of the property for strategies PROPERTY and METHOD.
  dictionaryKeyProperty: 'name',

  // Additional paths to look for plugins
  searchPaths: [__dirname + '/custom_path'],

  // Discover plugins in the global node_modules directory? (useful for CLI tools!)
  discoverGlobal: true,

  // Discover plugins in the current working directory?
  discoverCwd: true,

  // Import plugin based on named export? (e.g. module.exports.MyPlugin)
  importNamed: 'MyPlugin',

  // Import plugin based on named fallback export when not found after regular checks?
  namedFallback: 'MyPluginFallback',

  // Custom configurers. The key is used to allow for multiple configurers. 
  configurers: {
    myPlugin: (key, plugin, rootImport) => {
      // Configure for your own project here...
    }
  },

  // Internal method used to configure. Only override when you really know what you're doing.
  configure: (dictionaryKey, imported, root) => this.configure(dictionaryKey, imported, root)
};

Strategies

There are a couple of strategies available for determining what the key of your plugin will be in the produced plugin-dictionary.

All constants are available through: PluginDiscovery.constants.${constant}

STRATEGY_MODULE_NAME

Using this strategy, the key will simply be the entire module name, including the prefix.

STRATEGY_STRIP_PREFIX

Using this strategy, the key will simply be the entire module name, minus the prefix.

STRATEGY_PROPERTY

This strategy allows you to export the key of the plugin, in the plugin.

module.exports = {
  name  : 'my_plugin',
  plugin: MyPlugin
};

Note: this option requires you to also configure the dictionaryKeyProperty.

STRATEGY_METHOD

This strategy allows you to export the key of the plugin, using a method on the plugin export.

module.exports = {
  name  : () => 'my_plugin',
  plugin: MyPlugin
};

Note: this option requires you to also configure the dictionaryKeyProperty.

STRATEGY_CUSTOM

This strategy means that PluginDiscovery won't index your plugins at all. Instead, you are expected to do so yourself in the configure phase.

License

MIT

plugin-discovery's People

Contributors

rwoverdijk avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.