forked from baptisteArno/typebot.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.sh
161 lines (134 loc) · 4.4 KB
/
env.sh
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
#!/bin/bash
# env.sh - Dead-simple .env file reader and generator
# Tunghsiao Liu (t@sparanoid.com)
#
# Inspired by:
# - https://github.com/andrewmclagan/react-env
# - https://www.freecodecamp.org/news/7f9d42a91d70/
# - https://github.com/kunokdev/cra-runtime-environment-variables
#
# Features:
# - Designed to be used for Next.js app inside a Docker container (General
# React app should also work)
# - Read env file and generate __env.js file for runtime client use
# - Merge current environment variables passing to it (Useful for Docker images)
# - No dependencies (More like a lite version of react-env). This is important
# to keep our container image as small as possible.
#
# Usage:
# - General usage:
# $ ./env.sh
#
# - Replacing varaible:
# $ NEXT_PUBLIC_API_BASE=xxx ./env.sh
#
# - Enviroment variable not in whitelist will be discarded:
# $ BAD_ENV=zzz ./env.sh
#
# - Change script options:
# $ ENVSH_ENV="./.env.staging" ENVSH_OUTPUT="./public/config.js" ./env.sh
#
# - Use it inside Dockerfile:
# RUN chmod +x ./env.sh
# ENTRYPOINT ["./env.sh"]
#
# Debug:
# NEXT_PUBLIC_OB_ENV=123_from_fish NEXT_BAD_ENV=zzz NEXT_PUBLIC_OB_TESTNEW=testenv NEXT_PUBLIC_CODE_UPLOAD_SIZE_LIMIT=6666 ./env.sh
echo -e "env.sh loaded"
# Config
ENVSH_ENV="${ENVSH_ENV:-"./.env.production"}"
ENVSH_PREFIX="${ENVSH_PREFIX:-"NEXT_PUBLIC_"}"
ENVSH_PREFIX_STRIP="${ENVSH_PREFIX_STRIP:-true}"
# Can be `window.__env = {` or `const ENV = {` or whatever you want
ENVSH_PREPEND="${ENVSH_PREPEND:-"window.__env = {"}"
ENVSH_APPEND="${ENVSH_APPEND:-"}"}"
ENVSH_OUTPUT="${ENVSH_OUTPUT:-"./public/__env.js"}"
# Utils
__green() {
printf '\033[1;31;32m%b\033[0m' "$1"
}
__yellow() {
printf '\033[1;31;33m%b\033[0m' "$1"
}
__red() {
printf '\033[1;31;40m%b\033[0m' "$1"
}
__info() {
printf "%s\n" "$1"
}
__debug() {
ENVSH_VERBOSE="${ENVSH_VERBOSE:-"false"}"
if [ "$ENVSH_VERBOSE" == "true" ]; then
printf "ENVSH_VERBOSE: %s\n" "$1"
fi
}
ENVSH_SED="sed"
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "macOS detected, switching to gsed"
if command -v gsed >/dev/null 2>&1 ; then
__info "$(__green "Found"): $(gsed --version | head -n 1)"
else
__info "gsed not found, trying to install..."
if command -v brew >/dev/null 2>&1 ; then
__info "$(__green "Found"): $(brew --version | head -n 1)"
brew install gnu-sed
else
__info "$(__red "Homebrew not found, install it first: https://brew.sh/")"
exit 1;
fi
fi
ENVSH_SED="gsed"
fi
# Recreate config file
rm -f "$ENVSH_OUTPUT"
touch "$ENVSH_OUTPUT"
# Create an array from inline variables
matched_envs=$(env | grep ${ENVSH_PREFIX})
IFS=$'\n' read -r -d '' -a matched_envs_arr <<< "$matched_envs"
__info "Matched inline env:"
for matched_env in "${matched_envs_arr[@]}"; do
echo $matched_env
done
# Add assignment
echo "$ENVSH_PREPEND" >> "$ENVSH_OUTPUT"
# Check if file exists
[[ -f "$ENVSH_ENV" ]] || { echo "$ENVSH_ENV does not exist" ; exec "$@" ;}
# Process .env for runtime client use
__info "$(__green "Reading ${ENVSH_ENV}...")"
while IFS= read -r line
do
# Check if this line is a valid environment variable and matches our prefix
if printf '%s' "$line" | grep -e "=" | grep -e "$ENVSH_PREFIX"; then
# Read and apply environment variable if exists
# NOTE: <<< here operator not working with `sh`
awk -F '=' '{print $1 ": \"" (ENVIRON[$1] ? ENVIRON[$1] : $2) "\","}' \
<<< "$line" >> "$ENVSH_OUTPUT"
fi
done < "$ENVSH_ENV"
echo "$ENVSH_APPEND" >> "$ENVSH_OUTPUT"
# Strip prefix if needed
$ENVSH_PREFIX_STRIP && $ENVSH_SED -i'' -e "s~$ENVSH_PREFIX~~g" "$ENVSH_OUTPUT"
# NOTE: This step is not necessary because variables on pages inside the
# Next.js prod server won't be changed. They're already inlined during the
# build time.
for matched_env in "${matched_envs_arr[@]}"; do
echo $matched_env
done
for i in "${!matched_envs_arr[@]}"; do
IFS='=' read -ra key_arr <<< "${matched_envs_arr[$i]}"
key=${key_arr[0]}
if [[ "${matched_envs_arr[$i]}" = *"${key}"* ]]; then
index="$i"
__info "Got index from inline env: ${index}, replacing ${key}"
find "$ENVSH_ENV" -type f -exec $ENVSH_SED -i'' \
-e "s~$key=.*~${matched_envs_arr[$index]}~g" {} \;
fi
done
# Print result
__debug "$(__green "Done! Final result in ${ENVSH_OUTPUT}:")"
__debug "`cat "$ENVSH_OUTPUT"`"
__debug "$(__green "Done! Modified ${ENVSH_ENV}:")"
__debug "`cat "$ENVSH_ENV"`"
__info "$(__green "env.sh done\n")"
# Accepting commands (for Docker)
exec "$@"