-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-and-deploy.js
87 lines (76 loc) · 2 KB
/
build-and-deploy.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
/* eslint-disable @typescript-eslint/no-var-requires */
const inquirer = require('inquirer')
const esbuild = require('esbuild')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
async function main() {
const ui = new inquirer.ui.BottomBar()
const {functionName, action} = await inquirer.prompt([
{
type: 'list',
name: 'functionName',
message: 'Which lambda function to process?',
choices: ['tinkoff-get-status']
},
{
type: 'list',
name: 'action',
message: 'What shall we do?',
choices: [
{
key: '1',
name: 'Build and deploy',
value: 'buildAndDeploy'
},
{
key: '2',
name: 'Just build',
value: 'build'
}
],
default: 'buildAndDeploy'
}
])
ui.log.write(`Building ${functionName}...`)
await build(functionName)
ui.log.write(`🏗 Build complete.`)
if (action !== 'buildAndDeploy') {
return
}
ui.log.write(`Compressing...`)
await zip(functionName)
ui.log.write(`🗜 Created ${functionName}.zip`)
ui.log.write(`Deploying...`)
await deploy(functionName)
ui.log.write(`🌐 Deployed to AWS Lambda`)
ui.log.write(`Cleaning up...`)
await cleanup(functionName)
ui.log.write(`🎉 Done!`)
}
function build(functionName) {
return esbuild
.build({
entryPoints: [`./src/${functionName}/index.ts`],
bundle: true,
platform: 'node',
target: ['node14.0'],
external: ['aws-sdk'],
outdir: `./${functionName}`
})
.catch((error) => {
console.error(error)
process.exit(1)
})
}
function zip(functionName) {
return exec(`zip -j ${functionName}.zip ./${functionName}/index.js`)
}
function deploy(functionName) {
return exec(
`aws lambda update-function-code --function-name ${functionName} --zip-file fileb://${functionName}.zip`
)
}
function cleanup(functionName) {
return exec(`rm ${functionName}.zip & rm -rf ./${functionName}`)
}
main()