-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart.sh
More file actions
211 lines (192 loc) · 6.19 KB
/
start.sh
File metadata and controls
211 lines (192 loc) · 6.19 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
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
#!/bin/bash
# Defined some useful colors for echo outputs.
# Use BLUE for informational.
BLUE="\033[1;34m"
# Use Green for a successful action.
GREEN="\033[0;32m"
# Use YELLOW for warning informational and initiating actions.
YELLOW="\033[1;33m"
# Use RED for error informational and extreme actions.
RED="\033[1;31m"
# No Color (used to stop or reset a color).
NC='\033[0m'
# By default, set these variables to false.
build=false
docker=false
install=false
migrate=false
no_auto_start=false
reset=false
# Checks if a specific param has been passed to the script.
has_param() {
local term="$1"
shift
for arg; do
if [[ $arg == "$term" ]]; then
return 0
fi
done
return 1
}
# If the `build` flag is passed or if the `-d or --docker` flag, set docker to true.
if has_param '-b' "$@" || has_param '--build' "$@" || has_param '-d' "$@" || has_param '--docker' "$@"
then
>&2 echo -e "${BLUE}Dockerize!${NC}"
docker=true
fi
# If the `-b or --build` flag is passed, set build to true.
if has_param '-b' "$@" || has_param '--build' "$@"
then
>&2 echo -e "${BLUE}Build requested${NC}"
build=true
fi
# If the `-i or --install` flag is passed, set install to true.
if has_param '-i' "$@" || has_param '--install' "$@"
then
>&2 echo -e "${BLUE}Install dependencies requested${NC}"
install=true
fi
# If the `-m or --migrate` flag is passed, set migrate to true.
if has_param '-m' "$@" || has_param '--migrate' "$@"
then
>&2 echo -e "${BLUE}Migrate dependencies requested${NC}"
migrate=true
fi
# If the `-n or --no_auto_start` flag is passed, set no_auto_start to true.
if has_param '-n' "$@" || has_param '--no_auto_start' "$@"
then
>&2 echo -e "${BLUE}No auto start requested${NC}"
no_auto_start=true
fi
# If the `-r or --reset_env` flag is passed, set reset to true.
if has_param '-r' "$@" || has_param '--reset_env' "$@"
then
>&2 echo -e "${BLUE}Reset environment variables requested${NC}"
reset=true
fi
if [ "${reset}" = true ]
then
# Reset the environment variables.
source ./reset_env_vars.sh
else
# Set the environment variables.
source ./set_env_vars.sh
fi
if [ "${no_auto_start}" = true ]
then
# Ensure the NO_AUTO_START environment variable is set to true.
export NO_AUTO_START=true
fi
if [ "${docker}" = true ] && ( has_param '-p' "$@" || has_param '--prune' "$@" )
then
docker system prune --force
fi
if [ "${build}" = true ]
then
# Build and start the container.
docker compose up -d --build
elif [ "${docker}" = true ]
then
# Start the container.
docker compose up -d
else
if [ "${install}" = true ]
then
# Install dependencies.
yarn
fi
if [ "${migrate}" = true ]
then
# Run migrations.
yarn prisma:migrate:dev
fi
if [ -z ${NO_AUTO_START} ]
then
# Start the app.
yarn dev
fi
fi
# Only execute if the `NO_AUTO_START` variable has NOT been set and it is a `Dockerized` build.
if [[ -z ${NO_AUTO_START} && "${docker}" = true ]]
then
# If CTRL+C is pressed, ensure the progress background PID is stopped too.
function ctrl_c()
{
>&2 echo -e "${RED} => CTRL+C received, exiting${NC}"
# Stop the progress indicator.
kill $progress_pid
wait $progress_pid 2>/dev/null
# Cursor visible again.
tput cnorm
exit
}
function open_url()
{
[[ -x $BROWSER ]] && exec "$BROWSER" "$url"
path=$(which xdg-open || which gnome-open || which open || which start) && exec "$path" "$url"
>&2 echo -e "${YELLOW}Can't find the browser.${NC}"
}
# Creates a animated progress (a cursor growing taller and shorter)
function progress() {
# Make sure to use non-unicode character type locale. (That way it works for any locale as long as the font supports the characters).
local LC_CTYPE=C
local char="▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
local charwidth=3
local i=0
# Cursor invisible
tput civis
while sleep 0.1; do
i=$(((i + $charwidth) % ${#char}))
printf "%s" "${char:$i:$charwidth}"
echo -en "\033[1D"
done
}
# Pings the ap up to 35 times to see if it is available yet.
function check_status() {
local max_num_tries=35
local status_code=$(curl --write-out %{http_code} --silent --output /dev/null ${SERVER_DOMAIN}:${SERVER_PORT}${API_ROOT})
if [[ ${iterator} -lt ${max_num_tries} && ${status_code} -eq 200 ]]
then
# Stop the progress indicator.
kill $progress_pid
wait $progress_pid 2>/dev/null
# Cursor visible again.
tput cnorm
>&2 echo -e "${GREEN}The app is up at ${SERVER_PROTOCOL}://${SERVER_DOMAIN}:${SERVER_PORT}${API_ROOT}${NC}"
url=${SERVER_PROTOCOL}://${SERVER_DOMAIN}:${SERVER_PORT}${API_ROOT}
open_url
elif [[ ${iterator} -eq ${max_num_tries} ]]
then
# Stop the progress indicator.
kill $progress_pid
wait $progress_pid 2>/dev/null
# Cursor visible again.
tput cnorm
>&2 echo -e "${YELLOW}Did not work. Perhaps the app is taking a long time to start?${NC}"
else
echo -en "${chars:$iterator:1}" "\r"
sleep 1
((iterator++))
check_status
fi
}
# Start the progress indicator.
>&2 echo -e "${YELLOW}* Checking if the app is up at ${SERVER_DOMAIN}:${SERVER_PORT}${NC} ..."
progress &
# Set the progress indicator's PID to a variable.
progress_pid=$!
# This is a trap for CTRL+C
trap ctrl_c INT
# Check the status
iterator=0
check_status
elif [ "${docker}" = true ]
then
>&2 echo -e "${GREEN}The app container is built and running.\n- The app has not been started; it must be started manually.\n- Please see the README.md for more information.${NC}"
elif [ ! -z "$NO_AUTO_START" ]
then
>&2 echo -e "${GREEN}The app has not been started.${NC}"
>&2 echo -e "${YELLOW}The 'NO_AUTO_START' environment variable is set and must be unset to start the app moving forward.${NC}"
else
>&2 echo -e "${GREEN}The app has been stopped.${NC}"
fi