-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.js
More file actions
77 lines (67 loc) · 2.77 KB
/
Copy pathtransform.js
File metadata and controls
77 lines (67 loc) · 2.77 KB
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
const fs = require('fs');
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;
const t = require('@babel/types');
// Function to transform a file using AST
function transformFile(filePath) {
const code = fs.readFileSync(filePath, 'utf8');
// Parse the code into an AST
const ast = parser.parse(code, {
sourceType: 'module',
plugins: ['typescript']
});
// Traverse the AST to apply transformations
traverse(ast, {
FunctionDeclaration(path) {
// Replace specific function declarations
if (path.node.id && ['GET', 'POST', 'OPTION', 'PATCH', 'DELETE'].includes(path.node.id.name)) {
path.node.id.name = 'handler';
path.node.async = true; // Ensure the function is async
path.node.params = [t.identifier('event'), t.identifier('context')]; // Change parameters
}
},
VariableDeclarator(path) {
if (path.node.id.name === 'body' && path.node.init.type === 'AwaitExpression' &&
path.node.init.argument.type === 'CallExpression' &&
path.node.init.argument.callee.object.name === 'request' &&
path.node.init.argument.callee.property.name === 'json') {
path.node.init.argument.callee.object.name = 'event';
}
},
VariableDeclaration(path) {
if (path.node.declarations[0].id.name === 'queryParams' &&
path.node.declarations[0].init.type === 'MemberExpression' &&
path.node.declarations[0].init.object.name === 'request' &&
path.node.declarations[0].init.property.name === 'query') {
path.node.declarations[0].init.object.name = 'event';
}
},
ReturnStatement(path) {
// Logic to modify return statements for Lambda format
if (path.node.argument && path.node.argument.type === 'CallExpression' &&
path.node.argument.callee.property &&
path.node.argument.callee.property.name === 'json') {
let responseBody = path.node.argument.arguments[0];
let responseOptions = path.node.argument.arguments[1];
// Create the new return statement for Lambda
let newReturnStatement = t.returnStatement(
t.objectExpression([
t.objectProperty(t.identifier('statusCode'), responseOptions ?
t.memberExpression(responseOptions, t.identifier('status')) : t.numericLiteral(200)),
t.objectProperty(t.identifier('headers'), t.objectExpression([
t.objectProperty(t.stringLiteral('Content-Type'), t.stringLiteral('application/json'))
])),
t.objectProperty(t.identifier('body'), t.callExpression(
t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [responseBody]))
])
);
path.replaceWith(newReturnStatement);
}
}
});
// Generate the modified code
const output = generate(ast, {}, code);
fs.writeFileSync(filePath, output.code);
}
module.exports = { transformFile };