diff --git a/lib/Function.js b/lib/Function.js index 44c0ae390..5beffdbc8 100644 --- a/lib/Function.js +++ b/lib/Function.js @@ -171,7 +171,8 @@ class ServerlessFunction { while( !_this._S.classes.Component.isComponentDir( p ) ) { if( SUtils.fileExistsSync(path.join(p, 's-templates.json'))) { - templates.unshift( require(path.join(p, 's-templates.json')) ); + let template = JSON.parse(fs.readFileSync(path.join(p, 's-templates.json'), 'utf8')); + templates.unshift(template); } p = path.join( p, '..' ); } @@ -266,10 +267,7 @@ class ServerlessFunction { // Populate let clone = _this.get(); clone = SUtils.populate(_this._S.state.getMeta(), _this.getTemplates(), clone, options.stage, options.region); - clone.endpoints = []; - for (let i = 0; i < _this.endpoints.length; i++) { - clone.endpoints[i] = _this.endpoints[i].getPopulated(options); - } + clone.endpoints = _this.endpoints.map( endpoint => endpoint.getPopulated(options) ); return clone; } diff --git a/lib/Project.js b/lib/Project.js index 0055682bd..ec3d57a13 100644 --- a/lib/Project.js +++ b/lib/Project.js @@ -299,7 +299,7 @@ class ServerlessProject extends VarContainer { resources = SUtils.readAndParseJsonSync(_this._S.getProject().getFilePath('s-resources-cf.json')); } - return _.flatten( _.map( _.values( _this.components ), c => c.getCFSnippets() ) ); + return _.flattenDeep( _.map( _.values( _this.components ), c => c.getCFSnippets() ) ); }) .then(function (cfSnippets) { @@ -477,7 +477,7 @@ class ServerlessProject extends VarContainer { getAllComponents(options){ if( _.get( options, 'paths' ) ){ - options.paths = _.map( options.paths, p => p.split(path.sep)[0] ); + options.paths = _.map( options.paths, p => p && p.split(path.sep)[0] ); } return SUtils.filterSPaths( _.values( this.components ), options ); } @@ -535,7 +535,7 @@ class ServerlessProject extends VarContainer { } getRegion( stageName, regionName ){ - if( this.hasStage( stageName ) ){ + if( this.validateStageExists( stageName ) ){ let stage = this.getStage( stageName ); if( stage.hasRegion( regionName ) ){ diff --git a/lib/ProviderAws.js b/lib/ProviderAws.js index a32a096d0..dcfc38a57 100644 --- a/lib/ProviderAws.js +++ b/lib/ProviderAws.js @@ -65,22 +65,24 @@ class ServerlessProviderAws { let req = awsService[method](params); // Add listeners... - //req.on('validate', function(r) {}); + req.on('validate', function(r) {}); - let performRequest = function() { - return new BbPromise(function(resolve, reject) { + return new BbPromise(function(resolve, reject) { + let performRequest = function() { req.send(function(err, data) { if (err && err.statusCode == 429) { SUtils.sDebug("'Too many requests' received, sleeping 5 seconds, then retrying..."); - setTimeout( performRequest, 5000 ); + return setTimeout( performRequest, 5000 ); } else if (err) { - reject( err ); + return reject(err); } - resolve(data); + + return resolve(data); }); - }); - }; - return performRequest(); + }; + + performRequest(); + }); } /** @@ -143,6 +145,14 @@ class ServerlessProviderAws { sessionToken: process.env['AWS_SESSION_TOKEN'], region: region }; + } else if (this._S.config.awsAdminKeyId) { + + // Access Keys from the config + credentials = { + accessKeyId: this._S.config.awsAdminKeyId, + secretAccessKey: this._S.config.awsAdminSecretKey, + region: region + }; } if (!credentials) { diff --git a/lib/actions/CodeDeployLambda.js b/lib/actions/CodeDeployLambda.js index 2f82acf30..898e20bfa 100644 --- a/lib/actions/CodeDeployLambda.js +++ b/lib/actions/CodeDeployLambda.js @@ -9,7 +9,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), BbPromise = require('bluebird'), Zip = require('node-zip'), diff --git a/lib/actions/CodePackageLambda.js b/lib/actions/CodePackageLambda.js index 6b303783f..05dcb858a 100644 --- a/lib/actions/CodePackageLambda.js +++ b/lib/actions/CodePackageLambda.js @@ -11,7 +11,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), BbPromise = require('bluebird'), fs = require('fs'), @@ -181,7 +181,9 @@ module.exports = function(SPlugin, serverlessPath) { SUtils.sDebug(`Getting ENV Vars: ${_this.meta.variables.projectBucket} - ${key}`); // Get ENV file from S3 + let NoSuchKey = {code: 'NoSuchKey'}; return _this.aws.request('S3', 'getObject', params, _this.evt.options.stage, _this.evt.options.region) + .catch(NoSuchKey => ({Body: ''})) .then(function(s3ObjData) { fs.writeFileSync( diff --git a/lib/actions/ComponentCreate.js b/lib/actions/ComponentCreate.js index 0bc8b7709..b8c23478a 100644 --- a/lib/actions/ComponentCreate.js +++ b/lib/actions/ComponentCreate.js @@ -13,7 +13,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), SUtils = require(path.join(serverlessPath, 'utils')), BbPromise = require('bluebird'), diff --git a/lib/actions/DashDeploy.js b/lib/actions/DashDeploy.js index 9eea89d16..bd53f924b 100644 --- a/lib/actions/DashDeploy.js +++ b/lib/actions/DashDeploy.js @@ -19,7 +19,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), diff --git a/lib/actions/EndpointBuildApiGateway.js b/lib/actions/EndpointBuildApiGateway.js index 2700641b1..d2ece72c3 100644 --- a/lib/actions/EndpointBuildApiGateway.js +++ b/lib/actions/EndpointBuildApiGateway.js @@ -8,7 +8,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), BbPromise = require('bluebird'), async = require('async'), diff --git a/lib/actions/EndpointDeploy.js b/lib/actions/EndpointDeploy.js index 4f64dbfdd..da3c7443b 100644 --- a/lib/actions/EndpointDeploy.js +++ b/lib/actions/EndpointDeploy.js @@ -20,7 +20,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), diff --git a/lib/actions/EndpointDeployApiGateway.js b/lib/actions/EndpointDeployApiGateway.js index d7aca13d7..665d600b8 100644 --- a/lib/actions/EndpointDeployApiGateway.js +++ b/lib/actions/EndpointDeployApiGateway.js @@ -8,7 +8,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), BbPromise = require('bluebird'), fs = require('fs'); diff --git a/lib/actions/EnvGet.js b/lib/actions/EnvGet.js index 6e683fb30..de0c4d74f 100644 --- a/lib/actions/EnvGet.js +++ b/lib/actions/EnvGet.js @@ -12,7 +12,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), SUtils = require(path.join(serverlessPath, 'utils/index')), chalk = require('chalk'), diff --git a/lib/actions/EnvList.js b/lib/actions/EnvList.js index 5028c9b24..faa71b7a5 100644 --- a/lib/actions/EnvList.js +++ b/lib/actions/EnvList.js @@ -11,7 +11,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), chalk = require('chalk'), BbPromise = require('bluebird'), diff --git a/lib/actions/EnvSet.js b/lib/actions/EnvSet.js index e95d0b7e9..513603d3a 100644 --- a/lib/actions/EnvSet.js +++ b/lib/actions/EnvSet.js @@ -7,7 +7,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), SUtils = require(path.join(serverlessPath, 'utils')); diff --git a/lib/actions/EnvUnset.js b/lib/actions/EnvUnset.js index 085ea1d5d..a9175a545 100644 --- a/lib/actions/EnvUnset.js +++ b/lib/actions/EnvUnset.js @@ -7,7 +7,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), SUtils = require(path.join(serverlessPath, 'utils')); diff --git a/lib/actions/EventDeploy.js b/lib/actions/EventDeploy.js index 8fe0b458c..01d9e902d 100644 --- a/lib/actions/EventDeploy.js +++ b/lib/actions/EventDeploy.js @@ -18,7 +18,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), diff --git a/lib/actions/EventDeployS3Lambda.js b/lib/actions/EventDeployS3Lambda.js index ebc992026..de613e32a 100644 --- a/lib/actions/EventDeployS3Lambda.js +++ b/lib/actions/EventDeployS3Lambda.js @@ -13,7 +13,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), BbPromise = require('bluebird'); diff --git a/lib/actions/EventDeploySNSLambda.js b/lib/actions/EventDeploySNSLambda.js index beb403f6c..90bd547fc 100644 --- a/lib/actions/EventDeploySNSLambda.js +++ b/lib/actions/EventDeploySNSLambda.js @@ -13,7 +13,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), BbPromise = require('bluebird'); diff --git a/lib/actions/EventDeployScheduledLambda.js b/lib/actions/EventDeployScheduledLambda.js index b65b9db94..40590bd27 100644 --- a/lib/actions/EventDeployScheduledLambda.js +++ b/lib/actions/EventDeployScheduledLambda.js @@ -13,7 +13,7 @@ module.exports = function (SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), BbPromise = require('bluebird'); diff --git a/lib/actions/EventDeployStreamLambda.js b/lib/actions/EventDeployStreamLambda.js index 2021dc1da..c9b4b2c64 100644 --- a/lib/actions/EventDeployStreamLambda.js +++ b/lib/actions/EventDeployStreamLambda.js @@ -14,7 +14,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), BbPromise = require('bluebird'); diff --git a/lib/actions/FunctionCreate.js b/lib/actions/FunctionCreate.js index 91ffab3b7..1b9cd1b99 100644 --- a/lib/actions/FunctionCreate.js +++ b/lib/actions/FunctionCreate.js @@ -13,7 +13,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), SUtils = require(path.join(serverlessPath, 'utils')); diff --git a/lib/actions/FunctionDeploy.js b/lib/actions/FunctionDeploy.js index b0308b540..98c2b8a22 100644 --- a/lib/actions/FunctionDeploy.js +++ b/lib/actions/FunctionDeploy.js @@ -12,7 +12,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), diff --git a/lib/actions/FunctionLogs.js b/lib/actions/FunctionLogs.js index b23558870..f1c577586 100644 --- a/lib/actions/FunctionLogs.js +++ b/lib/actions/FunctionLogs.js @@ -16,7 +16,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils/index')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), @@ -156,6 +156,7 @@ module.exports = function(SPlugin, serverlessPath) { return this.S.getProvider('aws') .request('Lambda', 'getAlias', params, this.evt.options.stage, this.evt.options.region) + .bind(this) .then(reply => this.evt.data.version = reply.FunctionVersion) .then(this._getLogStreams) .then( logStreams => { diff --git a/lib/actions/FunctionRun.js b/lib/actions/FunctionRun.js index 461488210..6cc91dd85 100644 --- a/lib/actions/FunctionRun.js +++ b/lib/actions/FunctionRun.js @@ -7,7 +7,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), BbPromise = require('bluebird'), chalk = require('chalk'), SCli = require( path.join( serverlessPath, 'utils', 'cli')); diff --git a/lib/actions/FunctionRunLambdaNodeJs.js b/lib/actions/FunctionRunLambdaNodeJs.js index 81fc146da..2f43959db 100644 --- a/lib/actions/FunctionRunLambdaNodeJs.js +++ b/lib/actions/FunctionRunLambdaNodeJs.js @@ -7,7 +7,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), diff --git a/lib/actions/FunctionRunLambdaPython2.js b/lib/actions/FunctionRunLambdaPython2.js index 721e94135..558bcaadb 100644 --- a/lib/actions/FunctionRunLambdaPython2.js +++ b/lib/actions/FunctionRunLambdaPython2.js @@ -6,7 +6,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SUtils = require(path.join(serverlessPath, 'utils')), spawnSync = require('child_process').spawnSync, SCli = require(path.join(serverlessPath, 'utils/cli')), diff --git a/lib/actions/ModuleInstall.js b/lib/actions/ModuleInstall.js index fb9eca01f..08218ad1f 100644 --- a/lib/actions/ModuleInstall.js +++ b/lib/actions/ModuleInstall.js @@ -14,7 +14,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), URL = require('url'), Download = require('download'), diff --git a/lib/actions/PluginCreate.js b/lib/actions/PluginCreate.js index a3df150f3..f84cecab4 100644 --- a/lib/actions/PluginCreate.js +++ b/lib/actions/PluginCreate.js @@ -12,7 +12,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), SUtils = require(path.join(serverlessPath, 'utils')), diff --git a/lib/actions/ProjectInit.js b/lib/actions/ProjectInit.js index 166bd168a..88ba9d91c 100644 --- a/lib/actions/ProjectInit.js +++ b/lib/actions/ProjectInit.js @@ -19,7 +19,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require( path.join( serverlessPath, 'ServerlessError' ) ), + SError = require( path.join( serverlessPath, 'Error' ) ), SCli = require( path.join( serverlessPath, 'utils/cli' ) ), SUtils = require( path.join( serverlessPath, 'utils' ) ), BbPromise = require('bluebird'), @@ -281,10 +281,8 @@ module.exports = function(SPlugin, serverlessPath) { _validateAndPrepare() { - this.S.getProvider('aws').getProfile(this.evt.options.profile); - // If Profile, extract API Keys - if (this.evt.options.profile) { + if (this.evt.options.profile && this.S.getProvider('aws').getProfile(this.evt.options.profile)) { this.S.config.awsAdminKeyId = this.S.getProvider('aws').getProfile(this.evt.options.profile).aws_access_key_id; this.S.config.awsAdminSecretKey = this.S.getProvider('aws').getProfile(this.evt.options.profile).aws_secret_access_key; } diff --git a/lib/actions/ProjectInstall.js b/lib/actions/ProjectInstall.js index 162329f2c..845b7a112 100644 --- a/lib/actions/ProjectInstall.js +++ b/lib/actions/ProjectInstall.js @@ -23,7 +23,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require( path.join( serverlessPath, 'ServerlessError' ) ), + SError = require( path.join( serverlessPath, 'Error' ) ), SCli = require( path.join( serverlessPath, 'utils/cli' ) ), SUtils = require( path.join( serverlessPath, 'utils' ) ), BbPromise = require('bluebird'), diff --git a/lib/actions/ProjectRemove.js b/lib/actions/ProjectRemove.js index b14796cab..2d03ac10b 100644 --- a/lib/actions/ProjectRemove.js +++ b/lib/actions/ProjectRemove.js @@ -11,7 +11,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), fs = require('fs'), BbPromise = require('bluebird'), @@ -85,7 +85,7 @@ usage: serverless project remove`, */ _removeAllStages() { -ยง let stages = this.S.getProject().getStages() + let stages = this.S.getProject().getStages() return BbPromise.each(stages, (stage) => { let evt = { diff --git a/lib/actions/RegionCreate.js b/lib/actions/RegionCreate.js index 943c4fa2c..a735fa696 100644 --- a/lib/actions/RegionCreate.js +++ b/lib/actions/RegionCreate.js @@ -17,7 +17,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), SUtils = require(path.join(serverlessPath, 'utils')); @@ -142,16 +142,15 @@ usage: serverless region create`, return BbPromise.reject(new SError('Region "' + _this.evt.options.region + '" already exists in stage "' + _this.evt.options.stage + '"')); } - // Update and save Meta - _this.meta = _this.S.state.getMeta(); + // Update and save Project + const project = _this.S.getProject() + const stage = project.getStage(_this.evt.options.stage); + const region = new _this.S.classes.Region(_this.S, stage, _this.evt.options.region) - _this.meta.stages[_this.evt.options.stage].regions[_this.evt.options.region] = { - variables: { - region: _this.evt.options.region - } - }; + stage.addRegion(region); + + return project.save(); - return _this.meta.save(); } /** @@ -173,8 +172,9 @@ usage: serverless region create`, // Create bucket, or skip if already exists const stage = this.evt.options.stage, - region = this.evt.options.region, - bucketName = this.S.state.getMeta().variables.projectBucket; + bucketName = this.S.state.getMeta().variables.projectBucket, + region = this.evt.options.region; + // region = bucketName.split('.')[1]; return this.S.getProvider('aws') .request('S3', 'getBucketAcl', { Bucket: bucketName }, stage, region) @@ -224,7 +224,7 @@ SERVERLESS_PROJECT_NAME=${projectName}`; let params = { Bucket: this.S.state.getMeta().variables.projectBucket, - Key: key, + Key: "/" + key, ACL: 'private', ContentType: 'text/plain', Body: envFileContents diff --git a/lib/actions/RegionRemove.js b/lib/actions/RegionRemove.js index 85a351157..631fc67f1 100644 --- a/lib/actions/RegionRemove.js +++ b/lib/actions/RegionRemove.js @@ -16,7 +16,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), fs = BbPromise.promisifyAll(require('fs')), @@ -149,13 +149,13 @@ usage: serverless region remove`, _listS3Objects() { SUtils.sDebug("List related S3 objects"); - let prefix = ['serverless', this.S.getProject().name, this.evt.options.stage, this.evt.options.region].join('/'), + let prefix = ['serverless', this.S.getProject().getName(), this.evt.options.stage, this.evt.options.region].join('/'), params = { Bucket: this.S.state.getMeta().variables.projectBucket, Prefix: prefix }; - return this.S.getProvider('aws').request('S3', 'listObjects', params, stage, region) - .then(reply => {return _.map(reply.Contents, (item) => ({Key: item.Key}) )}); + return this.S.getProvider('aws').request('S3', 'listObjects', params, this.evt.options.stage, this.evt.options.region) + .then(reply => _.map(reply.Contents, (item) => ({Key: item.Key}))); } _removeS3Objects(objects) { @@ -168,7 +168,7 @@ usage: serverless region remove`, Objects: objects } }; - return this.S.getProvider('aws').request('S3', 'deleteObjects', params, stage, region); + return this.S.getProvider('aws').request('S3', 'deleteObjects', params, this.evt.options.stage, this.evt.options.region); } else { SUtils.sDebug("S3 objects are not found. Skipping."); return BbPromise.resolve(); @@ -177,9 +177,8 @@ usage: serverless region remove`, _removeMeta() { // Update and save Meta - let meta = this.S.state.getMeta(); - delete meta.stages[this.evt.options.stage].regions[this.evt.options.region] - return meta.save(); + this.S.getProject().getStage(this.evt.options.stage).removeRegion(this.evt.options.region); + return this.S.getProject().save(); } _removeVariables() { diff --git a/lib/actions/ResourcesDeploy.js b/lib/actions/ResourcesDeploy.js index 81f67cd35..36a74ea83 100644 --- a/lib/actions/ResourcesDeploy.js +++ b/lib/actions/ResourcesDeploy.js @@ -9,7 +9,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), replaceall = require('replaceall'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), _ = require('lodash'), @@ -156,7 +156,7 @@ usage: serverless resources deploy`, _deployResources() { let _this = this; - let regionVars = _this.S.state.getMeta().stages[_this.evt.options.stage].regions[_this.evt.options.region].variables; + let regionVars = _this.S.getProject().getRegion(_this.evt.options.stage, _this.evt.options.region)._variables let resourceVars = [ 'iamRoleArnLambda' ].concat( _this.S.getProject().resourceVars); return _this.S.getProject().getResources({ @@ -262,7 +262,7 @@ usage: serverless resources deploy`, stage = this.evt.options.stage, region = this.evt.options.region, aws = this.S.getProvider('aws'), - stackName = this.S.state.getMeta().stages[stage].regions[region].variables.resourcesStackName || aws.getLambdasStackName(stage, projectName); + stackName = this.S.getProject().getRegion(stage, region)._variables.resourcesStackName || aws.getLambdasStackName(stage, projectName); // CF Params let params = { diff --git a/lib/actions/ResourcesDiff.js b/lib/actions/ResourcesDiff.js index e229b9986..c819549bb 100644 --- a/lib/actions/ResourcesDiff.js +++ b/lib/actions/ResourcesDiff.js @@ -13,7 +13,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), fs = BbPromise.promisifyAll(require('fs')), diff --git a/lib/actions/ResourcesRemove.js b/lib/actions/ResourcesRemove.js index 27967bc03..086abb5ca 100644 --- a/lib/actions/ResourcesRemove.js +++ b/lib/actions/ResourcesRemove.js @@ -8,7 +8,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), SUtils = require(path.join(serverlessPath, 'utils/index')), @@ -157,7 +157,7 @@ usage: serverless resources remove`, let _this = this, stage = _this.evt.options.stage, region = _this.evt.options.region, - regionVars = _this.S.state.getMeta().stages[stage].regions[region].variables, + regionVars = _this.S.getProject().getRegion(stage, region)._variables, projectBucket = _this.S.state.getMeta().variables.projectBucket, projectName = _this.S.getProject().getName(), aws = _this.S.getProvider('aws'); diff --git a/lib/actions/StageCreate.js b/lib/actions/StageCreate.js index dd1f06fab..53ad4570b 100644 --- a/lib/actions/StageCreate.js +++ b/lib/actions/StageCreate.js @@ -14,7 +14,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), os = require('os'), fs = require('fs'), @@ -182,21 +182,25 @@ usage: serverless stage create`, */ _createStage() { + const project = this.S.getProject(); + const stage = new this.S.classes.Stage(this.S, project, this.evt.options.stage); - let _this = this; + project.addStage(stage); - // Update Meta - _this.meta = _this.S.state.getMeta(); + return project.save(); - _this.meta.stages[_this.evt.options.stage] = { - regions: {}, - variables: { - stage: _this.evt.options.stage - } - }; + // // Update Meta + // _this.meta = _this.S.state.getMeta(); + + // _this.meta.stages[_this.evt.options.stage] = { + // regions: {}, + // variables: { + // stage: _this.evt.options.stage + // } + // }; // Save Meta before adding region - return _this.meta.save(); + // return _this.meta.save(); } /** diff --git a/lib/actions/StageRemove.js b/lib/actions/StageRemove.js index 77209b1a0..588320cab 100644 --- a/lib/actions/StageRemove.js +++ b/lib/actions/StageRemove.js @@ -11,7 +11,7 @@ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), - SError = require(path.join(serverlessPath, 'ServerlessError')), + SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), fs = require('fs'), BbPromise = require('bluebird'), diff --git a/lib/utils/index.js b/lib/utils/index.js index 439fd2453..141c03829 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -299,14 +299,16 @@ exports.doesFunctionExist = function(functionName, componentName, projectRootPat exports.populate = function(meta, templates, data, stage, region) { + const project = meta._S.getProject() + // Validate required params if (!meta || !templates || !data || !stage || !region) throw new SError(`Missing required params: Serverless, project, stage, region`); // Validate: Check stage exists - if (typeof stage != 'undefined' && !meta.stages[stage]) throw new SError(`Stage doesn't exist`); + if (typeof stage != 'undefined' && !project.validateStageExists(stage)) throw new SError(`Stage doesn't exist`); // Validate: Check region exists in stage - if (typeof region != 'undefined' && !meta.stages[stage].regions[region]) throw new SError(`Region doesn't exist in provided stage`); + if (typeof region != 'undefined' && !project.validateRegionExists(stage, region)) throw new SError(`Region doesn't exist in provided stage`); // Sanitize: Remove nested properties. DO NOT populate these. Rely on calling those classes getPopulated methods instead. if (data.components) delete data.components; @@ -316,12 +318,12 @@ exports.populate = function(meta, templates, data, stage, region) { let varTemplateSyntax = /\${([\s\S]+?)}/g, templateTemplateSyntax = /\$\${([\s\S]+?)}/g; - if (meta._S.getProject().variableSyntax) { - varTemplateSyntax = RegExp(meta._S.getProject().variableSyntax,'g'); + if (project.variableSyntax) { + varTemplateSyntax = RegExp(project.variableSyntax,'g'); } - if (meta._S.getProject().templateSyntax) { - templateTemplateSyntax = RegExp(meta._S.getProject().templateSyntax,'g'); + if (project.templateSyntax) { + templateTemplateSyntax = RegExp(project.templateSyntax,'g'); } // Populate templates @@ -362,10 +364,10 @@ exports.populate = function(meta, templates, data, stage, region) { let variableName = variableSyntax.replace(varTemplateSyntax, (match, varName) => varName.trim()); let value; - if (meta.stages[stage].regions[region].variables[variableName]) { - value = meta.stages[stage].regions[region].variables[variableName] - } else if (meta.stages[stage].variables[variableName]) { - value = meta.stages[stage].variables[variableName]; + if (project.getRegion(stage, region)._variables[variableName]) { + value = project.getRegion(stage, region)._variables[variableName] + } else if (project.getStage(stage)._variables[variableName]) { + value = project.getStage(stage)._variables[variableName]; } else if (meta.variables[variableName]) { value = meta.variables[variableName]; } diff --git a/tests/tests/actions/ProjectInit.js b/tests/tests/actions/ProjectInit.js index aaced21ff..91376cfa7 100644 --- a/tests/tests/actions/ProjectInit.js +++ b/tests/tests/actions/ProjectInit.js @@ -88,7 +88,7 @@ let cleanup = function(Meta, cb, evt) { // Delete CloudFormation Resources Stack let cloudformation = new AWS.CloudFormation(); cloudformation.deleteStack({ - StackName: Meta.stages[config.stage].regions[config.region].variables.resourcesStackName + StackName: serverless.getProject().getRegion(config.stage, config.region)._variables.resourcesStackName }, function (err, data) { if (err) console.log(err, err.stack); // an error occurred @@ -140,14 +140,17 @@ describe('Test action: Project Init', function() { // Validate Meta let Meta = serverless.state.getMeta(); + let stage = serverless.getProject().getStage(config.stage); + let region = serverless.getProject().getRegion(config.stage, config.region); + assert.equal(true, typeof Meta.variables.project != 'undefined'); assert.equal(true, typeof Meta.variables.domain != 'undefined'); assert.equal(true, typeof Meta.variables.projectBucket != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage].variables.stage != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region].variables.region != 'undefined'); + assert.equal(true, typeof stage._variables.stage != 'undefined'); + assert.equal(true, typeof region._variables.region != 'undefined'); if (!config.noExecuteCf) { - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region].variables.iamRoleArnLambda != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region].variables.resourcesStackName != 'undefined'); + assert.equal(true, typeof region._variables.iamRoleArnLambda != 'undefined'); + assert.equal(true, typeof region._variables.resourcesStackName != 'undefined'); } // Validate Event diff --git a/tests/tests/actions/ProjectInstall.js b/tests/tests/actions/ProjectInstall.js index fded5d0b9..6716b98b3 100644 --- a/tests/tests/actions/ProjectInstall.js +++ b/tests/tests/actions/ProjectInstall.js @@ -140,15 +140,18 @@ describe('Test action: Project Install', function() { // Validate Meta let Meta = serverless.state.getMeta(); + let stage = serverless.getProject().getStage(config.stage); + let region = serverless.getProject().getRegion(config.stage, config.region); + assert.equal(true, typeof Meta.variables.project != 'undefined'); assert.equal(true, typeof Meta.variables.domain != 'undefined'); assert.equal(true, typeof Meta.variables.projectBucket != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage].variables.stage != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region].variables.region != 'undefined'); + assert.equal(true, typeof stage._variables.stage != 'undefined'); + assert.equal(true, typeof region._variables.region != 'undefined'); if (!config.noExecuteCf) { - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region].variables.iamRoleArnLambda != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region].variables.resourcesStackName != 'undefined'); + assert.equal(true, typeof region._variables.iamRoleArnLambda != 'undefined'); + assert.equal(true, typeof region._variables.resourcesStackName != 'undefined'); } // Validate Event diff --git a/tests/tests/actions/RegionCreate.js b/tests/tests/actions/RegionCreate.js index 48338a563..5cfa45304 100644 --- a/tests/tests/actions/RegionCreate.js +++ b/tests/tests/actions/RegionCreate.js @@ -105,7 +105,8 @@ describe('Test Action: Region Create', function() { let Meta = serverless.state.meta; //console.log(serverless.state.meta.stages) - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region2].variables.region != 'undefined'); + + assert.equal(true, typeof serverless.getProject().getRegion(config.stage, config.region2)._variables.region != 'undefined'); // Validate Event validateEvent(evt); diff --git a/tests/tests/actions/StageCreate.js b/tests/tests/actions/StageCreate.js index f77330c41..88f912459 100644 --- a/tests/tests/actions/StageCreate.js +++ b/tests/tests/actions/StageCreate.js @@ -97,8 +97,9 @@ describe('Test Action: Stage Create', function() { .then(function(evt) { let Meta = serverless.state.meta; - assert.equal(true, typeof Meta.stages[config.stage2].variables.stage != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage2].regions[config.region].variables.region != 'undefined'); + let project = serverless.getProject(); + assert.equal(true, typeof project.getStage(config.stage2)._variables.stage != 'undefined'); + assert.equal(true, typeof project.getRegion(config.stage2, config.region)._variables.region != 'undefined'); // Validate EVT validateEvent(evt); diff --git a/tests/tests/actions/projectLifeCycle.js b/tests/tests/actions/projectLifeCycle.js index 0e56e6c2c..c6ba7cb2b 100644 --- a/tests/tests/actions/projectLifeCycle.js +++ b/tests/tests/actions/projectLifeCycle.js @@ -116,14 +116,18 @@ describe('Test: Project Live Cycle', function() { // Validate Meta let Meta = serverless.state.getMeta(); + let stage = serverless.getProject().getStage(config.stage); + let region = serverless.getProject().getRegion(config.stage, config.region); + + assert.equal(true, typeof Meta.variables.project != 'undefined'); assert.equal(true, typeof Meta.variables.domain != 'undefined'); assert.equal(true, typeof Meta.variables.projectBucket != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage].variables.stage != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region].variables.region != 'undefined'); + assert.equal(true, typeof stage._variables.stage != 'undefined'); + assert.equal(true, typeof region._variables.region != 'undefined'); if (!config.noExecuteCf) { - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region].variables.iamRoleArnLambda != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage].regions[config.region].variables.resourcesStackName != 'undefined'); + assert.equal(true, typeof region._variables.iamRoleArnLambda != 'undefined'); + assert.equal(true, typeof region._variables.resourcesStackName != 'undefined'); } // Validate Event @@ -156,8 +160,11 @@ describe('Test: Project Live Cycle', function() { .then(function(evt) { let Meta = serverless.state.meta; - assert.equal(true, typeof Meta.stages[config.stage2].variables.stage != 'undefined'); - assert.equal(true, typeof Meta.stages[config.stage2].regions[config.region].variables.region != 'undefined'); + let stage = serverless.getProject().getStage(config.stage2); + let region = serverless.getProject().getRegion(config.stage2, config.region); + + assert.equal(true, typeof stage._variables.stage != 'undefined'); + assert.equal(true, typeof region._variables.region != 'undefined'); // Validate EVT validateEvent(evt); @@ -188,8 +195,7 @@ describe('Test: Project Live Cycle', function() { return serverless.actions.regionCreate(evt) .then(function(evt) { - let Meta = serverless.state.meta; - assert.equal(true, typeof Meta.stages[config.stage2].regions[config.region2].variables.region != 'undefined'); + assert.equal(true, typeof serverless.getProject().getRegion(config.stage2, config.region2)._variables.region != 'undefined'); // Validate Event validateEvent(evt); diff --git a/tests/tests/classes/ServerlessProjectTest.js b/tests/tests/classes/ServerlessProjectTest.js index d5d75c447..b282d2912 100644 --- a/tests/tests/classes/ServerlessProjectTest.js +++ b/tests/tests/classes/ServerlessProjectTest.js @@ -31,7 +31,6 @@ describe('Test Serverless Project Class', function() { return serverless.init() .then(function() { - // Instantiate Class instance = serverless.getProject(); @@ -78,7 +77,7 @@ describe('Test Serverless Project Class', function() { // These functions have their own s-templates.json files which give them the same template, with one different property // Function1 template - assert.equal(true, data.components.nodejscomponent.functions['nodejscomponent/group1/function1'].endpoints[0].requestTemplates['application/json'].pathParams === "$input.path('$.id1')"); + assert.equal(data.components.nodejscomponent.functions['nodejscomponent/group1/function1'].endpoints[0].requestTemplates['application/json'].pathParams, "$input.path('$.id1')"); // Function2 template assert.equal(true, data.components.nodejscomponent.functions['nodejscomponent/group1/function2'].endpoints[0].requestTemplates['application/json'].pathParams === "$input.path('$.id2')"); // Function3 template - s-templates.json left undefined