-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.sh
62 lines (51 loc) · 1.23 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
#!/bin/sh
readonly ENV_FORMAT="window._env_"
readonly REACT_ENV_FILTER="REACT_APP_"
readonly REGEX_KEY="s/=.*//"
readonly REGEX_VALUE="s/^[^=]*=//"
readonly INPUT_FILE=./.env
readonly OUTPUT_FILE=./env.js
isReactEnv() {
( [ "${1%"${1#??????????}"}" = $REACT_ENV_FILTER ] && true ) || false
}
isKeyValuePair() {
( printf "%s\n" "$1" | grep -q -e '=' && true ) || false
}
getKey() {
isKeyValuePair "$1" && printf "%s\n" "$1" | sed -e "$REGEX_KEY"
}
getValue() {
isKeyValuePair "$1" && printf "%s\n" "$1" | sed -e "$REGEX_VALUE"
}
toJson() {
echo " $1: \"$2\"," >> $OUTPUT_FILE
}
ReadFromFile() {
while read -r line || [ -n "$line" ];
do
isReactEnv "$line" && (
key=$( getKey "$line" )
tempValue=$( getValue "$line")
eval value="\$$key"
[ -z "$value" ] && value=${tempValue}
toJson "$key" "$value"
)
done < $INPUT_FILE
}
ReadFromEnv() {
printenv | \
while read -r line || [ -n "$line" ];
do
isReactEnv "$line" && (
key=$( getKey "$line" )
value=$( getValue "$line")
toJson "$key" "$value"
)
done
}
rm -rf $OUTPUT_FILE
touch $OUTPUT_FILE
echo "$ENV_FORMAT = {" >> $OUTPUT_FILE
( [ -f $INPUT_FILE ] && ReadFromFile ) || ReadFromEnv
echo "}" >> $OUTPUT_FILE
exec "$@"