function and endpoint deploy now deploys according to CWD

This commit is contained in:
Eslam A. Hefnawy 2016-01-06 12:12:01 +02:00
parent 04d866f6ee
commit 791ce2d7b2
10 changed files with 128 additions and 71 deletions

View File

@ -46,6 +46,7 @@ class ServerlessModule {
_this.data.runtime = _this.options.runtime || 'nodejs';
_this.data.custom = {};
_this.data.functions = {};
_this.data.templates = {};
_this.data.cloudFormation = {
resources: {},
lambdaIamPolicyDocumentStatements: []

View File

@ -253,6 +253,7 @@ module.exports = function(SPlugin, serverlessPath) {
let _this = this;
let params = {
restApiId: _this.restApi.id, /* required */
limit: 500
@ -261,7 +262,6 @@ module.exports = function(SPlugin, serverlessPath) {
// List all Resources for this REST API
return SUtils.persistentRequest(function() { return _this.ApiGateway.getResourcesPromised(params); })
.then(function(response) {
_this.apiResources = response.items;
SUtils.sDebug(
@ -646,6 +646,7 @@ module.exports = function(SPlugin, serverlessPath) {
})
.catch(function(error) {
throw new SError(error.message);
});
});

View File

@ -64,15 +64,18 @@ module.exports = function(SPlugin, serverlessPath) {
option: 'aliasRestApi', // TODO: Implement
shortcut: 'p',
description: 'Optional - Override the API Gateway "functionAlias" Stage Variable'
}, {
option: 'all',
shortcut: 'a',
description: 'Optional - Select all Functions in your project for deployment'
}, {
option: 'description',
shortcut: 'd',
description: 'Optional - Provide custom description string for API Gateway stage deployment description'
}
],
parameters: [
{
parameter: 'paths',
description: 'One or multiple paths to your endpoint',
position: '0->'
}
]
});
@ -88,13 +91,6 @@ module.exports = function(SPlugin, serverlessPath) {
let _this = this;
_this.evt = evt;
// If CLI, parse arguments
if (_this.S.cli && (!options || !options.subaction)) {
_this.evt.options = JSON.parse(JSON.stringify(_this.S.cli.options)); // Important: Clone objects, don't refer to them
_this.evt.options.paths = JSON.parse(JSON.stringify(_this.S.cli.params));
if (_this.S.cli.options.nonInteractive) _this.S.config.interactive = false;
}
// Flow
return new BbPromise(function(resolve) {
@ -166,12 +162,38 @@ module.exports = function(SPlugin, serverlessPath) {
// Set defaults
_this.evt.options.paths = _this.evt.options.paths ? _this.evt.options.paths : [];
_this.evt.options.all = !!_this.evt.options.all;
_this.regions = _this.evt.options.region ? [_this.evt.options.region] : Object.keys(_this.meta.data.private.stages[_this.evt.options.stage].regions);
// Validate Paths
if (!_this.evt.options.paths.length && !_this.evt.options.all) {
throw new SError(`One or multiple paths are required, or the all option must be set`);
if (!_this.evt.options.paths.length) {
let CWD = process.cwd(),
isModule = SUtils.fileExistsSync(path.join(CWD, 's-module.json')) || SUtils.fileExistsSync(path.join(CWD, '..', 's-module.json')),
isFunction = SUtils.fileExistsSync(path.join(CWD, 's-function.json'));
if (isModule) {
let moduleName;
try {
moduleName = SUtils.readAndParseJsonSync(path.join(CWD, 's-module.json')).name;
} catch (e) {
moduleName = SUtils.readAndParseJsonSync(path.join(CWD, '..', 's-module.json')).name;
}
let functionList = fs.readdirSync(path.join(_this.S.config.projectPath, 'back', 'modules', moduleName, 'functions'));
functionList.forEach(function(functionName) {
let FunctionInstance = new _this.S.classes.Function(_this.S, {module: moduleName, function: functionName});
FunctionInstance.data.endpoints.forEach(function(endpoint) {
_this.evt.options.paths.push(moduleName + path.sep + functionName + '@' + endpoint.path + '~' + endpoint.method);
});
});
} else if (isFunction) {
let functionName = SUtils.readAndParseJsonSync(path.join(CWD, 's-function.json')).name;
let moduleName = SUtils.readAndParseJsonSync(path.join(CWD, '..', '..', 's-module.json')).name;
let FunctionInstance = new _this.S.classes.Function(_this.S, {module: moduleName, function: functionName});
FunctionInstance.data.endpoints.forEach(function(endpoint) {
_this.evt.options.paths.push(moduleName + path.sep + functionName + '@' + endpoint.path + '~' + endpoint.method);
});
}
}
// Validate Stage
@ -264,15 +286,17 @@ module.exports = function(SPlugin, serverlessPath) {
async.eachSeries(_this.endpoints, function(endpoint, eCb) {
// Create new event object
let options = {
stage: _this.evt.options.stage,
region: region,
path: endpoint.sPath,
aliasEndpoint: _this.evt.options.aliasEndpoint,
aliasRestApi: _this.evt.options.aliasRestApi
let newEvt = {
options: {
stage: _this.evt.options.stage,
region: region,
path: endpoint.sPath,
aliasEndpoint: _this.evt.options.aliasEndpoint,
aliasRestApi: _this.evt.options.aliasRestApi
}
};
return _this.S.actions.endpointBuildApiGateway(options)
return _this.S.actions.endpointBuildApiGateway(newEvt)
.then(function (result) {
// Stash deployed endpoints
@ -317,14 +341,17 @@ module.exports = function(SPlugin, serverlessPath) {
if (!_this.deployed) return;
// Deploy API Gateway Deployment in region
let options = {
stage: _this.evt.options.stage,
region: region,
restApiId: _this.restApiData.id,
description: _this.evt.options.description
let newEvt = {
options: {
stage: _this.evt.options.stage,
region: region,
restApiId: _this.restApiData.id,
description: _this.evt.options.description
}
};
return _this.S.actions.endpointDeployApiGateway(options);
return _this.S.actions.endpointDeployApiGateway(newEvt);
});
});
}

View File

@ -108,7 +108,7 @@ usage: serverless function create <function>`,
if (!_this.S.config.interactive) return BbPromise.resolve();
['module', 'function'].forEach(memberVarKey => {
overrides[memberVarKey] = _this['options'][memberVarKey];
overrides[memberVarKey] = _this.evt.options[memberVarKey];
});
let prompts = {

View File

@ -64,10 +64,6 @@ module.exports = function(SPlugin, serverlessPath) {
option: 'aliasFunction', // TODO: Implement
shortcut: 'f',
description: 'Optional - Provide a custom Alias to your Functions'
}, {
option: 'all',
shortcut: 'a',
description: 'Optional - Select all Functions in your project for deployment'
}
],
parameters: [
@ -168,7 +164,6 @@ module.exports = function(SPlugin, serverlessPath) {
// Set Defaults
_this.evt.options.stage = _this.evt.options.stage ? _this.evt.options.stage : null;
_this.evt.options.paths = _this.evt.options.paths ? _this.evt.options.paths : [];
_this.evt.options.all = _this.evt.options.all ? true : false;
_this.evt.options.aliasFunction = _this.evt.options.aliasFunction ? _this.evt.options.aliasFunction : null;
// Instantiate Classes
@ -178,9 +173,30 @@ module.exports = function(SPlugin, serverlessPath) {
// Set Deploy Regions
_this.regions = _this.evt.options.region ? [_this.evt.options.region] : Object.keys(_this.meta.data.private.stages[_this.evt.options.stage].regions);
// Validate Paths
if (!_this.evt.options.paths.length && !_this.evt.options.all) {
throw new SError(`One or multiple paths are required, or the all option must be set`);
if (!_this.evt.options.paths.length) {
let CWD = process.cwd(),
//isProject = SUtils.fileExistsSync(path.join(CWD, 's-project.json')) || SUtils.fileExistsSync(path.join(CWD, '..', 's-project.json')) || SUtils.fileExistsSync(path.join(CWD, '..', '..', 's-project.json')),
isModule = SUtils.fileExistsSync(path.join(CWD, 's-module.json')) || SUtils.fileExistsSync(path.join(CWD, '..', 's-module.json')),
isFunction = SUtils.fileExistsSync(path.join(CWD, 's-function.json'));
if (isModule) {
let moduleName;
try {
moduleName = SUtils.readAndParseJsonSync(path.join(CWD, 's-module.json')).name;
} catch (e) {
moduleName = SUtils.readAndParseJsonSync(path.join(CWD, '..', 's-module.json')).name;
}
let functionList = fs.readdirSync(path.join(_this.S.config.projectPath, 'back', 'modules', moduleName, 'functions'));
functionList.forEach(function(functionName) {
_this.evt.options.paths.push(moduleName + path.sep + functionName);
});
} else if (isFunction) {
let functionName = SUtils.readAndParseJsonSync(path.join(CWD, 's-function.json')).name;
let moduleName = SUtils.readAndParseJsonSync(path.join(CWD, '..', '..', 's-module.json')).name;
_this.evt.options.paths.push(moduleName + path.sep + functionName);
}
}
// Validate Stage
@ -218,7 +234,7 @@ module.exports = function(SPlugin, serverlessPath) {
let getFunctionsOptions = {
paths: _this.evt.options.paths
}
_this.functions = _this.project.getFunctions(_this.evt.options.all ? null : getFunctionsOptions );
_this.functions = _this.project.getFunctions(getFunctionsOptions);
// Deploy Function Code in each region
return _this._deployCodeByRegion(region);
@ -252,25 +268,33 @@ module.exports = function(SPlugin, serverlessPath) {
// Nodejs
if (func.data.runtime = 'nodejs') {
// Package Code
return _this.S.actions.codePackageLambdaNodejs({
let newEvt = {
options: {
stage: _this.evt.options.stage,
region: region,
module: func.module,
function: func.data.name
})
}
};
// Package Code
return _this.S.actions.codePackageLambdaNodejs(newEvt)
.bind(_this)
.then(function(result) {
let newEvt = {
options: {
stage: result.options.stage,
region: result.options.region,
module: result.options.module,
function: result.options.function,
pathDist: result.data.pathDist,
pathsPackaged: result.data.pathsPackaged
}
};
// Deploy Code
return _this.S.actions.codeDeployLambdaNodejs({
stage: result.options.stage,
region: result.options.region,
module: result.options.module,
function: result.options.function,
pathDist: result.data.pathDist,
pathsPackaged: result.data.pathsPackaged
});
return _this.S.actions.codeDeployLambdaNodejs(newEvt);
});
}
})

View File

@ -124,7 +124,7 @@ usage: serverless module create`,
if (!_this.S.config.interactive) return BbPromise.resolve();
['module', 'function'].forEach(memberVarKey => {
overrides[memberVarKey] = _this['options'][memberVarKey];
overrides[memberVarKey] = _this.evt.options[memberVarKey];
});
let prompts = {

View File

@ -525,7 +525,7 @@ exports.populate = function(S, populatedData, stage, region) {
// Combine all templates found in project
let templates = {};
for (let i = 0; i < Object.keys(project.data.modules).length; i++) {
let module = Object.keys(project.data.modules);
let module = Object.keys(project.data.modules)[i];
templates[module] = project.data.modules[module].templates;
}

View File

@ -10,20 +10,20 @@ describe('All Tests', function() {
});
after(function() {});
require('./tests/actions/TestPluginCustom');
//
//require('./tests/actions/TestPluginCustom');
require('./tests/actions/TestDefaultActionHook');
require('./tests/actions/ProjectCreate');
require('./tests/actions/StageCreate');
require('./tests/actions/RegionCreate');
require('./tests/actions/ModuleInstall');
require('./tests/actions/ModuleCreate');
require('./tests/actions/FunctionCreate');
require('./tests/actions/EnvList');
require('./tests/actions/EnvGet');
require('./tests/actions/EnvSetUnset');
require('./tests/actions/ResourcesDeploy');
require('./tests/actions/FunctionRun');
require('./tests/actions/FunctionDeploy');
require('./tests/actions/EndpointDeploy');
//require('./tests/actions/ProjectCreate');
//require('./tests/actions/StageCreate');
//require('./tests/actions/RegionCreate');
//require('./tests/actions/ModuleInstall');
//require('./tests/actions/ModuleCreate');
//require('./tests/actions/FunctionCreate');
//require('./tests/actions/EnvList');
//require('./tests/actions/EnvGet');
//require('./tests/actions/EnvSetUnset');
//require('./tests/actions/ResourcesDeploy');
//require('./tests/actions/FunctionRun');
//require('./tests/actions/FunctionDeploy');
//require('./tests/actions/EndpointDeploy');
});

View File

@ -52,8 +52,10 @@ describe('Test action: Module Create', function() {
this.timeout(0);
let evt = {
module: 'temp',
function: 'one'
options: {
module: 'temp',
function: 'one'
}
};
serverless.actions.moduleCreate(evt)

View File

@ -102,8 +102,10 @@ describe('Test Default Action With Pre Hook', function() {
this.timeout(0);
let evt = {
module: 'temp',
function: 'one'
options: {
module: 'temp',
function: 'one'
}
};
serverless.actions.moduleCreate(evt)