From 8f9ea9773d77eeb2a9ba658ef9e386b94cd8b87f Mon Sep 17 00:00:00 2001 From: Kamil Burzynski Date: Thu, 11 Feb 2016 19:23:37 +0100 Subject: [PATCH] Moved getEvents() to project. Extraced Project._filterSPaths --- lib/ServerlessFunction.js | 4 ++ lib/ServerlessProject.js | 68 ++++++++++--------- lib/ServerlessState.js | 70 +++----------------- lib/actions/EventDeploy.js | 6 +- lib/actions/EventDeployS3Lambda.js | 2 +- lib/actions/EventDeploySNSLambda.js | 2 +- lib/actions/EventDeployScheduledLambda.js | 2 +- lib/actions/EventDeployStreamLambda.js | 2 +- tests/tests/classes/ServerlessProjectTest.js | 18 +++++ tests/tests/classes/ServerlessStateTest.js | 19 ------ 10 files changed, 76 insertions(+), 117 deletions(-) diff --git a/lib/ServerlessFunction.js b/lib/ServerlessFunction.js index 3cb1c39a9..5bbf8e235 100644 --- a/lib/ServerlessFunction.js +++ b/lib/ServerlessFunction.js @@ -399,6 +399,10 @@ class ServerlessFunction { return component.getRuntime(); } + getAllEvents() { + return this.events; + } + /** * Get Project * - Returns reference to the instance diff --git a/lib/ServerlessProject.js b/lib/ServerlessProject.js index d780b0080..990a7f464 100644 --- a/lib/ServerlessProject.js +++ b/lib/ServerlessProject.js @@ -403,6 +403,24 @@ class ServerlessProject { }) } + _filterSPaths( collection, options ) { + // TODO: remove this, caller should be filtering such stuff later + let paths = _.get( options, 'paths' ); + if( paths && (paths.length > 0) ) { + collection = _.filter( collection, f => + _.any( paths, p => + p.indexOf( f.getSPath() ) !== -1; + ) + ) + } + + if( _.get( options, 'returnPaths' ) == true ){ + collection = _.map( collection, func => func.getSPath(); ); + } + + return collection; + } + /** * Get Functions * - Returns an array of this project's function instances @@ -412,25 +430,9 @@ class ServerlessProject { */ getAllFunctions(options) { - let allFunctions = _.flatten( _.map( _.values( this.components ), component => + return this._filterSPaths( _.flatten( _.map( _.values( this.components ), component => component.getAllFunctions(); - )); - - // TODO: remove this, caller should be filtering such stuff later - let paths = _.get( options, 'paths' ); - if( paths ) { - allFunctions = _.filter( allFunctions, f => - _.any( paths, p => - p.indexOf( f.getSPath() ) !== -1; - ) - ) - } - - if( _.get( options, 'returnPaths' ) == true ){ - return _.map( allFunctions, func => func.getSPath(); ); - } - - return allFunctions; + )), options ); } getFunction( path ){ @@ -447,18 +449,7 @@ class ServerlessProject { */ getAllComponents(options){ - let c = _.values( this.components ); - let paths = _.get( options, 'paths' ); - - if( paths ) { - c = _.filter( c, component => - _.any( paths, p => - p.indexOf( component.getSPath() ) !== -1; - ) - ) - } - - return c; + return this._filterSPaths( this.components, options ); } getComponent( path ){ @@ -466,6 +457,23 @@ class ServerlessProject { c.getSPath().indexOf( path ) !== -1; ) } + + /** + * Get Events + * - Returns an array of this state's Events instances + * - Options: paths, component, module, function, event name + * - options.paths is an array of Serverless paths like this: ['component/module/function#eventName'] + */ + + getEvents(options) { + return this._filterSPaths( _.flatten( _.map( this.getAllFunctions(), f => f.getAllEvents() ) ), options ); + } + + getEvent( path ){ + return _.find( _.values( this.getAllEvents() ), e => + e.getSPath().indexOf( path ) !== -1; + ) + } } module.exports = ServerlessProject; \ No newline at end of file diff --git a/lib/ServerlessState.js b/lib/ServerlessState.js index 15d5cbb97..0ffc5ccd7 100644 --- a/lib/ServerlessState.js +++ b/lib/ServerlessState.js @@ -178,6 +178,9 @@ class ServerlessState { allEndpoints = [], foundEndpoints = []; + // Default options.paths + if (options && !options.paths) options.paths = []; + // Get all functions for (let i = 0; i < Object.keys(_this._S.getProject().components).length; i++) { let component = _this._S.getProject().components[Object.keys(_this._S.getProject().components)[i]]; @@ -190,12 +193,12 @@ class ServerlessState { } } - // Default options.paths - options = options ? options : {}; - options.paths = options.paths ? options.paths : []; + // Return if no options specified + if (!options) return allEndpoints; + if (options && Object.keys(options).length === 1 && options.returnPaths === true) return allEndpoints.map(function(d) { return d._config.sPath }); // XXX // If component, module or functions convert to sPath - // TODO: Back Compat Support -- Eventually remove and support sPath only + // TODO: Eventually remove and support sPath only if (options.component) { options.paths = [options.component]; if (options.module) options.paths[0] = options.paths[0] + '/' + options.module; @@ -204,18 +207,13 @@ class ServerlessState { if (options.endpointMethod) options.paths[0] = options.paths[0] + '~' + options.endpointMethod; } - // Return if no paths - if (!options.paths.length) { - return options.returnPaths ? allEndpoints.map(function(d) { return d._config.sPath }) : allEndpoints; - } - // If options specified, loop through functions and find the ones specified for (let i = 0; i < allEndpoints.length; i++) { let endpoint = allEndpoints[i]; for (let j = 0; j < options.paths.length; j++) { - if (endpoint._config.sPath.indexOf(options.paths[j]) !== -1) { - foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint); + if (endpoint._config.sPath.indexOf(options.paths[j]) !== -1) { // XXX + foundEndpoints.push(options.returnPaths ? endpoint._config.sPath : endpoint); // XXX break; } } @@ -236,56 +234,6 @@ class ServerlessState { return foundEndpoints; } - /** - * Get Events - * - Returns an array of this state's Events instances - * - Options: paths, component, module, function, event name - * - options.paths is an array of Serverless paths like this: ['component/module/function#eventName'] - */ - - getEvents(options) { - - let _this = this, - allFunctions = [], - allEvents = [], - foundEvents = []; - - // If options.component, options.module or options.function, throw error - if (options && (options.component || options.module || options.function)) { - throw new SError('options.component, options.module, options.function is not supported.'); - } - - // Get all events - allFunctions = this.getFunctions(); - for (let i = 0; i < allFunctions.length; i++) { - if (allFunctions[i].events) { - allEvents = allEvents.concat(allFunctions[i].events); - } - } - - // Default options.paths - options = options ? options : {}; - options.paths = options.paths ? options.paths : []; - - // Return if no paths - if (!options.paths.length) { - return options.returnPaths ? allEvents.map(function(d) { return d._config.sPath }) : allEvents; - } - - // If options specified, loop through functions and find the ones specified - for (let i = 0; i < allEvents.length; i++) { - let event = allEvents[i]; - for (let j = 0; j < options.paths.length; j++) { - if (event._config.sPath.indexOf(options.paths[j]) !== -1) { - foundEvents.push(options.returnPaths ? event._config.sPath : event); - break; - } - } - } - - return foundEvents; - } - /** * Validate Stage Exists * - Checks to see if a stage exists in your project diff --git a/lib/actions/EventDeploy.js b/lib/actions/EventDeploy.js index e083f66fb..41ff71c81 100644 --- a/lib/actions/EventDeploy.js +++ b/lib/actions/EventDeploy.js @@ -163,7 +163,7 @@ module.exports = function(SPlugin, serverlessPath) { throw new SError(`You must be in a component to deploy events`); } - _this.evt.options.paths = _this.S.state.getEvents({ + _this.evt.options.paths = _this.S.getProject().getEvents({ paths: [sPath], returnPaths: true }); @@ -177,7 +177,7 @@ module.exports = function(SPlugin, serverlessPath) { // If --all is selected, load all paths if (_this.evt.options.all) { - _this.evt.options.paths = _this.S.state.getEvents({ returnPaths: true }); + _this.evt.options.paths = _this.S.getProject().getEvents({ returnPaths: true }); } // Validate Stage @@ -231,7 +231,7 @@ module.exports = function(SPlugin, serverlessPath) { async.eachSeries(_this.evt.options.paths, function(path, eCb) { - let event = _this.S.state.getEvents({ paths: [path] })[0]; + let event = _this.S.getProject().getEvent( path ); if(!event) throw new SError(`Event could not be found: ${path}`); diff --git a/lib/actions/EventDeployS3Lambda.js b/lib/actions/EventDeployS3Lambda.js index d36d317c6..0883fe648 100644 --- a/lib/actions/EventDeployS3Lambda.js +++ b/lib/actions/EventDeployS3Lambda.js @@ -49,7 +49,7 @@ module.exports = function(SPlugin, serverlessPath) { if (!_this.evt.options.stage || !_this.evt.options.region || !_this.evt.options.path) { return BbPromise.reject(new SError(`Missing stage, region or path.`)); } - let event = _this.S.state.getEvents({ paths: [_this.evt.options.path] })[0], + let event = _this.S.getProject().getEvent( _this.evt.options.path ), populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}), functionName = _this.S.getProject().getFunction( _this.evt.options.path.split('#')[0] ).getDeployedName(_this.evt.options), statementId = 'sEvents-' + functionName + '-' + event.name + '-' + _this.evt.options.stage, diff --git a/lib/actions/EventDeploySNSLambda.js b/lib/actions/EventDeploySNSLambda.js index c5cb39f48..f52826a69 100644 --- a/lib/actions/EventDeploySNSLambda.js +++ b/lib/actions/EventDeploySNSLambda.js @@ -50,7 +50,7 @@ module.exports = function(SPlugin, serverlessPath) { return BbPromise.reject(new SError(`Missing stage, region or path.`)); } - let event = _this.S.state.getEvents({ paths: [_this.evt.options.path] })[0], + let event = _this.S.getProject().getEvent( _this.evt.options.path ), populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}), functionName = _this.S.getProject().getFunction( _this.evt.options.path.split('#')[0] ).getDeployedName(_this.evt.options), statementId = 'sEvents-' + functionName + '-' + event.name + '-' + _this.evt.options.stage, diff --git a/lib/actions/EventDeployScheduledLambda.js b/lib/actions/EventDeployScheduledLambda.js index 2e05327cf..e7daa3c14 100644 --- a/lib/actions/EventDeployScheduledLambda.js +++ b/lib/actions/EventDeployScheduledLambda.js @@ -49,7 +49,7 @@ module.exports = function (SPlugin, serverlessPath) { if (!_this.evt.options.stage || !_this.evt.options.region || !_this.evt.options.path) { return BbPromise.reject(new SError(`Missing stage, region or path.`)); } - let event = _this.S.state.getEvents({paths: [_this.evt.options.path]})[0], + let event = _this.S.getProject().getEvent( _this.evt.options.path ), populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}), functionName = _this.S.getProject().getFunction( _this.evt.options.path.split('#')[0] ).getDeployedName(_this.evt.options), ruleName = functionName + '-' + event.name, diff --git a/lib/actions/EventDeployStreamLambda.js b/lib/actions/EventDeployStreamLambda.js index fa83150d0..4c5c35d03 100644 --- a/lib/actions/EventDeployStreamLambda.js +++ b/lib/actions/EventDeployStreamLambda.js @@ -59,7 +59,7 @@ module.exports = function(SPlugin, serverlessPath) { _this.Lambda = require('../utils/aws/Lambda')(awsConfig); - let event = _this.S.state.getEvents({ paths: [_this.evt.options.path] })[0], + let event = _this.S.getProject().getEvent( _this.evt.options.path ), populatedEvent = event.getPopulated({stage: _this.evt.options.stage, region: _this.evt.options.region}), functionName = _this.S.getProject().getFunction( _this.evt.options.path.split('#')[0] ).getDeployedName(_this.evt.options), regionVars = _this.S.state.getMeta().stages[_this.evt.options.stage].regions[_this.evt.options.region].variables, diff --git a/tests/tests/classes/ServerlessProjectTest.js b/tests/tests/classes/ServerlessProjectTest.js index d5b7fde62..423a9d766 100644 --- a/tests/tests/classes/ServerlessProjectTest.js +++ b/tests/tests/classes/ServerlessProjectTest.js @@ -135,6 +135,24 @@ describe('Test Serverless Project Class', function() { done(); }); + it('Get events w/o paths', function(done) { + let events = instance.getEvents(); + assert.equal(true, events.length === 4); + done(); + }); + + it('Get events w paths', function(done) { + let events = instance.getEvents({ paths: ['nodejscomponent/group1/function1#s3'] }); + assert.equal(true, events.length === 1); + done(); + }); + + it('Get events w partial paths', function(done) { + let events = instance.getEvents({ paths: ['nodejscomponent/group1'] }); + assert.equal(true, events.length === 4); + done(); + }); + it('Create new and save', function(done) { // TODO: Project creation is an unholy mess now. It currently is done partially outside of Project class, // split between ServerlessState and Meta classes, ProjectInit action, and ServerlessProject itself. diff --git a/tests/tests/classes/ServerlessStateTest.js b/tests/tests/classes/ServerlessStateTest.js index ac3cc062f..b07adb8bc 100644 --- a/tests/tests/classes/ServerlessStateTest.js +++ b/tests/tests/classes/ServerlessStateTest.js @@ -170,25 +170,6 @@ describe('Test Serverless State Class', function() { done(); }); - // asfasf - it('Get events w/o paths', function(done) { - let events = instance.getEvents(); - assert.equal(true, events.length === 4); - done(); - }); - - it('Get events w paths', function(done) { - let events = instance.getEvents({ paths: ['nodejscomponent/group1/function1#s3'] }); - assert.equal(true, events.length === 1); - done(); - }); - - it('Get events w partial paths', function(done) { - let events = instance.getEvents({ paths: ['nodejscomponent/group1'] }); - assert.equal(true, events.length === 4); - done(); - }); - it('Validate stage exists', function(done) { assert.equal(true, instance.validateStageExists(config.stage)); assert.equal(false, instance.validateStageExists('invalid'));