serverless/lib/actions/FunctionRun.js
Austen Collins 01ba9f6a52 All: Rebrand
2015-12-03 20:31:49 -08:00

77 lines
1.8 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
*/
const SPlugin = require('../ServerlessPlugin'),
SError = require('../ServerlessError'),
BbPromise = require('bluebird'),
SUtils = require('../utils'),
path = require('path');
/**
* 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 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') {
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);
} 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;