-
Notifications
You must be signed in to change notification settings - Fork 5
/
env.sh
executable file
·37 lines (28 loc) · 1.27 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
#!/bin/bash
# This is only meant to be run on docker container environments,
# so that variables in the .env file can be redefined with the --env flag on
# container creation instead of with a new image build.
# This script will take the environment variables in the .env file and
# create a JS file with their values that is injected before React loads.
# The app will first try to read env values from this generated file, else fallback to .env file.
# Recreate config file
rm -rf ./env-config.js &>/dev/null
touch ./env-config.js
# Add assignment
echo "window._env_ = {" >>./env-config.js
# Read each line in .env file [key=value pairs]
while read -r line || [[ -n "$line" ]]; do
# Split env variables when character '=' is found
if printf '%s\n' "$line" | grep -q -e '='; then
varname=$(printf '%s\n' "$line" | sed -e 's/=.*//' | sed -e 's/REACT_APP_//') # Strip REACT_APP_ prefix
varvalue=$(printf '%s\n' "$line" | sed -e 's/^[^=]*=//')
# Read value of current variable if exists as Environment variable
value=$(printf '%s\n' "${!varname}")
# Otherwise use value from .env file
[[ -z $value ]] && value=${varvalue}
# Append configuration property to JS file
echo " $varname: \"$value\"," >>./env-config.js
fi
done \
<.env
echo "}" >>./env-config.js