ModuleCreate: finished refactor

This commit is contained in:
Eslam A. Hefnawy 2015-11-18 21:30:23 +02:00
parent 597bb02a30
commit 6cf95eef6d

View File

@ -8,7 +8,6 @@ const JawsPlugin = require('../../JawsPlugin'),
JawsError = require('../../jaws-error'),
JawsCLI = require('../../utils/cli'),
path = require('path'),
os = require('os'),
BbPromise = require('bluebird'),
JawsUtils = require('../../utils');
@ -36,12 +35,6 @@ class ModuleCreate extends JawsPlugin {
constructor(Jaws, config) {
super(Jaws, config);
this._templatesDir = path.join(__dirname, '..', '..', 'templates');
this._resource = '';
this._action = '';
this._pkgMgr = false;
this._createLambda = false;
this._createEndpoint = false;
this._runtime = 'nodejs';
}
/**
@ -88,7 +81,7 @@ usage: jaws module create <module resource> <action>`,
],
});
return Promise.resolve();
return BbPromise.resolve();
}
/**
@ -101,84 +94,95 @@ usage: jaws module create <module resource> <action>`,
* @param action
* @returns {Promise}
*/
moduleCreate(runtime, createLambda, createEndpoint, pkgMgr, resource, action) {
moduleCreate(evt) {
let _this = this;
_this.evt = evt;
if (!resource || !action) {
return Promise.reject(new JawsError('Must specify a resource and action'), JawsError.errorCodes.UNKNOWN);
}
if (!createLambda && !createEndpoint) { //default is to create both
createEndpoint = true;
createLambda = true;
}
this._resource = resource;
this._action = action;
this._createEndpoint = createEndpoint;
this._createLambda = createLambda;
this._runtime = runtime || 'nodejs';
if (!supportedRuntimes[this._runtime]) {
throw new JawsError('Unsupported runtime ' + _this._runtime, JawsError.errorCodes.UNKNOWN);
}
this._pkgMgr = pkgMgr || supportedRuntimes[this._runtime].defaultPkgMgr;
if (supportedRuntimes[this._runtime].validPkgMgrs.indexOf(this._pkgMgr) == -1) {
throw new JawsError('Unsupported package manger "' + this._pkgMgr + '"', JawsError.errorCodes.UNKNOWN);
}
return this.Jaws.validateProject()
.bind(_this)
.then(_this._validateData)
.then(_this._sanitizeData)
.then(_this._createSkeleton)
.then(_this._createPackageMgrSkeleton)
.then(_this._initRuntime)
.then(function() {
JawsCLI.log('Successfully created '
+ _this._resource
+ _this.evt.resource
+ '/'
+ _this._action);
+ _this.evt.action);
});
}
_validateData() {
let _this = this;
if (!_this.evt.resource || !_this.evt.action) {
return BbPromise.reject(new JawsError('Must specify a resource and action'), JawsError.errorCodes.UNKNOWN);
}
if (!_this.evt.createLambda && !_this.evt.createEndpoint) { //default is to create both
_this.evt.createEndpoint = true;
_this.evt.createLambda = true;
}
if(!_this.evt.runtime) {
_this.evt.runtime = 'nodejs';
}
if (!supportedRuntimes[_this.evt.runtime]) {
throw new JawsError('Unsupported runtime ' + _this.evt.runtime, JawsError.errorCodes.UNKNOWN);
}
if (!_this.evt.pkgMgr){
_this.evt.pkgMgr = supportedRuntimes[_this.evt.runtime].defaultPkgMgr;
}
if (supportedRuntimes[_this.evt.runtime].validPkgMgrs.indexOf(_this.evt.pkgMgr) == -1) {
throw new JawsError('Unsupported package manger "' + _this.evt.pkgMgr + '"', JawsError.errorCodes.UNKNOWN);
}
return BbPromise.resolve();
};
_sanitizeData() {
this._action = this._action.toLowerCase().trim()
_this.evt.action = _this.evt.action.toLowerCase().trim()
.replace(/\s/g, '-')
.replace(/[^a-zA-Z-\d:]/g, '')
.substring(0, 19);
this._resource = this._resource.toLowerCase().trim()
_this.evt.resource = _this.evt.resource.toLowerCase().trim()
.replace(/\s/g, '-')
.replace(/[^a-zA-Z-\d:]/g, '')
.substring(0, 19);
return BbPromise.resolve();
}
_generateActionAwsmJson() {
let actionTemplateJson = JawsUtils.readAndParseJsonSync(path.join(this._templatesDir, 'lambda.awsm.json'));
//We prefix with an l to make sure the CloudFormation resource map index is unique
actionTemplateJson.name = this._resource.charAt(0).toUpperCase() + this._resource.slice(1) + this._action.charAt(0).toUpperCase() + this._action.slice(1);
actionTemplateJson.name = _this.evt.resource.charAt(0).toUpperCase() + _this.evt.resource.slice(1) + _this.evt.action.charAt(0).toUpperCase() + _this.evt.action.slice(1);
if (this._createLambda) {
actionTemplateJson.cloudFormation.lambda.Function.Properties.Runtime = this._runtime;
if (_this.evt.createLambda) {
actionTemplateJson.cloudFormation.lambda.Function.Properties.Runtime = _this.evt.runtime;
// Create files for lambda actions
switch (this._runtime) {
switch (_this.evt.runtime) {
case 'nodejs':
actionTemplateJson.cloudFormation.lambda.Function.Properties.Handler = path.join('aws_modules', this._resource, this._action, 'handler.handler');
actionTemplateJson.cloudFormation.lambda.Function.Properties.Handler = path.join('aws_modules', _this.evt.resource, _this.evt.action, 'handler.handler');
break;
default:
throw new JawsError('This runtime is not supported "' + this._runtime + '"', JawsError.errorCodes.UNKNOWN);
throw new JawsError('This runtime is not supported "' + _this.evt.runtime + '"', JawsError.errorCodes.UNKNOWN);
break;
}
} else {
delete actionTemplateJson.lambda;
}
if (this._createEndpoint) {
actionTemplateJson.cloudFormation.apiGateway.Endpoint.Path = this._resource + '/' + this._action;
if (_this.evt.createEndpoint) {
actionTemplateJson.cloudFormation.apiGateway.Endpoint.Path = _this.evt.resource + '/' + _this.evt.action;
} else {
delete actionTemplateJson.cloudFormation.apiGateway;
}
@ -191,7 +195,7 @@ usage: jaws module create <module resource> <action>`,
_generateModuleAwsmJson() {
let moduleTemplateJson = JawsUtils.readAndParseJsonSync(path.join(this._templatesDir, 'module.awsm.json'));
moduleTemplateJson.name = this._resource;
moduleTemplateJson.name = _this.evt.resource;
return moduleTemplateJson;
};
@ -204,11 +208,11 @@ usage: jaws module create <module resource> <action>`,
let _this = this,
deferredWrites = [];
switch (_this.runtime) {
switch (_this.evt.runtime) {
case 'nodejs':
if (_this.pkgMgr == 'npm') {
if (_this.evt.pkgMgr == 'npm') {
let modulePath = path.join(_this.Jaws._projectRootPath, 'node_modules', _this._resource);
let modulePath = path.join(_this.Jaws._projectRootPath, 'node_modules', _this.evt.resource);
// Create node_module if DNE in node_modules
if (!JawsUtils.dirExistsSync(modulePath)) {
@ -218,7 +222,7 @@ usage: jaws module create <module resource> <action>`,
// Create module package.json if DNE in node_module
if (!JawsUtils.fileExistsSync(path.join(modulePath, 'package.json'))) {
let packageJsonTemplate = JawsUtils.readAndParseJsonSync(path.join(_this._templatesDir, 'nodejs', 'package.json'));
packageJsonTemplate.name = _this._resource;
packageJsonTemplate.name = _this.evt.resource;
packageJsonTemplate.description = 'An aws-module';
packageJsonTemplate.dependencies = {};
@ -247,7 +251,7 @@ usage: jaws module create <module resource> <action>`,
}
// Create action if DNE in node_module
let actionPath = path.join(modulePath, 'awsm', _this._action);
let actionPath = path.join(modulePath, 'awsm', _this.evt.action);
if (!JawsUtils.dirExistsSync(actionPath)) {
let actionTemplateJson = this._generateActionAwsmJson(),
@ -268,7 +272,7 @@ usage: jaws module create <module resource> <action>`,
break;
}
return Promise.all(deferredWrites);
return BbPromise.all(deferredWrites);
}
/**
@ -279,8 +283,8 @@ usage: jaws module create <module resource> <action>`,
_createSkeleton() {
let _this = this,
writeFilesDeferred = [],
modulePath = path.join(this.Jaws._projectRootPath, 'aws_modules', this._resource),
actionPath = path.join(modulePath, this._action);
modulePath = path.join(this.Jaws._projectRootPath, 'aws_modules', _this.evt.resource),
actionPath = path.join(modulePath, _this.evt.action);
// If module/action already exists, throw error
if (JawsUtils.dirExistsSync(actionPath)) {
@ -319,7 +323,7 @@ usage: jaws module create <module resource> <action>`,
JawsUtils.writeFile(path.join(actionPath, 'lambda.awsm.json'), JSON.stringify(actionTemplateJson, null, 2))
);
return Promise.all(writeFilesDeferred);
return BbPromise.all(writeFilesDeferred);
}
/**
@ -332,7 +336,7 @@ usage: jaws module create <module resource> <action>`,
JawsCLI.log('Preparing your runtime..');
if (_this._runtime === 'nodejs') {
if (_this.evt.runtime === 'nodejs') {
let packageJsonTemplate = JawsUtils.readAndParseJsonSync(path.join(_this._templatesDir, 'nodejs', 'package.json'));
packageJsonTemplate.name = _this.Jaws._projectJson.name;
return fs.writeFileAsync(path.join(_this.Jaws._projectRootPath, 'package.json'), JSON.stringify(packageJsonTemplate, null, 2))
@ -344,4 +348,4 @@ usage: jaws module create <module resource> <action>`,
}
}
module.exports = ModuleCreate;
module.exports = ModuleCreate;