From 4c8e3421f8a792890bd66efa91c1758693e29e05 Mon Sep 17 00:00:00 2001 From: Doug Moscrop Date: Tue, 29 Nov 2016 10:18:00 -0500 Subject: [PATCH] add Expose plugin hooks so that I can visualize them with an external plugin --- lib/classes/PluginManager.js | 16 ++++++++++++---- lib/classes/PluginManager.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/classes/PluginManager.js b/lib/classes/PluginManager.js index 4eda68d8f..22430b664 100644 --- a/lib/classes/PluginManager.js +++ b/lib/classes/PluginManager.js @@ -107,9 +107,13 @@ class PluginManager { } loadHooks(pluginInstance) { - _.forEach(pluginInstance.hooks, (hook, event) => { + const pluginName = pluginInstance.constructor.name; + _.forEach(pluginInstance.hooks, (fn, event) => { this.hooks[event] = this.hooks[event] || []; - this.hooks[event].push(hook); + this.hooks[event].push({ + pluginName, + fn, + }); }); } @@ -143,6 +147,10 @@ class PluginManager { return this.plugins; } + getHooks(events) { + return _.flatMap([].concat(events), (event) => this.hooks[event] || []); + } + run(commandsArray) { const command = this.getCommand(commandsArray); @@ -150,14 +158,14 @@ class PluginManager { this.validateOptions(command); const events = this.getEvents(command); - const hooks = _.flatMap(events, (event) => this.hooks[event] || []); + const hooks = this.getHooks(events); if (hooks.length === 0) { const errorMessage = 'The command you entered did not catch on any hooks'; throw new this.serverless.classes.Error(errorMessage); } - return BbPromise.reduce(hooks, (__, hook) => hook(), null); + return BbPromise.reduce(hooks, (__, hook) => hook.fn(), null); } validateCommand(commandsArray) { diff --git a/lib/classes/PluginManager.test.js b/lib/classes/PluginManager.test.js index b4155d306..a886e7c27 100644 --- a/lib/classes/PluginManager.test.js +++ b/lib/classes/PluginManager.test.js @@ -504,6 +504,30 @@ describe('PluginManager', () => { }); }); + describe('#getHooks()', () => { + beforeEach(function () { // eslint-disable-line prefer-arrow-callback + pluginManager.addPlugin(SynchronousPluginMock); + }); + + it('should get hooks for an event with some registered', () => { + expect(pluginManager.getHooks(['deploy:functions'])).to.be.an('Array').with.length(1); + }); + + it('should have the plugin name and function on the hook', () => { + const hooks = pluginManager.getHooks(['deploy:functions']); + expect(hooks[0].pluginName).to.equal('SynchronousPluginMock'); + expect(hooks[0].fn).to.be.a('Function'); + }); + + it('should not get hooks for an event that does not have any', () => { + expect(pluginManager.getHooks(['deploy:resources'])).to.be.an('Array').with.length(0); + }); + + it('should accept a single event in place of an array', () => { + expect(pluginManager.getHooks('deploy:functions')).to.be.an('Array').with.length(1); + }); + }); + describe('#getPlugins()', () => { beforeEach(function () { // eslint-disable-line prefer-arrow-callback mockRequire('ServicePluginMock1', ServicePluginMock1);