-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathenv.sh
More file actions
executable file
·63 lines (56 loc) · 2.01 KB
/
Copy pathenv.sh
File metadata and controls
executable file
·63 lines (56 loc) · 2.01 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
#!/bin/bash
# Recreate config file
rm -rf ./config.js
touch ./config.js
# Start JSON object
echo "window.SERVER_CONF = {" >> ./config.js
# Check if .env file exists
if [[ -f .env ]]; then
# 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/=.*//' | xargs)
varvalue=$(printf '%s\n' "$line" | sed -e 's/^[^=]*=//' | xargs)
fi
if [[ -n "$varname" ]]; then
# 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 if varname and value are not empty
echo " \"$varname\": \"$value\"," >> ./config.js
fi
done < .env
else
# If .env does not exist, read from config.example.js
if [[ -f config.example.js ]]; then
# Extract keys from config.example.js using grep and sed
keys=$(awk -F: '/^[[:space:]]*["'"'"']?[A-Za-z_$][A-Za-z0-9_$]*["'"'"']?[[:space:]]*:/{gsub(/["'"'"']/, "", $1); print $1}' config.example.js)
if [[ -n "$keys" ]]; then
for varname in $keys; do
if [[ -n "$varname" ]]; then
value=$(printf '%s\n' "${!varname}")
# Use value from environment variables if available, otherwise use empty string
[[ -z $value ]] && value=""
if [[ -n "$value" ]]; then
echo " \"$varname\": \"$value\"," >> ./config.js
fi
fi
done
else
echo "Error: Failed to extract keys from config.example.js. Ensure it is a valid JS file with key-value pairs." >&2
exit 1
fi
fi
fi
# Remove the trailing comma from the last line and close the object properly
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' '$ s/,$//' ./config.js
else
sed -i '$ s/,$//' ./config.js
fi
# End JavaScript object
echo "};" >> ./config.js