-
Notifications
You must be signed in to change notification settings - Fork 9
/
awslambda2googlefunctions.js
139 lines (93 loc) · 2.92 KB
/
awslambda2googlefunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"use strict";
/*
Refs:
- AWS Lambda http://docs.aws.amazon.com/lambda/latest/dg/programming-model.html
- AWS Lambda https://cloud.google.com/functions/writing
- rewrite-js https://github.com/chrisdickinson/rewrite-js
- cssauron https://github.com/chrisdickinson/cssauron-falafel
- falafel https://github.com/substack/node-falafel
How to:
> cat lambda_function.js | rewrite-js awslambda2googlefunctions.js > google_function.js
*/
function convertIentifiers(node) {
//every "event" identifier becomes "data"
switch (node.name) {
case 'event':
if (node.parent && node.parent.type === 'MemberExpression' && node.parent.property == node) {
// special case ("event.event" -> "data.event")
return;
}
node.update('data');
break;
}
}
function _swapList(nodeList) {
// fetch names
var names = nodeList.map(function (node) {
return node.name;
});
// if both event and context in list
if (names.indexOf('event') !== -1 && names.indexOf('context') !== -1) {
// find them and swap their name
nodeList.forEach(function (node) {
switch (node.name) {
case 'event':
node.update('context');
break;
case 'context':
node.update('data'); // directly "data" (not "event")
break;
}
});
}
}
function swapFunctionParams(node) {
// if both event and context as params -> swap them
_swapList(node.params);
}
function swapFunctionArgs(node) {
// if both event and context as arguments -> swap them
_swapList(node.arguments);
}
function _findByParent(node, type) {
// recursively find node with parent of the given type
if (node && node.parent && node.parent.type === type) {
return node;
}
if (node && node.parent) {
return _findByParent(node.parent, type);
}
return null;
}
function _commentStatementByNode(node) {
// find the main block related to this node and comment it (should never be null)
var block = _findByParent(node, 'BlockStatement');
block.update("/* TODO: fix me!\n\t" + block.source() + "\n\t*/");
}
function convertFunctionLookup(node) {
// look for context lookups
switch (node.object.name) {
case 'context':
// map context methods
switch (node.property.name) {
case 'done':
break;
case 'succeed':
node.property.update('success');
break;
case 'fail':
node.property.update('failure');
break;
default:
// comment everything else
_commentStatementByNode(node);
}
break;
}
}
module.exports = {
'id': convertIentifiers,
'function lookup': convertFunctionLookup,
'function': swapFunctionParams,
'call': swapFunctionArgs
};