From 56efd97f8a08dc7e6270408a128d62ab6f2cd14d Mon Sep 17 00:00:00 2001 From: "Eslam A. Hefnawy" Date: Thu, 3 Mar 2016 01:54:41 +0700 Subject: [PATCH] CLI testing: some fixes --- lib/Function.js | 2 +- lib/actions/DashDeploy.js | 15 +++++++-------- lib/actions/DashSummary.js | 4 ++-- lib/actions/EndpointDeploy.js | 12 +++++++----- lib/actions/FunctionCreate.js | 6 +++--- lib/actions/FunctionDeploy.js | 12 ++++++++---- 6 files changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/Function.js b/lib/Function.js index 6c7883a11..74e342eff 100644 --- a/lib/Function.js +++ b/lib/Function.js @@ -27,7 +27,7 @@ class Function extends SerializerFileSystem { this.name = data.name || 'function' + SUtils.generateShortId(6); this.customName = false; this.customRole = false; - this.handler = './' + data.name +'/handler.handler'; + this.handler = data.name +'/handler.handler'; this.timeout = 6; this.memorySize = 1024; this.custom = { diff --git a/lib/actions/DashDeploy.js b/lib/actions/DashDeploy.js index 746a86fd0..6a8b63571 100644 --- a/lib/actions/DashDeploy.js +++ b/lib/actions/DashDeploy.js @@ -165,16 +165,15 @@ module.exports = function(SPlugin, serverlessPath) { _prompt() { let _this = this, - sPath = _this.getSPathFromCwd(_this.S._projectPath), - sPathParsed = SUtils.parseSPath(sPath), + cwdType = SUtils.getCwdType(), functions; - if (sPathParsed.function) { - functions = [_this.S.getProject().getFunction(sPathParsed.function)]; - } else if (sPathParsed.component) { - functions = _this.S.getProject().getComponent(sPathParsed.component).getAllFunctions(); + if(cwdType.function) { + functions = [_this.project.getFunction(cwdType.function)]; + } else if (cwdType.component) { + functions = _this.project.getComponent(cwdType.component).getAllFunctions(); } else { - functions = _this.S.getProject().getAllFunctions(); + functions = _this.project.getAllFunctions(); } // Prepare function & endpoints choices @@ -183,7 +182,7 @@ module.exports = function(SPlugin, serverlessPath) { _.each( functions, function(func){ // Push function component as spacer choices.push({ - spacer: func.getComponent() + spacer: func.getName() }); choices.push({ diff --git a/lib/actions/DashSummary.js b/lib/actions/DashSummary.js index faf9241be..fb6d09a33 100644 --- a/lib/actions/DashSummary.js +++ b/lib/actions/DashSummary.js @@ -64,8 +64,8 @@ module.exports = function(SPlugin, serverlessPath) { SCli.log(`${stages.length} stages ------------------------------`); stages.forEach(function(stage) { stagesNum++; - let regions = _this.S.getProject().getAllRegions(stage); - SCli.log(` |_ ${stage} (${regions.length} regions)`); + let regions = _this.S.getProject().getAllRegions(stage.name); + SCli.log(` |_ ${stage.name} (${regions.length} regions)`); // list regions for stage regions.forEach(function(region) { diff --git a/lib/actions/EndpointDeploy.js b/lib/actions/EndpointDeploy.js index 71c8d24d7..219afc217 100644 --- a/lib/actions/EndpointDeploy.js +++ b/lib/actions/EndpointDeploy.js @@ -172,8 +172,10 @@ module.exports = function(SPlugin, serverlessPath) { _this.regions = _this.evt.options.region ? [_this.evt.options.region] : _this.project.getAllRegions(_this.evt.options.stage).map(r => {return r.name;}); if (_this.evt.options.names.length) { - _this.evt.options.names.forEach(function(endpointName) { - _this.endpoints.push(_this.project.getEndpoint(endpointName.split('#')[0], endpointName.split('#')[1])); + _this.evt.options.names.forEach(function(name) { + let endpoint = _this.project.getEndpoint(name.split('#')[0], name.split('#')[1]); + if (!endpoint) throw new SError(`Endpoint "${name}" doesn't exist in your project`); + _this.endpoints.push(_this.project.getEndpoint(endpoint)); }); } @@ -197,10 +199,10 @@ module.exports = function(SPlugin, serverlessPath) { _this.endpoints = _this.project.getAllEndpoints(); } + if (_this.endpoints.length === 0) throw new SError(`You don't have any endpoints in your project`); + // Validate Stage - if (!_this.evt.options.stage) { - throw new SError(`Stage is required`); - } + if (!_this.evt.options.stage) throw new SError(`Stage is required`); return BbPromise.resolve(); } diff --git a/lib/actions/FunctionCreate.js b/lib/actions/FunctionCreate.js index 4e3dbf25a..84d367f9f 100644 --- a/lib/actions/FunctionCreate.js +++ b/lib/actions/FunctionCreate.js @@ -162,16 +162,16 @@ usage: serverless function create `, _this.functionName = _this.evt.options.path.split('/')[_this.evt.options.path.split('/').length - 1]; - // Validate: Don't allow function creation within a function + if (_this.S.getProject().getFunction( _this.functionName )) { - return BbPromise.reject(new SError('You cannot create a function in another function')); + return BbPromise.reject(new SError(`Please choose a unique function name. You already have a function named "${_this.functionName}"`)); } // If component does not exist in project, throw error _this.evt.options.component = _this.S.getProject().getComponent( _this.evt.options.path.split('/')[0] ); if (!_this.evt.options.component) { return BbPromise.reject(new SError( - 'Component (' + _this.evt.options.path.split('/')[0] + ') does not exist in project', + 'Component "' + _this.evt.options.path.split('/')[0] + '" does not exist in project', SError.errorCodes.INVALID_PROJECT_SERVERLESS )); } diff --git a/lib/actions/FunctionDeploy.js b/lib/actions/FunctionDeploy.js index c9f05b88c..29ee45bb3 100644 --- a/lib/actions/FunctionDeploy.js +++ b/lib/actions/FunctionDeploy.js @@ -188,13 +188,12 @@ module.exports = function(SPlugin, serverlessPath) { if (_this.evt.options.names.length) { _this.evt.options.names.forEach(function(name) { + let func = _this.project.getFunction(name); + if (!func) throw new SError(`Function "${name}" doesn't exist in your project`); _this.functions.push(_this.project.getFunction(name)); }); } - // Validate Stage - if (!_this.evt.options.stage) throw new SError(`Stage is required`); - // If CLI and no function names targeted, deploy from CWD if (_this.S.cli && !_this.evt.options.names.length && @@ -211,7 +210,7 @@ module.exports = function(SPlugin, serverlessPath) { // If --all is selected, load all paths if (_this.evt.options.all) { - _this.evt.options.functions = _this.S.getProject().getAllFunctions(); + _this.functions = _this.S.getProject().getAllFunctions(); } // Ensure tmp folder exists in _meta @@ -219,6 +218,11 @@ module.exports = function(SPlugin, serverlessPath) { fse.mkdirSync(_this.S.getProject().getRootPath('_meta', '_tmp')); } + if (_this.functions.length === 0) throw new SError(`You don't have any functions in your project`); + + // Validate Stage + if (!_this.evt.options.stage) throw new SError(`Stage is required`); + return BbPromise.resolve(); }