-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.sh
executable file
·86 lines (65 loc) · 2.17 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
#!/bin/bash
# Recreate config file
rm -rf ./env-config.js
touch ./env-config.js
echo "---Creating env-config.js file for dynamic environment variables---"
ENV_FILE=.env
# only if there is no .env file, do we use the .env.example file provided as a template
if [ -f $ENV_FILE ]; then
echo "env file $ENV_FILE exists."
else
echo "env file $ENV_FILE does not exist. Using provided defaults"
cp .env.example $ENV_FILE
if [ -z "${APOLLO_KEY}" ]; then
echo "Missing Apollo Key Env"
else
echo APOLLO_KEY="$APOLLO_KEY" >> "$ENV_FILE"
fi
if [ -z "${API_URL}" ]; then
echo "Missing API URL Env"
else
echo API_URL="$API_URL" >> "$ENV_FILE"
fi
if [ -z "${SENTRY_DSN}" ]; then
echo "Missing Sentry DSN"
else
echo SENTRY_DSN="$SENTRY_DSN" >> "$ENV_FILE"
fi
fi
# Add assignment
echo "window._env_ = {" >> ./env-config.js
# Read each line in .env file
# Each line represents key=value pairs
while read -r line || [[ -n "$line" ]];
do
# Split env variables by character `=`
if printf '%s\n' "$line" | grep -q -e '='; then
varname=$(printf '%s\n' "$line" | sed -e 's/=.*//')
varvalue=$(printf '%s\n' "$line" | sed -e 's/^[^=]*=//')
fi
# 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
done < $ENV_FILE
echo "}" >> ./env-config.js
echo "---Done---"
# Alternatively the below script can also be used to perform above function
##!/bin/sh
## line endings must be \n, not \r\n !
#echo "---Creating env-config.js file for dynamic environment variables---"
#ENV_FILE=.env
#
## only if there is no .env file, do we use the .env.example file provided as a template
#if [ -f $ENV_FILE ]; then
# echo "env file $ENV_FILE exists."
#else
# echo "env file $ENV_FILE does not exist. Using provided default"
# cp .env.example $ENV_FILE
#fi
#echo "window._env_ = {" > ./env-config.js
#awk -F '=' '{ print $1 ": \"" (ENVIRON[$1] ? ENVIRON[$1] : $2) "\"," }' ./$ENV_FILE >> ./env-config.js
#echo "}" >> ./env-config.js
#echo "---Done---"