-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-dockerfile.js
More file actions
46 lines (36 loc) · 1.6 KB
/
template-dockerfile.js
File metadata and controls
46 lines (36 loc) · 1.6 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
const fs = require('fs')
const path = require('path')
// CODE_PATH PACKAGE_JSON_PATH BUN_LOCK_PATH FILE_NAME
const args = process.argv.slice(2);
const templates = {
CODE_PATH: args[0],
PACKAGE_JSON_PATH: args[1],
BUN_LOCK_PATH: args[2],
LAMBDA_FUNCTION_HANDLER: args[3],
}
const FILE_NAME = args[4]
const COMPILE_CODE = args[5];
const DOCKERFILE_TEMPLATE_PATH = path.join(__dirname, "Dockerfile.template");
const COMPILE_BUILD = `RUN bun build --compile /var/task/${templates.LAMBDA_FUNCTION_HANDLER} --outfile lambda`;
const COMPILE_COMMAND = `CMD ["/var/task/lambda"]`;
const TRANSPILE_COMMAND = `CMD ["bun", "run", "/var/task/${templates.LAMBDA_FUNCTION_HANDLER}"]`;
function templateDockerfile() {
let templateFile = fs.readFileSync(DOCKERFILE_TEMPLATE_PATH, 'utf8');
Object.keys(templates).forEach(template => {
templateFile = templateFile.replaceAll(`{{${template}}}`, templates[template]);
});
if (COMPILE_CODE === "true") {
templateFile = templateFile.replaceAll(`{{COMPILE}}`, COMPILE_BUILD);
templateFile = templateFile.replaceAll(`{{COMMAND}}`, COMPILE_COMMAND);
templateFile = templateFile.replaceAll(`{{BUN_INSTALL}}`, ``);
} else {
templateFile = templateFile.replaceAll(`{{COMPILE}}`, "");
templateFile = templateFile.replaceAll(`{{COMMAND}}`, TRANSPILE_COMMAND);
templateFile = templateFile.replaceAll(`{{BUN_INSTALL}}`, `COPY --from=bun_latest /usr/local/bin/bun /usr/local/bin/bun`);
}
const fileName = path.join(__dirname, `${FILE_NAME}.Dockerfile`);
fs.writeFileSync(fileName, templateFile);
}
if (require.main === module) {
templateDockerfile();
}