1
+ #! /usr/bin/env bash
2
+
3
+ # ======================================================================= #
4
+ # Shell Params #
5
+ # ======================================================================= #
6
+ set -o errexit
7
+ set -e
8
+ shopt -s expand_aliases
9
+
10
+
11
+ # ======================================================================= #
12
+ # Setup #
13
+ # ======================================================================= #
14
+ DC=" ${DC:- exec} "
15
+
16
+ # If we're running in CI we need to disable TTY allocation for docker compose
17
+ # commands that enable it by default, such as exec and run.
18
+ TTY=" "
19
+ if [ ! -t 1 ]; then
20
+ TTY=" -T"
21
+ fi
22
+
23
+ # ======================================================================= #
24
+ # Utilities #
25
+ # ======================================================================= #
26
+ check_executable () {
27
+ executable_name=$1 ;
28
+
29
+ print_style " Checking $executable_name ... " " info"
30
+
31
+ if ! command -v " $executable_name " & > /dev/null; then
32
+ print_style " Missing" " danger" ;
33
+ exit 1;
34
+ fi
35
+
36
+ print_style " $( command -v " $executable_name " ) ; \n" " success" ;
37
+ }
38
+
39
+
40
+ generate_secret () {
41
+ length=$1 ;
42
+
43
+ LC_ALL=C tr -dc \
44
+ ' A-Za-z0-9!"#$%&' \' ' ()*+,-./:;<=>?@[\]^_`{|}~' < /dev/urandom \
45
+ | head -c " $length " ; echo
46
+ }
47
+
48
+ generate_safer_secret () {
49
+ length=$1 ;
50
+
51
+ LC_ALL=C tr -dc \
52
+ ' A-Za-z0-9' < /dev/urandom \
53
+ | head -c " $length " ; echo
54
+ }
55
+
56
+ print_style () {
57
+ if [ " $2 " = " info" ] ; then
58
+ COLOR=" 96m" ;
59
+ elif [ " $2 " = " success" ] ; then
60
+ COLOR=" 92m" ;
61
+ elif [ " $2 " = " warning" ] ; then
62
+ COLOR=" 93m" ;
63
+ elif [ " $2 " = " danger" ] ; then
64
+ COLOR=" 91m" ;
65
+ else # default color
66
+ COLOR=" 0m" ;
67
+ fi
68
+
69
+ STARTCOLOR=" \e[$COLOR " ;
70
+ ENDCOLOR=" \e[0m" ;
71
+
72
+ printf " $STARTCOLOR %b$ENDCOLOR " " $1 " ;
73
+ }
74
+
75
+ # ======================================================================= #
76
+ # Migration #
77
+ # ======================================================================= #
78
+ _build_migration_command () {
79
+ # ? I know I know. Try using 'npx' yourself and see what happens.
80
+
81
+ echo ./node_modules/.bin/typeorm-ts-node-commonjs \
82
+ -d " $MIGRATION_CONF_FILE " \
83
+ " migration:$1 " \
84
+ " $MIGRATION_ROOT_DIR /" " $2 " " _" " $3 "
85
+ }
86
+
87
+ _get_last_migration_code () {
88
+ source " ./.env.development"
89
+
90
+ find " $MIGRATION_ROOT_DIR " -type f -name " [0-9][0-9][0-9][0-9]*.ts" |
91
+ awk -F_ ' {print $NF}' |
92
+ cut -d ' .' -f 1 |
93
+ sort -r |
94
+ head -n 1
95
+ }
96
+
97
+ _generate_next_migration_code () {
98
+ # shellcheck source=./.env.development
99
+ source " ./.env.development"
100
+
101
+ last_migration_code=$( _get_last_migration_code)
102
+
103
+ printf " %04d\n" $(( 10 #$last_migration_code + 1 ))
104
+
105
+ }
106
+
107
+ _escape ()
108
+ {
109
+ case $# in 0) return 0; esac
110
+ (
111
+ while :
112
+ do
113
+ printf " '"
114
+ unescaped=$1
115
+ while :
116
+ do
117
+ case $unescaped in
118
+
119
+ * \' * )
120
+ printf %s " ${unescaped%% \' * } " " '\''"
121
+ unescaped=${unescaped#* \' }
122
+ ;;
123
+ * )
124
+ printf %s " $unescaped "
125
+ break
126
+ esac
127
+ done
128
+ shift
129
+ case $# in 0) break ; esac
130
+ printf " ' "
131
+ done
132
+ printf " '\n"
133
+ )
134
+ }
135
+
136
+ _sanitize_migration_name () {
137
+ migration_name=" $1 "
138
+
139
+ if [ -z " $1 " ]; then
140
+ echo " Missing migration name."
141
+ exit 1
142
+ fi
143
+
144
+ _escape " $migration_name " | \
145
+ tr -d ' [:punct:]' | \
146
+ tr ' [:upper:]' ' [:lower:]' | \
147
+ tr ' ' ' _'
148
+ }
149
+
150
+ migration_create_empty () {
151
+ migration_name=$( _sanitize_migration_name " $1 " )
152
+
153
+ . " ./.env.development"
154
+
155
+ printf " Creating Empty Migration %s inside %s\n" " $migration_name " " $MIGRATION_ROOT_DIR "
156
+
157
+ npx typeorm-ts-node-commonjs migration:create " $MIGRATION_ROOT_DIR /$1 " " _" " $( _generate_next_migration_code) "
158
+ }
159
+
160
+ migration_generate () {
161
+ migration_name=$( _sanitize_migration_name " $1 " )
162
+
163
+ source " ./.env.development"
164
+
165
+ next_migration_code=$( _generate_next_migration_code)
166
+ migration_command=$( _build_migration_command " generate" " $migration_name " " $next_migration_code " )
167
+
168
+ printf " Generating Migration Number %s\n" " $next_migration_code "
169
+
170
+ exec $migration_command
171
+ }
172
+
173
+ # migration_revert() {
174
+ # . "./.env.development"
175
+
176
+ # last_migration_code=$(_get_last_migration_code)
177
+ # migration_command=$(_build_migration_command "revert" "$last_migration_code")
178
+
179
+ # printf "Reverting Migration Number %s\n" "$last_migration_code"
180
+
181
+ # exec "$migration_command"
182
+ # }
183
+
184
+ migration_run () {
185
+ # shellcheck source=.env.production
186
+ # shellcheck source=.env.development
187
+ source " ./.env.$NODE_ENV "
188
+
189
+ migration_command=$( _build_migration_command " run" " " )
190
+
191
+ printf " Running Migrations..."
192
+
193
+ npx typeorm-ts-node-commonjs migration:run \
194
+ -d " $MIGRATION_CONF_FILE "
195
+
196
+ }
197
+
198
+
199
+ # ======================================================================= #
200
+ # Help!!! #
201
+ # ======================================================================= #
202
+ help () {
203
+ printf " %s <task> [args]\n\nTasks:\n" " ${0} "
204
+
205
+ compgen -A function | grep -v " ^_" | cat -n
206
+
207
+ printf " \nExtended help:\n Each task has comments for general usage\n"
208
+ }
209
+
210
+ TIMEFORMAT=$' \n Task completed in %3lR'
211
+ " ${@:- help} "
0 commit comments