Permalink
Browse files

fix(compat): make it work for apps based on Base or Base+base-plugins

it just works and uses their .use method ;]

fixes #3
  • Loading branch information...
tunnckoCore committed Apr 3, 2017
1 parent d38561c commit b630cf4ce3e14d3882a6f33a1e601091e449e5aa
Showing with 412 additions and 177 deletions.
  1. +23 −3 index.js
  2. +3 −1 package.json
  3. +31 −0 test.js
  4. +355 −173 yarn.lock
@@ -57,9 +57,29 @@ var betterUse = require('dush-better-use')

module.exports = function minibaseCreatePlugin (name, fn) {
return function _generatedPlugin (options) {
return function _generatedPlugin (app, opts) {
app.use(betterUse())
app.use(name, fn, mixinDeep({}, options, opts))
return function _generatedPlugin (app, opts, pluginOptionsOfBaseApps) {
// when your app is based on Base,
// plugin options are passed as 3rd argument
if (app.isBase) {
opts = pluginOptionsOfBaseApps
}

// when `base-plugins` is registered
// we don't need to add `dush-better-use`
if (!app.isBase || (app.isRegistered && !app.isRegistered('base-plugins'))) {
app.use(betterUse())
}

// merge options passed to .use method
// and the options passed to the plugin function
// e.g. app.use('foobar', plugin({ foo: 1 }), { bar: 2 })
opts = mixinDeep({}, options, opts)

// partial fix
// @see https://github.com/node-base/base-plugins/issues/5
fn = app.isBase && !fn ? opts : fn

app.use(name, fn, opts)
return app
}
}
@@ -20,10 +20,12 @@
"commit": "npm-run-all -s test git"
},
"dependencies": {
"dush-better-use": "^1.1.0",
"dush-better-use": "^1.1.1",
"mixin-deep": "^1.2.0"
},
"devDependencies": {
"base": "^0.13.0",
"base-plugins": "^1.0.0",
"commitizen": "~2.7.0",
"cz-conventional-changelog": "1.1.5",
"dush": "^3.0.4",
31 test.js
@@ -12,6 +12,9 @@
var test = require('mukla')
var createPlugin = require('./index')

var plugins = require('base-plugins')
var Base = require('base')

var dush = require('dush')
var app = dush()

@@ -69,3 +72,31 @@ test('should not merge plugin options into app.options', function (done) {
test.strictEqual(app.registered.zzz, true)
done()
})

test('should work for raw Base apps, which not use base-plugins', function (done) {
var plugin = createPlugin(function somePlugin (app, base, options, env) {
test.strictEqual(arguments.length, 4)
test.strictEqual(options.abc, 'xyz')
done()
})

var base = new Base()
base.on('error', done)
base.use(plugin({ abc: 'xyz' }))
done()
})

test('should work for Base apps that uses base-plugins', function (done) {
var base = new Base()
base.use(plugins())

var foobarPlugin = createPlugin(function (app, base, options) {
test.deepEqual(options, {
xxx: 'vbz',
baz: 12345
})
done()
})

base.use(foobarPlugin({ xxx: 'vbz' }), { baz: 12345 })
})
Oops, something went wrong.

0 comments on commit b630cf4

Please sign in to comment.