Created
May 18, 2021 14:26
-
-
Save lezhkin11/d6b0a14127b4920feaece278e5323ee1 to your computer and use it in GitHub Desktop.
Generate env-config.js based on .env file and ENVs variables. ENVs varibales overwrite .env
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/sh | |
# line endings must be \n, not \r\n ! | |
# Writes JS code which opens object literal and assigns it to the global window object. | |
# Reads each line of .env file and splits into key/value pair. | |
# Looks for the environment variable, if set, uses its value, otherwise, uses the default value from .env file. | |
# Removes all carriage returns (\r) | |
# Append it to object that we assigned to global window object. | |
# Close object literal | |
echo "window.env = {" > ./env-config.js | |
awk -F '=' '{gsub (/ |\r$/, "")}{ print $1 ": \"" (ENVIRON[$1] ? ENVIRON[$1] : $2) "\"," }' ./.env >> ./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