'use strict'; /** * Serverless Function Class * - options.path format is: "component/cPath/functionFolder" * - cPath is for component path. You can nest functions in any amounts of subfolders (these subfolders are the cPath) * - But you must use '/' to divide subfolders. E.g., restApi/data/users/show * - In the example, data/users is the component path. The 'cPath' */ const SError = require('./ServerlessError'), SUtils = require('./utils/index'), BbPromise = require('bluebird'), async = require('async'), path = require('path'), fs = require('fs'), _ = require('lodash'); class ServerlessFunction { /** * Constructor */ constructor(Serverless, config) { // Validate required attributes if (!config.component || !config.function) throw new SError('Missing required config.component or config.function'); let _this = this; _this._S = Serverless; _this._config = {}; _this.updateConfig(config); // Default properties _this.name = _this._config.function || 'function' + SUtils.generateShortId(6); _this.handler = path.posix.join( _this._config.cPath ? _this._config.cPath.split('/').join(path.sep) : null, _this._config.function, 'handler.handler'); _this.timeout = 6; _this.memorySize = 1024; _this.custom = { excludePatterns: [], envVars: [] }; _this.endpoints = []; _this.endpoints.push(new _this._S.classes.Endpoint(_this._S, { component: _this._config.component, cPath: _this._config.cPath, function: _this._config.function, endpointPath: _this._config.cPath ? _this._config.cPath + '/' + _this._config.function : _this._config.function, endpointMethod: 'GET' })); _this.templates = {}; } /** * Set * - Set data * - Accepts a data object */ set(data) { let _this = this; // Instantiate Components for (let i = 0; i < data.endpoints.length; i++) { if (data.endpoints[i] instanceof _this._S.classes.Endpoint) { throw new SError('You cannot pass subclasses into the set method, only object literals'); } let instance = new _this._S.classes.Endpoint(_this._S, { component: _this._config.component, cPath: _this._config.cPath, function: _this.name, endpointPath: _this._config.cPath ? _this._config.cPath + '/' + _this._config.function : _this._config.function, endpointMethod: data.endpoints[i].method }); data.endpoints[i] = instance.set(data.endpoints[i]); } // Merge _.assign(_this, data); return _this; } /** * Update Config * - Takes config.component, config.cPath, config.function */ updateConfig(config) { if (!config) return; // Check cPath separators if (config.cPath.indexOf('\\') !== -1) throw new SError('Cannot use "\\" in config.cPath'); // Set sPath if (config.component || config.cPath || config.function) { this._config.component = config.component; this._config.function = config.function; this._config.cPath = config.cPath ? config.cPath : null; this._config.sPath = SUtils.buildSPath({ component: config.component, cPath: config.cPath, function: config.function }); } // Make full path if (this._S.config.projectPath && this._config.sPath) { let parse = SUtils.parseSPath(this._config.sPath); this._config.fullPath = path.join( this._S.config.projectPath, parse.component, parse.cPath ? parse.cPath.split('/').join(path.sep) : null, parse.function ); } } /** * Load * - Load from source (i.e., file system); * - Returns Promise */ load() { let _this = this, functionJson; return BbPromise.try(function() { // Validate: Check project path is set if (!_this._S.config.projectPath) throw new SError('Function could not be loaded because no project path has been set on Serverless instance'); // Validate: Check function exists if (!SUtils.fileExistsSync(path.join(_this._config.fullPath, 's-function.json'))) { throw new SError('Function could not be loaded because it does not exist in your project: ' + _this._config.sPath); } functionJson = SUtils.readAndParseJsonSync(path.join(_this._config.fullPath, 's-function.json')); return functionJson.endpoints; }) .each(function(e, i) { // Add Endpoint Class Instances functionJson.endpoints[i] = new _this._S.classes.Endpoint(_this._S, { component: _this._config.component, cPath: _this._config.cPath, function: functionJson.name, endpointPath: e.path, endpointMethod: e.method }); return functionJson.endpoints[i].load() .then(function(instance) { functionJson.endpoints[i] = instance; return functionJson.endpoints[i]; }); }) .then(function() { // Get templates if (_this._config.fullPath && SUtils.fileExistsSync(path.join(_this._config.fullPath, 's-templates.json'))) { functionJson.templates = require(path.join(_this._config.fullPath, 's-templates.json')); } }) .then(function() { // Merge _.assign(_this, functionJson); return _this; }); } /** * Get * - Return data */ get() { let _this = this; let clone = _.cloneDeep(_this); for (let i = 0; i < _this.endpoints.length; i++) { clone.endpoints[i] = _this.endpoints[i].get(); } return SUtils.exportClassData(clone); } /** * getPopulated * - Fill in templates then variables * - Returns Promise */ getPopulated(options) { let _this = this; options = options || {}; // Validate: Check Stage & Region if (!options.stage || !options.region) throw new SError('Both "stage" and "region" params are required'); // Validate: Check project path is set if (!_this._S.config.projectPath) throw new SError('Function could not be populated because no project path has been set on Serverless instance'); // 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); } return clone; } /** * Get Templates * - Returns clone of templates * - Inherits parent templates */ getTemplates() { return _.merge( this.getProject().getTemplates(), this.getComponent().getTemplates(), _.cloneDeep(this.templates) ); } /** * Save * - Saves data to file system * - Returns promise */ save(options) { let _this = this; return new BbPromise.try(function() { // Validate: Check project path is set if (!_this._S.config.projectPath) throw new SError('Function could not be saved because no project path has been set on Serverless instance'); // Create if does not exist if (!SUtils.fileExistsSync(path.join(_this._config.fullPath, 's-function.json'))) { return _this._create(); } }) .then(function() { // Save all nested endpoints if (options && options.deep) { return BbPromise.try(function () { return _this.endpoints; }) .each(function(endpoint) { return endpoint.save(); }) } }) .then(function() { // If templates, save templates if (_this.templates && Object.keys(_this.templates).length) { return SUtils.writeFile(path.join(_this._config.fullPath, 's-templates.json'), _this.templates); } }) .then(function() { let clone = _this.get(); // Strip properties if (clone.templates) delete clone.templates; // Write file return SUtils.writeFile(path.join(_this._config.fullPath, 's-function.json'), JSON.stringify(clone, null, 2)); }) .then(function() { return _this; }) } /** * Create (scaffolding) * - Returns promise */ _create() { let _this = this; return BbPromise.try(function() { let writeDeferred = []; // Runtime: nodejs writeDeferred.push( fs.mkdirSync(_this._config.fullPath), SUtils.writeFile(path.join(_this._config.fullPath, 'event.json'), '{}') ) if (_this.getRuntime() === 'nodejs') { writeDeferred.push( SUtils.writeFile(path.join(_this._config.fullPath, 'handler.js'), fs.readFileSync(path.join(_this._S.config.serverlessPath, 'templates', 'nodejs', 'handler.js'))) ) } else if (_this.getRuntime() === 'python2.7') { writeDeferred.push( SUtils.writeFile(path.join(_this._config.fullPath, 'handler.py'), fs.readFileSync(path.join(_this._S.config.serverlessPath, 'templates', 'python2.7', 'handler.py'))) ) } return BbPromise.all(writeDeferred); }); } getRuntime() { let _this = this; let component = _this._S.state.getComponents({ paths: [_this._config.component] })[0]; if (!component) throw new SError('The component containing runtime information for this function could not be found'); return component.runtime; } /** * Get Project * - Returns reference to the instance */ getProject() { return this._S.state.project; } /** * Get Component * - Returns reference to the instance */ getComponent() { let components = this._S.state.getComponents({ component: this._config.component }); if (components.length === 1) { return components[0]; } throw new SError('Could not find component for endpoint'); } } module.exports = ServerlessFunction;