-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-cli.js
executable file
·181 lines (171 loc) · 4.83 KB
/
run-cli.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env node
'use strict'
const { prompt } = require('inquirer')
const execa = require('execa')
const chalk = require('chalk')
async function cli () {
const shell = (command, opt) => {
console.log(chalk.cyan(`executing ${command}`))
return execa.command(command, { shell: '/bin/bash', ...opt })
}
while (true) {
const { option } = await prompt({
type: 'list',
name: 'option',
choices: [
'exit',
'interactive shell',
'local cleanup',
'start offline',
'deploy',
'remove deploy',
'create fake data',
'replay from archive',
'query DynamoDB'
]
})
if (option === 'exit') {
break
} else if (option === 'local cleanup') {
const { deleteDb } = await prompt({
name: 'deleteDb',
type: 'confirm',
default: false,
message: 'delete sqlite db [n]?'
})
if (deleteDb) {
await shell('rm -rf db', { stdio: 'inherit' })
}
const { composeDown } = await prompt({
name: 'composeDown',
type: 'confirm',
default: true,
message: 'docker-compose down [y]?'
})
if (composeDown) {
await shell('./cli.sh stop', { stdio: 'inherit' })
break
}
} else if (
option === 'start offline' ||
option === 'deploy' ||
option === 'remove deploy'
) {
if (option === 'start offline') {
await shell(
`
if [[ -z $(docker ps -q --filter 'name=dynamodb-logs-dynamodb') ]]; then
docker-compose up -d
npm exec sls dynamodb migrate -- --stage=local -c dynamodb.local.yml
fi
`,
{ stdio: 'inherit' }
)
}
const { nodeOrPython } = await prompt({
type: 'list',
name: 'nodeOrPython',
choices: ['node', 'python']
})
if (nodeOrPython === 'node') {
if (option === 'start offline') {
await shell(
'npm exec sls offline start -- --stage=local -c serverless-node.yml',
{ stdio: 'inherit' }
)
} else if (option === 'deploy') {
await shell(
'npm exec sls deploy -- --stage dev -c serverless-node.yml',
{
stdio: 'inherit'
}
)
} else if (option === 'remove deploy') {
await shell(
`
export external_ip4_address=unknown
npm exec sls remove -- --stage dev -c serverless-local-archive-run.yml
`,
{
stdio: 'inherit'
}
)
await shell(
'npm exec sls remove -- --stage dev -c serverless-node.yml',
{
stdio: 'inherit'
}
)
}
} else if (nodeOrPython === 'python') {
await shell(
`
if ! [[ -d venv ]]; then
virtualenv venv
fi
. venv/bin/activate
pip3 install --no-cache-dir -r requirements.txt
`,
{ stdio: 'inherit' }
)
if (option === 'start offline') {
await shell(
`
. venv/bin/activate
npm exec sls offline start -- --stage=local -c serverless-python.yml
`,
{ stdio: 'inherit' }
)
} else if (option === 'deploy') {
await shell(
`
. venv/bin/activate
npm exec sls deploy -- --stage dev -c serverless-python.yml
`,
{ stdio: 'inherit' }
)
} else if (option === 'remove deploy') {
await shell(
`
export external_ip4_address=unknown
npm exec sls remove -- --stage dev -c serverless-local-archive-run.yml
`,
{
stdio: 'inherit'
}
)
await shell(
`
. venv/bin/activate
npm exec sls remove -- --stage dev -c serverless-python.yml
`,
{ stdio: 'inherit' }
)
}
}
} else if (option === 'create fake data') {
await shell(`./create-fake-data.js`, { stdio: 'inherit' })
} else if (option === 'replay from archive') {
await shell(`./run-archive.js`, { stdio: 'inherit' })
} else if (option === 'query DynamoDB') {
const { env } = await prompt({
type: 'list',
name: 'env',
message: 'environment?',
choices: ['local', 'aws']
})
if (env === 'local') {
await shell(`./dynamodb-local-query.sh`, { stdio: 'inherit' })
} else if (env === 'aws') {
await shell('npm exec dynamodb-query', { stdio: 'inherit' })
}
} else {
await shell('bash', {
stdio: 'inherit',
env: { ...process.env, __INTERACTIVE: 1 }
})
break
}
}
}
cli().catch(console.error)