serverless/lib/actions/FunctionRun.js
2015-12-07 13:57:41 +02:00

97 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* Action: FunctionRun
* - Runs the function in the CWD for local testing
*/
const SPlugin = require('../ServerlessPlugin'),
SError = require('../ServerlessError'),
BbPromise = require('bluebird'),
SUtils = require('../utils'),
path = require('path');
let fs = require('fs');
BbPromise.promisifyAll(fs);
/**
* FunctionRun Class
*/
class FunctionRun extends SPlugin {
/**
* Constructor
*/
constructor(S, config) {
super(S, config);
}
/**
* Define your plugins name
*/
static getName() {
return 'serverless.core.' + FunctionRun.name;
}
/**
* @returns {Promise} upon completion of all registrations
*/
registerActions() {
this.S.addAction(this.functionRun.bind(this), {
handler: 'functionRun',
description: `Runs the service locally. Reads the services runtime and passes it off to a runtime-specific runner`,
context: 'function',
contextAction: 'run',
options: [],
});
return BbPromise.resolve();
}
/**
* Action
*/
functionRun() {
let _this = this,
cwd = process.cwd(),
event = SUtils.readAndParseJsonSync(path.join(cwd, 'event.json')),
awsmJson = SUtils.readAndParseJsonSync(path.join(cwd, 's-function.json'));
if (awsmJson.cloudFormation.lambda.Function.Properties.Runtime == 'nodejs') {
// copy .env file into function dir
if(SUtils.fileExistsSync(path.join(_this.S._projectRootPath, 'back', '.env'))) {
fs.createReadStream('../../../.env').pipe(fs.createWriteStream('.env'));
}
let handlerParts = awsmJson.cloudFormation.lambda.Function.Properties.Handler.split('/').pop().split('.');
// running the nodejs subaction
let newEvent = {
awsmJson: awsmJson,
handler: require(cwd + '/' + handlerParts[0] + '.js')[handlerParts[1]],
event: event
};
return _this.S.actions.functionRunLambdaNodeJs(newEvent)
.then(function() {
// remove the .env file
if(SUtils.fileExistsSync(path.join(process.cwd(), '.env'))) {
fs.unlinkSync('.env');
}
});
} else {
return BbPromise.reject(new SError('To simulate you must have an index.js that exports run(event,context)', SError.errorCodes.UNKNOWN));
}
}
}
module.exports = FunctionRun;