'use strict'; /** * Serverless Function Class * - options.path format is: "moduleFolder/functionFolder#functionName" */ 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.module || !config.function) throw new SError('Missing required config.component, config.module 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.module, _this._config.function, 'handler.handler'); _this.timeout = 6; _this.memorySize = 1024; _this.custom = { excludePatterns: [], envVars: [] }; _this.events = []; _this.endpoints = []; _this.endpoints.push(new _this._S.classes.Endpoint(_this._S, { component: _this._config.component, module: _this._config.module, function: _this._config.function, endpointPath: _this._config.module + '/' + _this._config.function, endpointMethod: 'GET' })); _this.templates = {}; } /** * Set * - Set data * - Accepts a data object */ set(data) { let _this = this; // Instantiate endpoints 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, module: _this._config.module, function: _this.name, endpointPath: _this._config.module + '/' + _this.name, endpointMethod: data.endpoints[i].method }); data.endpoints[i] = instance.set(data.endpoints[i]); } // Instantiate events for (let i = 0; i < data.events.length; i++) { if (data.events[i] instanceof _this._S.classes.Event) { throw new SError('You cannot pass subclasses into the set method, only object literals'); } let instance = new _this._S.classes.Event(_this._S, { component: _this._config.component, module: _this._config.module, function: _this.name, name: data.events[i].name, type: data.events[i].type, config: data.events[i].config }); data.events[i] = instance.set(data.events[i]); } // Merge _.assign(_this, data); return _this; } /** * Update Config * - Takes config.component, config.module, config.function */ updateConfig(config) { if (!config) return; // Set sPath if (config.component || config.module || config.function) { this._config.component = config.component; this._config.module = config.module; this._config.function = config.function; this._config.sPath = SUtils.buildSPath({ component: config.component, module: config.module, 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.module, 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')); // loop through endpoints return functionJson.endpoints; }) .then(function(endpoints){ for (let i = 0; i < endpoints.length; i++) { functionJson.endpoints[i] = new _this._S.classes.Endpoint(_this._S, { component: _this._config.component, module: _this._config.module, function: functionJson.name, endpointPath: endpoints[i].path, endpointMethod: endpoints[i].method }); functionJson.endpoints[i].load() .then(function(instance) { functionJson.endpoints[i] = instance; // loop through events return functionJson.endpoints[i]; }); } return functionJson.events; }) .then(function(events) { // Add Event Class Instances for (let i = 0; i < events.length; i++) { functionJson.events[i] = new _this._S.classes.Event(_this._S, { component: _this._config.component, module: _this._config.module, function: functionJson.name, name: events[i].name, type: events[i].type, config: events[i].config }); functionJson.events[i].load() .then(function (instance) { functionJson.events[i] = instance; return functionJson.events[i]; }); } return functionJson; }) .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(); } for (let i = 0; i < _this.events.length; i++) { clone.events[i] = _this.events[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 = []; clone.events = []; for (let i = 0; i < _this.endpoints.length; i++) { clone.endpoints[i] = _this.endpoints[i].getPopulated(options); } for (let i = 0; i < _this.events.length; i++) { clone.events[i] = _this.events[i].getPopulated(options); } return clone; } /** * Get Templates * - Returns clone of templates * - Inherits parent templates */ getTemplates() { return _.merge( this.getProject().getTemplates(), this.getComponent().getTemplates(), this.getModule().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(){ return _this.events; }) .each(function(event) { return event.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'); } /** * Get Module * - Returns reference to the instance */ getModule() { let modules = this._S.state.getModules({ component: this._config.component, module: this._config.module }); if (modules.length === 1) { return modules[0]; } throw new SError('Could not find module for endpoint'); } } module.exports = ServerlessFunction;