diff --git a/README.md b/README.md index 366feb0a..6e5c2578 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ node-lambda deploy #### setup -Initializes the `event.json`, `.env` files, and `deploy.env` files. `event.json` is where you mock your event. `.env.` is where you place your deployment configuration. `deploy.env` has the same format as `.env`, but is used for holding any environment/config variables that you need to be deployed with your code to Lambda but you don't want in version control (e.g. DB connection info). +Initializes the `event.json`, `context.json`, `.env` files, and `deploy.env` files. `event.json` is where you mock your event. `context.json` is where you can add additional mock data to the context passed to your lambda function. .env.` is where you place your deployment configuration. `deploy.env` has the same format as `.env`, but is used for holding any environment/config variables that you need to be deployed with your code to Lambda but you don't want in version control (e.g. DB connection info). ``` $ node-lambda setup --help @@ -64,9 +64,10 @@ $ node-lambda run --help Options: - -h, --help output usage information - -h, --handler [index.handler] Lambda Handler {index.handler} - -j, --eventFile [event.json] Event JSON File + -h, --help output usage information + -h, --handler [index.handler] Lambda Handler {index.handler} + -j, --eventFile [event.json] Event JSON File + -x, --contextFile [context.json] Context JSON file ``` #### deploy diff --git a/bin/node-lambda b/bin/node-lambda index ad755f49..20a95732 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -23,6 +23,7 @@ var AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || ''; var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs'; var AWS_FUNCTION_VERSION = process.env.AWS_FUNCTION_VERSION || ''; var EVENT_FILE = process.env.EVENT_FILE || 'event.json'; +var CONTEXT_FILE = process.env.CONTEXT_FILE || 'context.json'; program .version(lambda.version) @@ -55,6 +56,7 @@ program .description('Run your Amazon Lambda application locally') .option('-h, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER) .option('-j, --eventFile [' + EVENT_FILE + ']', 'Event JSON File', EVENT_FILE) + .option('-x, --contextFile [' + CONTEXT_FILE + ']', 'Context JSON File', CONTEXT_FILE) .action(function (prg) { lambda.run(prg); }); diff --git a/lib/context.json.example b/lib/context.json.example new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/lib/context.json.example @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index 8e147765..31573e5d 100644 --- a/lib/main.js +++ b/lib/main.js @@ -37,31 +37,32 @@ Lambda.prototype.setup = function () { Lambda.prototype.run = function (program) { this._createSampleFile('event.json'); - + this._createSampleFile('context.json'); var splitHandler = program.handler.split('.'); var filename = splitHandler[0] + '.js'; var handlername = splitHandler[1]; var handler = require(process.cwd() + '/' + filename)[handlername]; var event = require(process.cwd() + '/' + program.eventFile); + var context = require(process.cwd() + '/' + program.contextFile); - this._runHandler(handler, event); + this._runHandler(handler, event, context); }; -Lambda.prototype._runHandler = function (handler, event) { - var context = { - succeed: function (result) { +Lambda.prototype._runHandler = function (handler, event, context) { + context.succeed = function (result) { console.log('succeed: ' + JSON.stringify(result)); process.exit(0); - }, - fail: function (error) { + } + + context.fail = function (error) { console.log('fail: ' + error); process.exit(-1); - }, - done: function () { + } + + context.done = function () { process.exit(0); } - }; handler(event, context); };