-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun
More file actions
executable file
·229 lines (210 loc) · 7.42 KB
/
run
File metadata and controls
executable file
·229 lines (210 loc) · 7.42 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env bash
# This script wraps the calls to docker compose run to fire up CLI commands within the runner container
# Safe error handling
set -Eeuo pipefail
# Entering project root directory
readonly projectRootDir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
cd "$projectRootDir"
# Clearer variable name for the current script
readonly thisScriptName="$0"
# The name of the container to run things, this needs to be configured in docker-compose.yml
readonly runnerContainerName="runner"
# TTY is enabled by default as it gives a nicer experience
# However for some applications, (eg git hooks) the TTY needs to be disabled
# To disable TTY, export DISABLE_RUN_TTY=1 before calling run
# eg DISABLE_RUN_TTY=1 ./run yarn next lint
readonly disableTty="$([[ "${DISABLE_RUN_TTY:-0}" == "1" ]] && echo '--no-TTY' || echo '')"
# This function will take an unlimited number of command arguments
# and run them inside the runner container
function composeRunBackend(){
local runCmd="docker compose run $disableTty --rm $runnerContainerName "
local commandsInsideContainer=${@}
set -x
$runCmd bash -c "$commandsInsideContainer"
set +x
}
# This function will take a port number and an unlimited number of command arguments
# and run them inside the runner container, with the port mapped
function composeRunBackendWithPort(){
local port=$1
local runCmd="docker compose run $disableTty -p $port:$port --rm $runnerContainerName "
local commandsInsideContainer=${@:2}
set -x
$runCmd bash -c "$commandsInsideContainer"
set +x
}
# This function will take an unlimited number of integer port args followed by an unlimited number of commands
# and run them inside the runner container, with the ports mapped
function composeRunBackendWithPorts(){
local portArgs=''
local commandsInsideContainer=''
local doingPorts=1
for curArg in "$@"; do
if [[ "1"=="$doingPorts" ]]; then
if [[ "$curArg" =~ ^[0-9]+$ ]]; then
portArgs="$portArgs -p $curArg:$curArg "
continue
else
doingPorts=0
fi
fi
commandsInsideContainer="$commandsInsideContainer $curArg"
done
local runCmd="docker compose run $disableTty $portArgs --rm $runnerContainerName "
set -x
$runCmd bash -c "$commandsInsideContainer"
set +x
}
function numberOfCpuCores(){
getconf _NPROCESSORS_ONLN
}
function numberOfWorkers(){
local divideCoresBy="${1:-2}"
echo $(($(numberOfCpuCores)/divideCoresBy))
}
function clearEverything(){
echo "Clearing Everything"
echo " - Removing containers + data"
docker compose down -v --remove-orphans
echo " - Removing .next directory"
rm -rf $projectRootDir/.next
echo " - Removing node_modules"
rm -rf $projectRootDir/node_modules
echo " - Truncating yarn-error.log"
echo '' > $projectRootDir/yarn-error.log
echo
echo "done..."
}
function cachedYarnInContainer(){
local commandsInsideContainer=${@}
mkdir -p "$projectRootDir/.next/cache/yarn"
composeRunBackend yarn --cache-folder .next/cache/yarn "$commandsInsideContainer"
}
# Some new lines for readability
echo
echo
# Copying Git User Config to Local
# Will not overwrite config already set
# This is required so that any git commands running inside containers have the correct user config
if [[ -d .git ]]; then
git config --local --get user.name > /dev/null || git config --local --add user.name "$(git config --global --get user.name)"
git config --local --get user.email > /dev/null || git config --local --add user.email "$(git config --global --get user.email)"
fi
# Getting argument
readonly arg=${1:-"'not set'"}
# Now we match the argument against various optoins
# Note, the structure of this block is important and indention must be preserved
# as this is used to autogenerate help
case "$arg" in
quickstart)
echo "Run all Quick Start Commands"
if [[ ! -f .env ]]; then
cp .env-dist .env;
fi
git pull
clearEverything
$thisScriptName up \
&& $thisScriptName install \
&& $thisScriptName generate \
&& $thisScriptName db-push \
&& $thisScriptName db-fill
echo "System full installed and database seeded"
$thisScriptName ps
;;
up)
echo "Start Docker Containers"
docker compose up -d
;;
down)
echo "Stop Docker Containers"
docker compose down
;;
destroy)
echo "Stop Docker Containers and Remove Data"
clearEverything
;;
ps)
echo "List of running Docker Containers"
docker compose ps
;;
i|install)
echo "Install all packages, set up Husky"
cachedYarnInContainer ""
;;
p|db-push)
echo "Force update DB to match Schema"
composeRunBackend yarn prisma db push
;;
m|db-migrate)
echo "Create and Run DB Migrations"
composeRunBackend yarn prisma migrate dev
;;
g|generate)
echo "Prisma Format Schema and Generate Client"
composeRunBackend yarn prisma format
composeRunBackend yarn prisma generate
;;
f|db-fill)
echo "Fill DB with Seed Data"
composeRunBackend yarn prisma db seed
;;
studio)
echo "Run Prisma Studio"
composeRunBackendWithPort 5555 yarn prisma studio --browser none --hostname 0.0.0.0
;;
yarn)
echo "Run Yarn Command"
cachedYarnInContainer ${@:2}
;;
d|dev)
echo "Run Dev Server for local development"
composeRunBackendWithPort 3000 yarn dev
;;
dbg|debug)
echo "Run Dev Server with Debugging for local development and debugging"
composeRunBackendWithPorts 9229 9230 3000 node --inspect-brk=0.0.0.0:9229 ./node_modules/.bin/next dev
;;
l|lint)
echo "Lint and Fix"
composeRunBackend yarn next lint --fix
;;
t|test)
echo "Run Jest Tests"
composeRunBackend yarn jest --max-workers $(numberOfWorkers)
;;
tw|testw)
echo "Jest Watch - Run Jest Tests Automatically on File Update"
composeRunBackend yarn jest --watch --max-workers $(numberOfWorkers)
;;
td|testd)
echo "Debug Jest Tests"
composeRunBackendWithPort 9229 node --inspect-brk=0.0.0.0:9229 ./node_modules/.bin/jest --runInBand --testTimeout=100000000
;;
tdw|twd|testdw)
echo "Jest Watch Debug - Debug Jest Tests, Rerun Automatically on File Update"
composeRunBackendWithPort 9229 node --inspect-brk=0.0.0.0:9229 ./node_modules/.bin/jest --runInBand --testTimeout=100000000 --watch
;;
nd|node-debug)
echo "Run node command (with debug)"
composeRunBackendWithPort 9229 node --inspect-brk=0.0.0.0:9229 ${@:2}
;;
n|node)
echo "Run node command"
composeRunBackend node ${@:2}
;;
prod|production)
echo "Build and Run Production Server"
$thisScriptName generate
composeRunBackend yarn next build && composeRunBackendWithPort 3000 yarn next start
;;
*)
echo "Help"
echo
echo "The $thisScriptName script has the following options"
echo
grep -Pzo '(?s)\s+?([a-z|-]+?)\)\s+?(?:echo ")[^"]+' "$thisScriptName" | sed -z 's/)\n echo "/: /g' | column -s : -t
;;
esac
# Some new lines for readability
echo
echo