UNPKG

3.53 kBJavaScriptView Raw
1"use strict";
2
3var
4aws = require( "aws-sdk" ),
5exec = require( "child_process" ).exec,
6fs = require( "fs" ),
7os = require( "os" ),
8packageJson = require( "./../package.json" ),
9path = require( "path" );
10
11var Lambda = function( ) {
12 this.version = packageJson.version;
13
14 return this;
15};
16
17Lambda.prototype._createEnvFile = function( ) {
18 var
19 envfile = process.cwd() + "/.env",
20 envexamplefile = __dirname + "/.env.example";
21
22 if ( !fs.existsSync( envfile ) ) {
23 console.log( ".env file successfully created" )
24 fs.writeFileSync( envfile, fs.readFileSync( envexamplefile) );
25 }
26
27};
28
29Lambda.prototype._createEventJsonFile = function( ) {
30 var
31 eventfile = process.cwd() + "/event.json",
32 eventexamplefile = __dirname + "/event.json.example";
33
34 if ( !fs.existsSync( eventfile ) ) {
35 console.log( "event.json file successfully created" )
36 fs.writeFileSync( eventfile, fs.readFileSync( eventexamplefile ) );
37 }
38
39};
40
41Lambda.prototype.setup = function( ) {
42 console.log( "Running setup." );
43 this._createEnvFile( );
44 this._createEventJsonFile( );
45 console.log( "Setup done. Edit the .env and event.json files as needed." );
46};
47
48Lambda.prototype.run = function( program ) {
49 this._createEventJsonFile( );
50
51 var
52 dir = program.directory,
53 splitHandler = program.handler.split( "." ),
54 filename = splitHandler[0] + ".js",
55 handlername = splitHandler[1];
56
57 var
58 handler = require( process.cwd() + "/" + filename )[handlername],
59 event = require( process.cwd() + "/event.json" );
60
61 this._runHandler( handler, event );
62}
63
64Lambda.prototype._runHandler = function( handler, event ) {
65 var context = {
66 done: function( ) {
67 process.exit(0);
68 }
69 }
70
71 handler( event, context );
72};
73
74Lambda.prototype._zipfileTmpPath = function( program ) {
75 var
76 ms_since_epoch = +new Date,
77 filename = program.functionName + "-" + ms_since_epoch + ".zip",
78 zipfile = path.join( os.tmpDir(), filename );
79
80 return zipfile;
81};
82
83Lambda.prototype._params = function( program, buffer ) {
84 var params = {
85 FunctionName: program.functionName + "-" + program.environment,
86 FunctionZip: buffer,
87 Handler: program.handler,
88 Mode: program.mode,
89 Role: program.role,
90 Runtime: program.runtime,
91 Description: program.description,
92 MemorySize: program.memorySize,
93 Timeout: program.timeout
94 };
95
96 return params;
97};
98
99Lambda.prototype.deploy = function( program ) {
100 this._createEnvFile( );
101
102 aws.config.update({
103 accessKeyId: program.accessKey,
104 secretAccessKey: program.secretKey,
105 region: program.region
106 });
107
108 var
109 _this = this,
110 lambda = new aws.Lambda( { apiVersion: "2014-11-11" } ),
111 zipfile = _this._zipfileTmpPath( program );
112
113 // zip up but ignore .git, .dotfiles, .log files, and the event.json file.
114 console.log( "Generating zip file" );
115 exec( "zip -roq " + zipfile + " * -x '.git' -x '*/\.*' -x '*/\*.log' -x 'event.json'", function( err, stdout, stderr ) {
116 if ( err ) {
117 throw err;
118 }
119
120 console.log( "Reading zip file to memory" );
121 var
122 buffer = fs.readFileSync( zipfile ),
123 params = _this._params( program, buffer );
124
125 console.log( "Uploading zip file to AWS Lambda with parameters:" );
126 console.log( params );
127
128 lambda.uploadFunction( params, function( err, data ) {
129 if ( err ) {
130 console.log( err );
131 } else {
132 console.log( "Zip file done uploading. Results follow:" );
133 console.log( data );
134 }
135 } );
136
137 } );
138}
139
140module.exports = new Lambda( );
141