-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·348 lines (279 loc) · 7.97 KB
/
run
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env bash
set -euo pipefail
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
. "${current_dir}/scripts/run/css.sh"
. "${current_dir}/scripts/run/docker_file.sh"
. "${current_dir}/scripts/run/git.sh"
. "${current_dir}/scripts/run/html.sh"
. "${current_dir}/scripts/run/javascript.sh"
. "${current_dir}/scripts/run/markdown.sh"
. "${current_dir}/scripts/run/prettier.sh"
. "${current_dir}/scripts/run/release.sh"
. "${current_dir}/scripts/run/shell.sh"
. "${current_dir}/scripts/run/spellcheck.sh"
. "${current_dir}/scripts/lib/utils.sh"
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
function help {
cat <<EOF
$0 [command] [OPTIONS] [sub-command]
Example: $0 --dc-dev dc:exec ./run test
Options:
-h, --help Print this message
--dc-ci Use "ci" file when docker compose
--dc-dev Use "dev" file when docker compose
Main commands:
help Print this message
check Test and lint the whole project
format Format code of the whole project
install:deps Install all dependencies
lint Lint the whole project
lint:staged Lint staged files
pre-commit Lint staged files and test the whole project
test Test the whole project
docker compose commands:
dc:build Build all docker services
dc:console Build, start, up all services and console of "app" docker service
dc:exec Execute a command in a "app" docker service
dc:run Run a command inside the "app" docker service and remove stopped containers
dc:up Starts all docker services
EOF
help:release
help:css
help:html
help:js
help:markdown
help:prettier
help:shell
help:dockerfile
help:spellcheck
help:git
}
# —————————————————————————————————————————————— #
# Helper functions #
# —————————————————————————————————————————————— #
# Check if the command is from the docker container or the host.
function is_inside_docker {
[[ ${INSIDE_DOCKER:-false} == true ]]
}
function is_ci {
[[ "${CI:-false}" == true ]]
}
# If we're running in CI we need to disable TTY allocation for docker-compose
# commands that enable it by default, such as exec and run.
# Idea from: https://github.com/nickjj/docker-flask-example/blob/main/run
TTY=""
if [[ ! -t 1 ]] || is_ci; then
TTY="-T"
fi
# —————————————————————————————————————————————— #
# Application #
# —————————————————————————————————————————————— #
# TODO: Call your application tests below
test_checks=$(
cat <<EOF
test:release
EOF
)
lint_checks=$(
cat <<EOF
lint:css
lint:dockerfile
lint:html
lint:js
lint:markdown
lint:prettier
lint:shell
lint:spellcheck
EOF
)
function check {
local status=0
local all_checks
all_checks=$(echo "${test_checks}" && echo "${lint_checks}")
# Options
for opt in "${@}"; do
case ${opt} in
--include)
local param_value="${2:-}"
if [[ -z "${param_value}" ]]; then alert_die "'--include' should contains a string"; fi
all_checks=$(echo "${all_checks}" && echo "${param_value}")
shift 2
;;
-h | --help)
cat <<EOF
Test and lint the whole project
$0 check [OPTIONS]
Example: $0 check
Options:
--include cmd1 Include an additional check from "./run"
-h, --help Print this message
EOF
close
;;
esac
done
# Run all checks concurrently
echo "${all_checks}" \
| parallel --keep-order --tagstring "[{1}]" "./run {1}" \
|| status=${?}
if [[ ${status} -ne 0 ]]; then
alert "✖ Project checked: issue(s) found"
exit ${status}
fi
success "✔ Project checked"
}
function test {
if is_inside_docker; then
local status=0
# Run all tests concurrently
echo "${test_checks}" | parallel --keep-order --tagstring "[{1}]" "./run {1}" \
|| status=${?}
if [[ ${status} -ne 0 ]]; then
alert "✖ Project tested: issue(s) found"
exit ${status}
fi
success "✔ Project tested"
else
dc:exec ./run test
fi
}
function lint {
# shellcheck disable=SC2119
if is_inside_docker; then
local status=0
# Run all lints concurrently
echo "${lint_checks}" | parallel --keep-order --tagstring "[{1}]" "./run {1}" \
|| status=${?}
if [[ ${status} -ne 0 ]]; then
alert "✖ Project linted: issue(s) found"
exit ${status}
fi
success "✔ Project linted"
else
dc:exec ./run lint
fi
}
function format {
# shellcheck disable=SC2119
if is_inside_docker; then
local status=0
# Run all formatters concurrently
(
cat <<EOF
format:css
format:dockerfile
format:js
format:shell
EOF
) | parallel --keep-order --tagstring "[{1}]" "./run {1}" \
|| status=${?}
# Prettier format should be done after CSS and JS format
print "\e[30;47m———[prettier]———\e[0m"
./run format:prettier || status=${?}
if [[ ${status} -ne 0 ]]; then
alert "✖ Project could not be formatted completely: issue(s) found"
exit ${status}
fi
success "✔ Project formatted"
else
dc:exec ./run format
fi
}
function pre-commit {
if is_inside_docker; then
local status=0
local pre_commit_checks
pre_commit_checks=$(echo "${test_checks}" && echo lint:staged)
# Run all checks concurrently
echo "${pre_commit_checks}" \
| parallel --keep-order --tagstring "[{1}]" "./run {1}" \
|| status=${?}
if [[ ${status} -ne 0 ]]; then
alert "✖ Project checked: issue(s) found"
exit ${status}
fi
success "✔ Project checked"
else
dc:exec ./run pre-commit
fi
}
function lint:staged {
npx lint-staged
}
function install:deps {
if is_inside_docker; then
npm ci --quiet
success "✔ Dependencies installed"
else
dc:exec ./run install:deps
fi
}
# —————————————————————————————————————————————— #
# docker compose #
# —————————————————————————————————————————————— #
dc_options=()
ci_dc_options=(
-f docker-compose.yml
-f docker-compose.ci.yml
)
dev_dc_options=(
-f docker-compose.yml
-f docker-compose.dev.yml
)
function dc {
docker compose "${dc_options[@]}" "${@}"
}
function dc:build {
docker buildx bake "${dc_options[@]}"
}
function dc:exec {
dc exec ${TTY} app "${@}"
}
function dc:run {
dc run ${TTY} app "${@}"
dc rm -f -v
}
function dc:up {
dc up -d --remove-orphans
}
function dc:console {
dc:build
dc:up
dc:exec bash
}
# —————————————————————————————————————————————— #
# run #
# —————————————————————————————————————————————— #
opt_dc_ci=
opt_dc_dev=
# Options
for opt in "${@}"; do
case ${opt} in
--dc-ci)
if [[ -n ${opt_dc_dev} ]]; then
alert_die "--dc-ci is not compatible with --dc-dev"
fi
dc_options=("${ci_dc_options[@]}")
opt_dc_ci=1
shift
;;
--dc-dev)
if [[ -n ${opt_dc_ci} ]]; then
alert_die "--dc-dev is not compatible with --dc-ci"
fi
dc_options=("${dev_dc_options[@]}")
opt_dc_dev=1
shift
;;
-h | --help)
if [[ "${#}" -eq 1 ]]; then
help
close
fi
;;
esac
done
# Idea from: https://github.com/adriancooney/Taskfile
TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"