improve plugin architecture

This commit is contained in:
Austen Collins 2015-10-18 14:32:48 -07:00 committed by doapp-ryanp
parent d324edf6f7
commit b31d6b29f2
5 changed files with 120 additions and 77 deletions

View File

@ -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 {
}
}
}
}

View File

@ -3,6 +3,7 @@
const Promise = require('bluebird');
class JawsPlugin {
/**
*
* @param Jaws class object

View File

@ -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

View File

@ -0,0 +1,9 @@
{
"plugins": [
{
"path": "./actions/ProjectCreate.js",
"options": ["options"],
"config": {}
}
]
}

View File

@ -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.<T>}
* @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)