node-minibase/minibase-create-plugin

refactor

Closed this issue · 1 comments

'use strict'

var betterUse = require('dush-better-use')
var mixinDeep = require('mixin-deep')

function minibaseCreatePlugin (name, fn) {
  return function _generatedPlugin (options) {
    return function _generatedPlugin (app) {
      app.use(betterUse())
      app.use(name, fn, options)
      return app
    }
  }
}

for example such plugin

module.exports = minibaseCreatePlugin('foo-bar-baz', function (app) {
  // code
})

will be use as

var fooBarBaz = require('foo-bar-baz')

app.use(fooBarBaz())

In short minibaseCreatePlugin just exposes a function that accepts optional options object, then when it is called it returns a plugin function that should be passed to .use method.

In bonus, we got 4 things:

  • automatically each plugin will be named
  • each plugin has good error handling, by emitting error event if it fails
  • automatic merging of plugin options to app.options if given
  • each plugin will be called only once, guarded by isRegistered