diff --git a/lib/Serverless.js b/lib/Serverless.js index 9a3a3a19b..c429fec5c 100644 --- a/lib/Serverless.js +++ b/lib/Serverless.js @@ -45,10 +45,11 @@ class Serverless { this.hooks = {}; this.commands = {}; this.classes = { - Meta: require('./ServerlessMeta'), - Project: require('./ServerlessProject'), - Module: require('./ServerlessModule'), - Function: require('./ServerlessFunction') + Meta: require('./ServerlessMeta'), + Project: require('./ServerlessProject'), + Component: require('./ServerlessComponent'), + Module: require('./ServerlessModule'), + Function: require('./ServerlessFunction') }; this.cli = null; @@ -312,6 +313,59 @@ class Serverless { return newEvt; } + + /** + * Build sPath + */ + + buildPath(data) { + let path = ''; + if (data.component) path = path + data.component.trim(); + if (data.module) path = path + '/' + data.module.trim(); + if (data.function) path = path + '/' + data.function.trim(); + if (data.urlPath) path = path + '@' + data.urlPath.trim(); + if (data.urlMethod) path = path + '~' + data.urlMethod.trim(); + return path; + } + + /** + * Parse sPath + */ + + parsePath(path) { + let parsed = {}; + parsed.component = path.split('/')[0] || null; + parsed.module = path.split('/')[1] || null; + parsed.func = path.split('/')[2] ? path.split('/')[2].split('@')[0] : null; + parsed.urlPath = path.split('@')[1] ? path.split('@')[1].split('~')[0] : null; + parsed.urlMethod = path.split('~')[1] || null; + return parsed; + } + + /** + * Validate sPath + */ + + validatePath(path, type) { + if (type.indexOf('component') > -1) { + if (!path) throw new SError('Invalid path'); + } else if (type.indexOf('module') > -1) { + let pathArray = path.split('/'); + if (!pathArray[0] || !pathArray[1] || pathArray[2] || path.indexOf('@') > -1 || path.indexOf('~') > -1) { + throw new SError('Invalid path'); + } + } else if (type.indexOf('function') > -1) { + let pathArray = path.split('/'); + if (!pathArray[0] || !pathArray[1] || !pathArray[2] || path.indexOf('@') > -1 || path.indexOf('~') > -1) { + throw new SError('Invalid path'); + } + } else if (type.indexOf('endpoint') > -1) { + if (!pathArray[0] || !pathArray[1] || !pathArray[2] || path.indexOf('@') == -1 || path.indexOf('~') == -1) { + throw new SError('Invalid path'); + } + } + } + } module.exports = Serverless; diff --git a/lib/ServerlessComponent.js b/lib/ServerlessComponent.js new file mode 100644 index 000000000..afd6dfd6f --- /dev/null +++ b/lib/ServerlessComponent.js @@ -0,0 +1,153 @@ +'use strict'; + +/** + * Serverless Component Class + */ + +const SError = require('./ServerlessError'), + SUtils = require('./utils/index'), + extend = require('util')._extend, + path = require('path'), + _ = require('lodash'), + fs = require('fs'), + BbPromise = require('bluebird'); + +class ServerlessComponent { + + /** + * Constructor + */ + + constructor(Serverless, config) { + this.S = Serverless; + this.config = {}; + this.updateConfig(config); + this.load(); + } + + /** + * Update Config + * - Takes config.sPath or config.component + */ + + updateConfig(config) { + if (config) { + // If specific options, create sPath + if (config.component) this.config.sPath = config.buildPath({ + component: config.component + }); + // If sPath, validate + if (config.sPath) { + this.S.validatePath(this.config.sPath, 'component'); + this.config.sPath = config.sPath; + } + // Make full path + if (this.S.config.projectPath && this.config.sPath) { + let parse = this.S.parsePath(this.config.sPath); + this._fullPath = path.join(this.S.config.projectPath, parse.component); + } + } + } + + /** + * Load + * - Load from source (i.e., file system); + */ + + load() { + + let _this = this; + + // Defaults + _this.data = {}; + _this.data.name = 'serverless' + SUtils.generateShortId(6); + _this.data.runtime = '0.0.1'; + + // If paths, check if this is on the file system + if (!_this.S.config.projectPath || + !_this._fullPath || + !SUtils.fileExistsSync(path.join(_this._fullPath, 's-component.json'))) return; + + // Get Component JSON + let component = SUtils.readAndParseJsonSync(path.join(_this._fullPath, 's-component.json')); + + // Add Modules & Functions + component.modules = {}; + let componentContents = fs.readdirSync(path.join(_this._fullPath, 'modules')); + + // Check folders to see which is a module + for (let i = 0; i < componentContents.length; i++) { + if (SUtils.fileExistsSync(path.join(_this._fullPath, componentContents[i], 's-module.json'))) { + let module = new this.S.classes.Module(_this.S, { module: componentContents[i] }); + module = module.get(); + component.modules[module.name] = module; + } + } + + // Add to data + _this = extend(_this.data, component); + } + + /** + * Get + * - Returns clone of data + */ + + get() { + return JSON.parse(JSON.stringify(this.data)); + } + + /** + * getPopulated + * - Fill in templates then variables + */ + + getPopulated(options) { + + options = options || {}; + + // Required: Stage & Region + if (!options.stage || !options.region) throw new SError('Both "stage" and "region" params are required'); + + // Required: Project Path + if (!this.S.config.projectPath) throw new SError('Project path must be set on Serverless instance'); + + // Return + return SUtils.populate(this.S, this.get(), options.stage, options.region); + } + + /** + * save + * - Saves data to file system + */ + + save(options) { + + let _this = this; + + // Validate path + if (!_this._fullPath) throw new SError('A Serverless path must be set to save to a location'); + + // Save JSON file + fs.writeFileSync(path.join( + _this._fullPath, + 's-component.json'), + JSON.stringify(this.data, null, 2)); + + // Save all nested data + if (options.deep) { + + // Loop over functions and save + Object.keys(_this.data.modules).forEach(function (moduleName) { + + let module = new _this.S.classes.Module(_this.S, { + sPath: _this.config.component + '/' + moduleName + }); + module.data = Object.create(_this.data.modules[moduleName]); + module.save(); + }); + } + } +} + +module.exports = ServerlessProject; \ No newline at end of file diff --git a/lib/ServerlessFunction.js b/lib/ServerlessFunction.js index 01a006ce7..e189f6555 100644 --- a/lib/ServerlessFunction.js +++ b/lib/ServerlessFunction.js @@ -6,12 +6,12 @@ */ const SError = require('./ServerlessError'), - SUtils = require('./utils/index'), - extend = require('util')._extend, - path = require('path'), - fs = require('fs'), - _ = require('lodash'), - BbPromise = require('bluebird'); + SUtils = require('./utils/index'), + extend = require('util')._extend, + path = require('path'), + fs = require('fs'), + _ = require('lodash'), + BbPromise = require('bluebird'); class ServerlessFunction { @@ -72,13 +72,13 @@ class ServerlessFunction { } ]; - if (_this.options.module && _this.options.function) { + if (_this.options.component && + _this.options.module && + _this.options.function) { _this.options.functionPath = path.join( _this.S.config.projectPath, - 'back', - 'modules', + _this.options.component, _this.options.module, - 'functions', _this.options.function); } @@ -88,10 +88,11 @@ class ServerlessFunction { let functionJson = SUtils.readAndParseJsonSync(path.join(_this.options.functionPath, 's-function.json')); _this.data = extend(_this.data, functionJson); - - // Add Module Name - _this.module = _this.options.module; - _this.sPath = path.posix.join(_this.options.module, _this.options.function); + + // Add Useful Data + _this.component = _this.options.component; + _this.module = _this.options.module; + _this.sPath = _this.options.component + '/' + _this.options.module + '/' + _this.options.function; } /** diff --git a/lib/ServerlessMeta.js b/lib/ServerlessMeta.js index c23b67548..43b3ca6c7 100644 --- a/lib/ServerlessMeta.js +++ b/lib/ServerlessMeta.js @@ -6,7 +6,6 @@ const SError = require('./ServerlessError'), SUtils = require('./utils/index'), - ServerlessModule = require('./ServerlessModule'), path = require('path'), fs = require('fs'), BbPromise = require('bluebird'); diff --git a/lib/ServerlessModule.js b/lib/ServerlessModule.js index 0510da47c..c35e5151c 100644 --- a/lib/ServerlessModule.js +++ b/lib/ServerlessModule.js @@ -6,13 +6,13 @@ */ const SError = require('./ServerlessError'), - SUtils = require('./utils/index'), - ServerlessFunction = require('./ServerlessFunction'), - extend = require('util')._extend, - path = require('path'), - _ = require('lodash'), - fs = require('fs'), - BbPromise = require('bluebird'); + SUtils = require('./utils/index'), + ServerlessFunction = require('./ServerlessFunction'), + extend = require('util')._extend, + path = require('path'), + _ = require('lodash'), + fs = require('fs'), + BbPromise = require('bluebird'); class ServerlessModule { @@ -20,12 +20,30 @@ class ServerlessModule { * Constructor */ - constructor(Serverless, options) { - this.S = Serverless; - this.options = options || {}; + constructor(Serverless, config) { + this.S = Serverless; + this.updateConfig(config || {}); this.load(); } + /** + * Update Config + * - Takes config.sPath and parses it to the scope's config object + */ + + updateConfig(config) { + if (config) { + this.config = extend(this.config, config); + if (this.config.sPath) { + this.S.validatePath(this.config.sPath, 'component'); + // Always parse sPath + this.config = extend(this.config, this.S.parsePath(this.config.sPath)); + } + // Add full path + this.config.fullPath = this.config.fullPath ? this.config.fullPath : path.join(this.S.config.projectPath, this.config.component); + } + } + /** * Load * - Load from source (i.e., file system); @@ -37,13 +55,13 @@ class ServerlessModule { // Defaults _this.data = {}; - _this.data.name = _this.options.module || 'module' + SUtils.generateShortId(6); + _this.data.name = 'module' + SUtils.generateShortId(6); _this.data.version = '0.0.1'; _this.data.profile = 'aws-v' + require('../package.json').version; _this.data.location = 'https://github.com/...'; _this.data.author = ''; _this.data.description = 'A Serverless Module'; - _this.data.runtime = _this.options.runtime || 'nodejs'; + _this.data.runtime = 'nodejs'; _this.data.custom = {}; _this.data.functions = {}; _this.data.templates = {}; @@ -52,18 +70,20 @@ class ServerlessModule { lambdaIamPolicyDocumentStatements: [] }; - if (_this.options.module) { - _this.options.modulePath = path.join(_this.S.config.projectPath, 'back', 'modules', _this.options.module) + if (_this.config.fullPath) { + let modulePath = path.join(_this.S.config.projectPath, _this.config.component, _this.config.module) } - // If no project path exists, return - if (!_this.S.config.projectPath || !_this.options.module || !SUtils.fileExistsSync(path.join(_this.options.modulePath, 's-module.json'))) return; + // If paths, check if this is on the file system + if (!_this.S.config.projectPath || + !_this.config.fullPath || + !SUtils.fileExistsSync(path.join(_this.config.fullPath, 's-module.json'))) return; let moduleJson = SUtils.readAndParseJsonSync(path.join(_this.options.modulePath, 's-module.json')); // Add Functions moduleJson.functions = {}; - let functionList = fs.readdirSync(path.join(_this.options.modulePath, 'functions')); + let functionList = fs.readdirSync(path.join(modulePath, 'functions')); for (let i = 0; i < functionList.length; i++) { @@ -130,7 +150,8 @@ class ServerlessModule { let _this = this; - if(!_this.options.modulePath) return; + // Validate path + if (!_this.config.sPath) throw new SError('A Serverless path must be set to save to a location'); // loop over functions and save Object.keys(_this.data.functions).forEach(function(functionName) { diff --git a/lib/ServerlessProject.js b/lib/ServerlessProject.js index deda6ba7b..e5a4f2511 100644 --- a/lib/ServerlessProject.js +++ b/lib/ServerlessProject.js @@ -16,7 +16,6 @@ class ServerlessProject { /** * Constructor - * - options.projectPath: absolute path to project */ constructor(Serverless, options) { @@ -43,7 +42,7 @@ class ServerlessProject { _this.data.author = ''; _this.data.description = 'A Slick New Serverless Project'; _this.data.custom = {}; - _this.data.modules = {}; + _this.data.components = {}; _this.data.plugins = []; _this.data.cloudFormation = { "AWSTemplateFormatVersion": "2010-09-09", @@ -114,16 +113,15 @@ class ServerlessProject { if (!_this.S.config.projectPath) return; // Get Project JSON - let project = SUtils.readAndParseJsonSync(path.join(_this.S.config.projectPath, 's-project.json')); + let project = SUtils.readAndParseJsonSync(path.join(_this.S.config.projectPath, 's-project.json')); + let projectContents = fs.readdirSync(path.join(_this.S.config.projectPath)); - // Add Modules & Functions - project.modules = {}; - let moduleList = fs.readdirSync(path.join(_this.S.config.projectPath, 'back', 'modules')); - - for (let i = 0; i < moduleList.length; i++) { - let module = new this.S.classes.Module(_this.S, { module: moduleList[i] }); - module = module.get(); - project.modules[module.name] = module; + for (let i = 0; i < projectContents.length; i++) { + if (SUtils.fileExistsSync(path.join(_this.S.config.projectPath, componentContents[i], 's-component.json'))) { + let component = new this.S.classes.Component(_this.S, { component: componentContents[i] }); + component = component.get(); + project.data.components[component.name] = component; + } } // Add to data @@ -145,6 +143,7 @@ class ServerlessProject { */ getPopulated(options) { + options = options || {}; // Required: Stage & Region @@ -163,6 +162,7 @@ class ServerlessProject { */ getResources(options) { + options = options || {}; // Required: Stage & Region @@ -174,30 +174,109 @@ class ServerlessProject { return SUtils.getResources(this.getPopulated(options)); } + /** + * getComponents + * - returns an array of component instances + * - options.paths is an array of serverless paths like this: ['component', 'component'] + */ + + getComponents(options) { + + let _this = this, + pathsObj = {}, + components = []; + + options = options || {}; + + // If no project path exists, throw error + if (!_this.S.config.projectPath) throw new SError('Project path must be set in Serverless to use this method'); + + // If paths, create temp obj for easy referencing + if (options.paths && options.paths.length) { + options.paths.forEach(function (path) { + + let component = path.split('/')[0]; + if (!pathsObj[component]) pathsObj[component] = {}; + }); + } + + for (let i = 0; i < Object.keys(project.components).length; i++) { + + let component = project.components[Object.keys(project.components)[i]]; + + // If paths, and this component is not included, skip + if (options.paths && + options.paths.length && + !pathsObj[component.name]) continue; + + let component = new _this.S.classes.Component(_this.S, { component: component.name }); + component.push(component); + } + + if (options.paths && !components.length) { + throw new SError('No components found in the paths you provided'); + } + + return components; + } + /** * getModules * - returns an array of module instances - * - paths is an array of module names: ['moduleOne', 'moduleTwo'] + * - options.paths is an array of serverless paths like this: ['component/moduleOne', 'component/moduleTwo'] */ getModules(options) { - let _this = this, - modules = []; + let _this = this, + pathsObj = {}, + modules = []; options = options || {}; - for (let i = 0; i < Object.keys(this.data.modules).length; i++) { + // If no project path exists, throw error + if (!_this.S.config.projectPath) throw new SError('Project path must be set in Serverless to use this method'); - // If paths, and this module is not included, skip + // If paths, create temp obj for easy referencing + if (options.paths && options.paths.length) { + options.paths.forEach(function (path) { + + let component = path.split('/')[0]; + let module = path.split('/')[1]; + + if (!pathsObj[component]) pathsObj[component] = {}; + if (!pathsObj[component][module]) pathsObj[component][module] = {}; + }); + } + + for (let i = 0; i < Object.keys(_this.data.components).length; i++) { + + let component = Object.keys(_this.data.components)[i]; + + // If paths, and this component is not included, skip if (options.paths && options.paths.length && - options.paths.indexOf(Object.keys(this.data.modules)[i]) === -1) continue; + !pathsObj[component.name]) continue; - let module = new _this.S.classes.Module(_this.S); - module.data = _this.data.modules[Object.keys(this.data.modules)[i]]; - modules.push(module); + for (let j = 0; j < component.modules.length; j++) { + let module = Object.keys(component.modules)[j]; + + // If paths, and this component is not included, skip + if (options.paths && + options.paths.length && + !pathsObj[component.name][module.name]) continue; + + let module = new _this.S.classes.Component(_this.S, { + component: component.name, + module: module.name + }); + modules.push(module); + } + } + + if (options.paths && !modules.length) { + throw new SError('No modules found in the paths you provided'); } return modules; @@ -206,7 +285,7 @@ class ServerlessProject { /** * getFunctions * - returns an array of function instances - * - paths is an array with this format: ['moduleOne/functionOne', 'moduleTwo/functionOne'] + * - options.paths is an array of Serverless paths like this: ['component/moduleOne/functionOne', 'component/moduleOne/functionOne'] */ getFunctions(options) { @@ -221,30 +300,51 @@ class ServerlessProject { if (options.paths && options.paths.length) { options.paths.forEach(function (path) { - let module = path.split('/')[0]; - let func = path.split('/')[1]; + let component = path.split('/')[0]; + let module = path.split('/')[1]; + let func = path.split('/')[2].split('@')[0]; // Allows using this in getEndpoints - if (!pathsObj[module]) pathsObj[module] = {}; - pathsObj[module][func] = true; + if (!pathsObj[component]) pathsObj[component] = {}; + if (!pathsObj[component][module]) [component][module] = {}; + pathsObj[component][module][func] = true; }); } - for (let i = 0; i < Object.keys(this.data.modules).length; i++) { + for (let i = 0; i < Object.keys(_this.data.components).length; i++) { - let module = this.data.modules[Object.keys(this.data.modules)[i]]; + let component = Object.keys(_this.data.components)[i]; - for (let j = 0; j < Object.keys(module.functions).length; j++) { + // If paths, and this component is not included, skip + if (options.paths && + options.paths.length && + !pathsObj[component.name]) continue; - let func = module.functions[Object.keys(module.functions)[j]]; + for (let j = 0; j < component.modules.length; j++) { - // If paths, and this function is not included, skip - if (options.paths && options.paths.length && (!pathsObj[module.name] || !pathsObj[module.name][func.name])) continue; + let module = Object.keys(component.modules)[j]; - let funcInstance = new _this.S.classes.Function(_this.S, { - module: module.name, - function: func.name - }); - functions.push(funcInstance); + // If paths, and this component is not included, skip + if (options.paths && + options.paths.length && + !pathsObj[component.name][module.name]) continue; + + for (let k = 0; k < module.functions.length; k++) { + + let func = Object.keys(module.functions)[k]; + + // If paths, and this component is not included, skip + if (options.paths && + options.paths.length && + !pathsObj[component.name][module.name] && + !pathsObj[component.name][module.name][func.name]) continue; + + let func = new _this.S.classes.Function(_this.S, { + component: component.name, + module: module.name, + function: func.name + }); + functions.push(func); + } } } @@ -278,10 +378,11 @@ class ServerlessProject { throw new SError('Invalid endpoint path provided: ' + path); } - let module = path.split('/')[0]; - let func = path.split('/')[1].split('@')[0]; - let urlPath = path.split('@')[1].split('~')[0]; - let method = path.split('~')[1]; + let component = path.split('/')[0]; + let module = path.split('/')[1]; + let func = path.split('/')[2].split('@')[0]; + let urlPath = path.split('@')[1].split('~')[0]; + let method = path.split('~')[1]; if (!pathsObj[module]) pathsObj[module] = {}; if (!pathsObj[module][func]) pathsObj[module][func] = {}; @@ -290,37 +391,22 @@ class ServerlessProject { }); } - // Loop - Modules - for (let i = 0; i < Object.keys(project.modules).length; i++) { + // Get Functions + let functions = _this.getFunctions(options); - let module = project.modules[Object.keys(project.modules)[i]]; + for (let i = 0; i < functions.length; i++) { - // Loop - Functions - for (let j = 0; j < Object.keys(module.functions).length; j++) { + let func = functions[i].data; - let func = module.functions[Object.keys(module.functions)[j]]; + for (let j = 0; j < func.endpoints.length; j++) { - // Loop - Endpoints - for (let k = 0; k < func.endpoints.length; k++) { + let endpoint = func.endpoints[j]; - let endpoint = { - data: func.endpoints[k] - }; + if (options.paths && + options.paths.length && + !pathsObj[func.component][func.module][func.name][endpoint.path][endpoint.method]) continue; - // If paths, and this endpoint is not included, skip - if (options.paths && - options.paths.length && - (!pathsObj[module.name] || - !pathsObj[module.name][func.name] || - !pathsObj[module.name][func.name][endpoint.data.path] || - !pathsObj[module.name][func.name][endpoint.data.path][endpoint.data.method]) - ) continue; - - endpoint.module = module.name; - endpoint.function = func.name; - endpoint.sPath = module.name + '/' + func.name + '@' + endpoint.data.path + '~' + endpoint.data.method; - endpoints.push(endpoint); - } + endpoints.push(endpoint); } } @@ -340,41 +426,18 @@ class ServerlessProject { let _this = this; - // Loop over functions and save - Object.keys(_this.data.modules).forEach(function(moduleName) { + // Loop over components and save + Object.keys(_this.data.components).forEach(function(componentName) { - let module = new _this.S.classes.Module(_this.S); - module.data = Object.create(_this.data.modules[moduleName]); - module.save(); + let component = new _this.S.classes.Module(_this.S); + component.data = Object.create(_this.data.components[componentName]); + component.save(); }); - let modulesTemp = false; - - // If file exists, do a diff and skip if equal - if (SUtils.fileExistsSync(path.join(_this.S.config.projectPath, 's-project.json'))) { - - let projectJson = SUtils.readAndParseJsonSync(path.join(_this.S.config.projectPath, 's-project.json')); - - // Temporarily store and delete functions to compare with JSON - modulesTemp = Object.create(_this.data.modules); - delete _this.data['modules']; - - // check if data changed - if (_.isEqual(projectJson, _this.data)) { - - // clone back functions property that we deleted - _this.data.modules = Object.create(modulesTemp); - return; - } - } - - // overwrite modules JSON file + // Save JSON file fs.writeFileSync(path.join(_this.S.config.projectPath, 's-project.json'), JSON.stringify(this.data, null, 2)); - if (modulesTemp) this.data.modules = Object.create(modulesTemp); - - return; } }