1 | const fs = require( "fs" );
|
2 | const AWS = require( "aws-sdk" );
|
3 |
|
4 | const utils = require( "./utils" );
|
5 |
|
6 | function getLambdaEventFile () {
|
7 |
|
8 | const path = `${process.env.COLLY__PROJECT_DIR}/${process.env.COLLY__LAMBDA_EVENT_FILE}`;
|
9 |
|
10 | if ( fs.existsSync( path ) ) {
|
11 | return JSON.parse( fs.readFileSync( path, "utf8" ) );
|
12 | } else {
|
13 | return {};
|
14 | }
|
15 |
|
16 | }
|
17 |
|
18 | function getLambdaContextFile () {
|
19 |
|
20 | const defaultContext = {
|
21 | "done": () => {}
|
22 | };
|
23 |
|
24 | const path = `${process.env.COLLY__PROJECT_DIR}/${process.env.COLLY__LAMBDA_CONTEXT_FILE}`;
|
25 |
|
26 | if ( fs.existsSync( path ) ) {
|
27 | return require( path );
|
28 | } else {
|
29 | return defaultContext;
|
30 | }
|
31 |
|
32 | }
|
33 |
|
34 | function runningLocally () {
|
35 | return JSON.parse( process.env.COLLY__RUN_LAMBDA_LOCAL );
|
36 | }
|
37 |
|
38 | function runLocally () {
|
39 |
|
40 | return new Promise( ( resolve, reject ) => {
|
41 |
|
42 | const projectConfig = utils.getProjectConfig();
|
43 |
|
44 | utils.addLambdaEnvironmentVariablesToProcess( projectConfig.environmentVariables );
|
45 |
|
46 | const config = utils.getLambdaConfigFile();
|
47 |
|
48 | const lambdaHandler = getLambdaHandler();
|
49 |
|
50 |
|
51 |
|
52 | lambdaHandler(
|
53 | getLambdaEventFile(),
|
54 | getLambdaContextFile(),
|
55 | function callback( err, data ) {
|
56 | if ( err ) {
|
57 | reject( err );
|
58 | }
|
59 | resolve( data );
|
60 | }
|
61 | );
|
62 |
|
63 | });
|
64 |
|
65 | }
|
66 |
|
67 | function runDeployedLamda () {
|
68 |
|
69 | utils.setAwsRegion();
|
70 | const config = utils.getLambdaConfigFile();
|
71 | const lambda = new AWS.Lambda();
|
72 | const params = {
|
73 | "FunctionName": utils.getLambdaName(),
|
74 | "InvocationType": "RequestResponse",
|
75 | "Payload": JSON.stringify( getLambdaEventFile() ),
|
76 | "LogType": "Tail"
|
77 | };
|
78 |
|
79 | lambda.invoke(params, function(err, data) {
|
80 | if ( err ) {
|
81 | console.log( err );
|
82 | } else {
|
83 | if ( data.LogResult ) {
|
84 | console.log( "Status code: " + data.StatusCode );
|
85 | console.log( "Payload: " + data.Payload );
|
86 | console.log( Buffer.from( data.LogResult, "base64" ).toString() );
|
87 | }
|
88 |
|
89 | }
|
90 | });
|
91 |
|
92 | }
|
93 |
|
94 | function getLambdaHandler() {
|
95 | try {
|
96 | return require( utils.getLambdaFilePath() )[ utils.getLambdaHandlerName() ];
|
97 | }
|
98 | catch( err ) {
|
99 | console.log( "Error: Cannot find the lambda you are trying to run. Check the `--name` or the handler property in the lambda's function file.");
|
100 | throw err;
|
101 | }
|
102 | }
|
103 |
|
104 | function init () {
|
105 |
|
106 | return new Promise( ( resolve, reject ) => {
|
107 |
|
108 | utils.authenticate().then( () => {
|
109 |
|
110 | if ( runningLocally() ) {
|
111 | runLocally()
|
112 | .then( ( data ) => {
|
113 | console.log( data );
|
114 | resolve( data );
|
115 | })
|
116 | .catch( reject );
|
117 | } else {
|
118 | runDeployedLamda();
|
119 | }
|
120 | resolve();
|
121 |
|
122 | }).catch( console.log );
|
123 |
|
124 | });
|
125 |
|
126 | }
|
127 |
|
128 | module.exports = {
|
129 | "getLambdaContextFile": getLambdaContextFile,
|
130 | "getLambdaEventFile": getLambdaEventFile,
|
131 | "init": init,
|
132 | "runLocally": runLocally,
|
133 | "runningLocally": runningLocally
|
134 | } |
\ | No newline at end of file |