From b31d6b29f2a0f2eaeb458addd03ef5dda891dd89 Mon Sep 17 00:00:00 2001 From: Austen Collins Date: Sun, 18 Oct 2015 14:32:48 -0700 Subject: [PATCH] improve plugin architecture --- lib/Jaws.js | 128 ++++++++++++++------------ lib/JawsPlugin.js | 1 + lib/defaults/actions/ProjectCreate.js | 20 ++-- lib/defaults/defaults.json | 9 ++ tests/cli/TestPlugins.js | 39 ++++++-- 5 files changed, 120 insertions(+), 77 deletions(-) create mode 100644 lib/defaults/defaults.json diff --git a/lib/Jaws.js b/lib/Jaws.js index 1bd5bac25..84afafb69 100644 --- a/lib/Jaws.js +++ b/lib/Jaws.js @@ -26,6 +26,8 @@ class Jaws { this._projectRootPath = utils.findProjectRootPath(process.cwd()); this._projectJson = false; this._queue = []; + this.actions = {}; + this.hooks = {}; // If within project, add further meta data if (this._projectRootPath) { @@ -46,11 +48,6 @@ class Jaws { this._credentials = AWSUtils.profilesGet(this._profile)[this._profile]; } - // Create registry for actions, with defaults - this.actions = {}; - let ProjectCreate = require('./defaults/actions/ProjectCreate'); - this.addPlugin(new ProjectCreate(this, {})); - //{ // ProjectCreate: null, // StageCreate: null, @@ -72,42 +69,44 @@ class Jaws { //}; // Create registry for hooks - this.hooks = { - PreProjectCreate: [], - PostProjectCreate: [], - PreStageCreate: [], - PostStageCreate: [], - PreRegionCreate: [], - PostRegionCreate: [], - PreModuleCreate: [], - PostModuleCreate: [], - PreModulePostInstall: [], - PostModulePostInstall: [], - PreLambdaPackage: [], - PostLambdaPackage: [], - PreLambdaUpload: [], - PostLambdaUpload: [], - PreLambdaProvision: [], - PostLambdaProvision: [], - PreApiGatewayProvision: [], - PostApiGatewayProvision: [], - PreResourcesProvision: [], - PostResourcesProvision: [], - PreEnvList: [], - PostEnvList: [], - PreEnvGet: [], - PostEnvGet: [], - PreEnvSet: [], - PostEnvSet: [], - PreTagResource: [], - PostTagResource: [], - PreLambdaRun: [], - PostLambdaRun: [], - PreDash: [], - PostDash: [], - }; + //PreProjectCreate: [], + //PostProjectCreate: [], + //PreStageCreate: [], + //PostStageCreate: [], + //PreRegionCreate: [], + //PostRegionCreate: [], + //PreModuleCreate: [], + //PostModuleCreate: [], + //PreModulePostInstall: [], + //PostModulePostInstall: [], + //PreLambdaPackage: [], + //PostLambdaPackage: [], + //PreLambdaUpload: [], + //PostLambdaUpload: [], + //PreLambdaProvision: [], + //PostLambdaProvision: [], + //PreApiGatewayProvision: [], + //PostApiGatewayProvision: [], + //PreResourcesProvision: [], + //PostResourcesProvision: [], + //PreEnvList: [], + //PostEnvList: [], + //PreEnvGet: [], + //PostEnvGet: [], + //PreEnvSet: [], + //PostEnvSet: [], + //PreTagResource: [], + //PostTagResource: [], + //PreLambdaRun: [], + //PostLambdaRun: [], + //PreDash: [], + //PostDash: [], - // If within project, load plugins + // Load plugins: defaults + //var defaults = require('./defaults/defaults.json'); + //this._loadPlugins(defaults); + + // Load plugins: project if (this._projectRootPath) { this._loadPlugins(this._projectJson.plugins); } @@ -126,21 +125,30 @@ class Jaws { if (config.plugins) { this._loadPlugins(config.plugins); } - } /** * Set Action */ - action(actionName, action) { + action(actionName, action, config) { - // Check action is valid - if (!this.actions[actionName]) { + let _this = this; + // Add Action + _this.actions[actionName] = action; + + // Add Action Pre & Post hooks + _this.hooks['Pre' + actionName] = []; + _this.hooks['Post' + actionName] = []; + + // Add Action handler + if (config && config.handler) { + _this[config.handler] = this.actions[actionName]; } - this.actions[actionName] = action; + // Add command + } /** @@ -158,12 +166,12 @@ class Jaws { this.hooks[hookName].splice(index, 0, hook); } - /** - * + * Add Plugin * @param JawsPlugin class object * @returns {Promise} */ + addPlugin(JawsPlugin) { return Promise.all([ JawsPlugin.registerActions(), @@ -175,14 +183,14 @@ class Jaws { * Project Create * @returns {*} */ - projectCreate(options) { - - // Prepare & Execute Queue - this._queue = this._queue.concat(this.hooks.PreProjectCreate); - this._queue.push(this.actions.ProjectCreate.bind({}, options)); - this._queue = this._queue.concat(this.hooks.PostProjectCreate); - return this._executeQueue(); - } + //projectCreate(options) { + // + // // Prepare & Execute Queue + // this._queue = this._queue.concat(this.hooks.PreProjectCreate); + // this._queue.push(this.actions.ProjectCreate.bind({}, options)); + // this._queue = this._queue.concat(this.hooks.PostProjectCreate); + // return this._executeQueue(); + //} /** * Execute Queue @@ -208,10 +216,14 @@ class Jaws { _loadPlugins(plugins) { - let pluginArray = Object.keys(plugins); + for (let i = 0; i < plugins.length; i++) { + let plugin = plugins[i]; - for (let i in pluginArray) { - pluginArray[i](plugins[pluginArray[i]]).bind(this); + if (plugin.path) { + require(plugin.path); + } else { + + } } } } diff --git a/lib/JawsPlugin.js b/lib/JawsPlugin.js index 300152d70..44ab9abb0 100644 --- a/lib/JawsPlugin.js +++ b/lib/JawsPlugin.js @@ -3,6 +3,7 @@ const Promise = require('bluebird'); class JawsPlugin { + /** * * @param Jaws class object diff --git a/lib/defaults/actions/ProjectCreate.js b/lib/defaults/actions/ProjectCreate.js index 8f0ae9227..3c250c93c 100644 --- a/lib/defaults/actions/ProjectCreate.js +++ b/lib/defaults/actions/ProjectCreate.js @@ -5,15 +5,15 @@ */ const JawsPlugin = require('../../JawsPlugin'), - JawsError = require('../../jaws-error'), - JawsCLI = require('../../utils/cli'), - Promise = require('bluebird'), - fs = require('fs'), - path = require('path'), - os = require('os'), - AWSUtils = require('../../utils/aws'), - utils = require('../../utils'), - shortid = require('shortid'); + JawsError = require('../../jaws-error'), + JawsCLI = require('../../utils/cli'), + Promise = require('bluebird'), + fs = require('fs'), + path = require('path'), + os = require('os'), + AWSUtils = require('../../utils/aws'), + utils = require('../../utils'), + shortid = require('shortid'); /** * ProjectCreate Class @@ -50,7 +50,7 @@ class ProjectCreate extends JawsPlugin { let _this = this; - return function* (options) { + return Promise.try(function(resolve, reject) { /** * Non-Interactive Validations diff --git a/lib/defaults/defaults.json b/lib/defaults/defaults.json new file mode 100644 index 000000000..3675a5f5c --- /dev/null +++ b/lib/defaults/defaults.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + { + "path": "./actions/ProjectCreate.js", + "options": ["options"], + "config": {} + } + ] +} \ No newline at end of file diff --git a/tests/cli/TestPlugins.js b/tests/cli/TestPlugins.js index 9115e75c5..69f239d1e 100644 --- a/tests/cli/TestPlugins.js +++ b/tests/cli/TestPlugins.js @@ -9,6 +9,7 @@ let JAWS = require('../../lib/Jaws.js'), JawsError = require('../../lib/jaws-error'), path = require('path'), os = require('os'), + commop = require ('commop'), utils = require('../../lib/utils'), assert = require('chai').assert, shortid = require('shortid'), @@ -35,15 +36,43 @@ class PromisePlugin extends JawsPlugin { super(Jaws, config); } + /** + * Register Actions + */ + registerActions() { - this.Jaws.action('ProjectCreate', this._actionProjectCreate()); + var config = { + handler: 'projectCreate', + command: 'project create', + options: ['options'], + }; + this.Jaws.action('ProjectCreate', this._actionProjectCreate.bind(this), config); // bind is optional } + /** + * Register Hooks + */ + registerHooks() { this.Jaws.hook('PreProjectCreate', this._hookPreProjectCreate()); this.Jaws.hook('PostProjectCreate', this._hookPostProjectCreate()); } + /** + * Plugin Logic + * @param options + * @returns {*|Promise.} + * @private + */ + + _actionProjectCreate(options) { + let _this = this; + return Promise.delay(500) + .then(function() { + _this.Jaws.generatorPluginHookAction = true; + }); + } + _hookPreProjectCreate() { let _this = this; return new Promise(function(resolve, reject) { @@ -52,14 +81,6 @@ class PromisePlugin extends JawsPlugin { }) } - _actionProjectCreate() { - let _this = this; - return Promise.delay(500) - .then(function() { - _this.Jaws.generatorPluginHookAction = true; - }); - } - _hookPostProjectCreate() { let _this = this; return Promise.delay(500)