Created
August 24, 2019 09:42
-
-
Save vtenq/7894d3b2b091364f4a857e18390d69d6 to your computer and use it in GitHub Desktop.
Generate js file that will add process env values to window object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Recreate config file | |
rm -rf ./env-config.js | |
touch ./env-config.js | |
# Add assignment | |
echo "(() => {" >> ./env-config.js | |
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\"," >> ./dist/browser/env-config.js | |
done < .env | |
echo " }" >> ./env-config.js | |
echo "})();" >> ./env-config.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment