From 4caed503bf1c3465bfc449389a46080d5c80de8d Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sun, 17 Jan 2016 00:06:28 +0200 Subject: [PATCH] extensions moved to separate modules --- lib/extensions/decorate.js | 26 -------- lib/extensions/expose.js | 20 ------ lib/server.js | 4 +- package.json | 2 + test/extensions/decorate.js | 121 ------------------------------------ test/extensions/expose.js | 114 --------------------------------- 6 files changed, 4 insertions(+), 283 deletions(-) delete mode 100644 lib/extensions/decorate.js delete mode 100644 lib/extensions/expose.js delete mode 100644 test/extensions/decorate.js delete mode 100644 test/extensions/expose.js diff --git a/lib/extensions/decorate.js b/lib/extensions/decorate.js deleted file mode 100644 index e394aa7..0000000 --- a/lib/extensions/decorate.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict' -const merge = require('merge') - -module.exports = function(remi, opts) { - let extension = { - decorate(type, prop, method) { - if (type !== 'server') - throw new Error('Only "server" type is supported') - - if (typeof prop === 'string') { - extension[prop] = method - } else if (typeof prop === 'object') { - merge(extension, prop) - } else { - throw new Error('invalid arguments passed to decorate') - } - - merge(this, extension) - merge(this.root, extension) - }, - } - - remi.pre('createPlugin', function(next, target, plugin) { - next(merge(target, extension), plugin) - }) -} diff --git a/lib/extensions/expose.js b/lib/extensions/expose.js deleted file mode 100644 index 626670e..0000000 --- a/lib/extensions/expose.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict' -const merge = require('merge') - -module.exports = function(remi, opts) { - remi.pre('createPlugin', function(next, target, plugin) { - target.plugins = target.plugins || {} - target.root.plugins = target.plugins - target.plugins[plugin.name] = target.plugins[plugin.name] || {} - - next(merge(target, { - expose(key, value) { - if (typeof key === 'string') { - target.plugins[plugin.name][key] = value - return - } - target.plugins[plugin.name] = merge(target.plugins[plugin.name], key) - }, - }), plugin) - }) -} diff --git a/lib/server.js b/lib/server.js index cf5a780..f28ef1e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -11,8 +11,8 @@ function Server() { this.methods = {} this._remi = new Remi({ extensions: [ - require('./extensions/decorate'), - require('./extensions/expose'), + require('remi-decorate'), + require('remi-expose'), ], }) } diff --git a/package.json b/package.json index 81d01bc..c99997f 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,8 @@ "joi": "^7.1.0", "merge": "^1.2.0", "remi": "^0.2.1", + "remi-decorate": "0.0.0", + "remi-expose": "0.0.0", "run-async": "^2.1.0", "thenify": "^3.1.1", "uva-amqp": "^0.5.1" diff --git a/test/extensions/decorate.js b/test/extensions/decorate.js deleted file mode 100644 index f724059..0000000 --- a/test/extensions/decorate.js +++ /dev/null @@ -1,121 +0,0 @@ -'use strict' -const chai = require('chai') -const expect = chai.expect -const sinon = require('sinon') -const sinonChai = require('sinon-chai') -chai.use(sinonChai) - -const Remi = require('remi') -const decorate = require('../../lib/extensions/decorate') - -describe('decorate', function() { - it('should decorate with a single property', function(done) { - function plugin1(app, options, next) { - app.decorate('server', 'foo', 1) - expect(app.foo).to.eq(1) - expect(app.root.foo).to.eq(1) - return next() - } - plugin1.attributes = { - name: 'plugin1', - version: '0.0.0', - } - function plugin2(app, options, next) { - expect(app.foo).to.eq(1) - expect(app.root.foo).to.eq(1) - return next() - } - plugin2.attributes = { - name: 'plugin2', - version: '0.1.0', - } - - let app = {} - let plugins = [{ - register: plugin1, - options: {foo: 1}, - }, { - register: plugin2, - },] - new Remi({ - extensions: [decorate], - }).register(app, plugins, function(err) { - expect(err).to.not.exist - expect(app.foo).to.eq(1) - done() - }) - }) - - it('should decorate with with multiple properties', function(done) { - function plugin1(app, options, next) { - app.decorate('server', { - foo: 1, - }) - expect(app.foo).to.eq(1) - expect(app.root.foo).to.eq(1) - return next() - } - plugin1.attributes = { - name: 'plugin1', - version: '0.0.0', - } - function plugin2(app, options, next) { - expect(app.foo).to.eq(1) - expect(app.root.foo).to.eq(1) - return next() - } - plugin2.attributes = { - name: 'plugin2', - version: '0.1.0', - } - - let app = {} - let plugins = [{ - register: plugin1, - options: {foo: 1}, - }, { - register: plugin2, - },] - new Remi({ - extensions: [decorate], - }).register(app, plugins, function(err) { - expect(err).to.not.exist - expect(app.foo).to.eq(1) - done() - }) - }) - - it('should share the decorated elements through register invocations', function() { - function plugin(app, options, next) { - app.decorate('server', 'foo', 'bar') - return next() - } - plugin.attributes = { - name: 'foo-plugin', - version: '0.0.0', - } - - function noopPlugin(app, options, next) { - next() - } - noopPlugin.attributes = { - name: 'noop-plugin', - } - - let app = {} - let remi = new Remi({ - extensions: [decorate], - }) - - return remi - .register(app, plugin, {}) - .then(() => { - expect(app.foo).to.eq('bar') - - return remi.register(app, [noopPlugin], {}) - }) - .then(() => { - expect(app.foo).to.eq('bar') - }) - }) -}) diff --git a/test/extensions/expose.js b/test/extensions/expose.js deleted file mode 100644 index 4ba9698..0000000 --- a/test/extensions/expose.js +++ /dev/null @@ -1,114 +0,0 @@ -'use strict' -const chai = require('chai') -const expect = chai.expect -const sinon = require('sinon') -const sinonChai = require('sinon-chai') - -chai.use(sinonChai) - -const Remi = require('remi') -const expose = require('../../lib/extensions/expose') - -describe('plugin expose', function() { - it('should expose property', function(done) { - function plugin(app, options, next) { - app.expose('foo', 1) - return next() - } - plugin.attributes = { - name: 'plugin', - version: '0.0.0', - } - - let app = {} - new Remi({ - extensions: [expose], - }).register(app, plugin, function(err) { - expect(err).to.not.exist - expect(app.plugins.plugin.foo).to.eq(1) - - done() - }) - }) - - it('should expose object', function(done) { - function plugin(app, options, next) { - app.expose({ - foo: 1, - bar: 3, - }) - return next() - } - plugin.attributes = { - name: 'plugin', - version: '0.0.0', - } - - let app = {} - new Remi({ - extensions: [expose], - }).register(app, plugin, function(err) { - expect(err).to.not.exist - expect(app.plugins.plugin.foo).to.eq(1) - expect(app.plugins.plugin.bar).to.eq(3) - - done() - }) - }) - - it('should have a plugin namespace in plugins', function(done) { - function plugin(app, options, next) { - expect(app.plugins['foo-plugin']).to.be.not.undefined - return next() - } - plugin.attributes = { - name: 'foo-plugin', - version: '0.0.0', - } - - let app = {} - new Remi({ - extensions: [expose], - }).register(app, plugin, function(err) { - expect(err).to.not.exist - expect(app.plugins['foo-plugin']).to.be.not.undefined - - done() - }) - }) - - it('should share the plugin namespace through register invocations', function() { - function plugin(app, options, next) { - expect(app.plugins['foo-plugin']).to.be.not.undefined - return next() - } - plugin.attributes = { - name: 'foo-plugin', - version: '0.0.0', - } - - function noopPlugin(app, options, next) { - next() - } - noopPlugin.attributes = { - name: 'noop-plugin', - } - - let app = {} - let remi = new Remi({ - extensions: [expose], - }) - - return remi - .register(app, plugin, {}) - .then(() => { - expect(app.plugins['foo-plugin']).to.be.not.undefined - - return remi.register(app, [noopPlugin], {}) - }) - .then(() => { - expect(app.plugins).to.be.not.undefined - expect(app.plugins['foo-plugin']).to.be.not.undefined - }) - }) -})