'use strict'; /** * Action: FunctionCreate * - takes existing component name and new function name * - validates that component exists * - validates that function does NOT already exists in component * - generates function structure based on runtime * * Event Options: * - sPath: (String) The relative path of the function from project root */ module.exports = function(SPlugin, serverlessPath) { const path = require('path'), SError = require(path.join(serverlessPath, 'Error')), SCli = require(path.join(serverlessPath, 'utils/cli')), BbPromise = require('bluebird'), SUtils = require(path.join(serverlessPath, 'utils')); let fs = require('fs'); BbPromise.promisifyAll(fs); /** * FunctionCreate Class */ class FunctionCreate extends SPlugin { constructor(S, config) { super(S, config); } static getName() { return 'serverless.core.' + FunctionCreate.name; } registerActions() { this.S.addAction(this.functionCreate.bind(this), { handler: 'functionCreate', description: `Creates scaffolding for a new function. usage: serverless function create `, context: 'function', contextAction: 'create', options: [], parameters: [ { parameter: 'path', description: 'One path to your function relative to the project root', position: '0' } ] }); return BbPromise.resolve(); } /** * Action */ functionCreate(evt) { let _this = this; _this.evt = evt; return _this._prompt() .bind(_this) .then(_this._validateAndPrepare) .then(_this._createFunction) .then(_this._scaffold) .then(function() { SCli.log('Successfully created function: "' + _this.evt.options.path + '"'); /** * Return Event */ return _this.evt; }); } /** * Prompt component, module & function if they're missing */ _prompt() { let _this = this, overrides = {}; // If non-interactive or path exists, skip if (!_this.S.config.interactive || _this.evt.options.path) return BbPromise.resolve(); if (!SUtils.fileExistsSync(path.join(process.cwd(), 's-component.json'))) { return BbPromise.reject(new SError('You must be in a component root to create a function')); } let prompts = { properties: { name: { description: 'Enter a new function name: '.yellow, message: 'Function name must contain only letters, numbers, hyphens, or underscores. It should not be longer than 20 characters.', required: true, conform: function(functionName) { return SUtils.isFunctionNameValid(functionName); } } } }; return _this.cliPromptInput(prompts, overrides) .then(function(answers) { _this.evt.options.path = process.cwd().split(path.sep).pop() + '/' + answers.name; }); }; /** * Validate and prepare data before creating module */ _validateAndPrepare() { let _this = this; // Validate: check path if (!_this.evt.options.path) { return BbPromise.reject(new SError('path is required.')); } _this.functionName = _this.evt.options.path.split('/')[_this.evt.options.path.split('/').length - 1]; // Validate: Don't allow function creation within a function if (_this.S.getProject().getFunction( _this.functionName )) { return BbPromise.reject(new SError('You cannot create a function in another function')); } // If component does not exist in project, throw error _this.evt.options.component = _this.S.getProject().getComponent( _this.evt.options.path.split('/')[0] ); if (!_this.evt.options.component) { return BbPromise.reject(new SError( 'Component (' + _this.evt.options.path.split('/')[0] + ') does not exist in project', SError.errorCodes.INVALID_PROJECT_SERVERLESS )); } return BbPromise.resolve(); }; /** * Create Function Skeleton */ _createFunction() { this.function = new this.S.classes.Function(this.S, this.evt.options.component, { name: this.functionName }, path.join(this.S.getProject().getRootPath(), this.evt.options.path)); this.evt.options.component.setFunction( this.function ); this.evt.data.path = this.evt.options.path; return this.function.save(); }; /** * Generate Extra Scaffolding */ _scaffold() { let _this = this, writeDeferred = []; return BbPromise.try(function() { if (_this.evt.options.component.getRuntime().name === 'nodejs') { let handlerJs = fs.readFileSync(path.join(_this.S.getServerlessPath(), 'templates', 'nodejs', 'handler.js')); writeDeferred.push( SUtils.writeFile(path.join(_this.function.getRootPath(), 'handler.js'), handlerJs) ); } else if (_this.evt.options.component.getRuntime().name === 'python2.7') { let handlerPy = fs.readFileSync(path.join(_this.S.getServerlessPath(), 'templates', 'python2.7', 'handler.py')); writeDeferred.push( SUtils.writeFile(path.join(_this.function.getRootPath(), 'handler.py'), handlerPy) ); } writeDeferred.push( SUtils.writeFile(path.join(_this.function.getRootPath(), 'event.json'), {}) ); return BbPromise.all(writeDeferred); }); }; } return( FunctionCreate ); };