Skip to content

Instantly share code, notes, and snippets.

@lezhkin11
Created May 18, 2021 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lezhkin11/d6b0a14127b4920feaece278e5323ee1 to your computer and use it in GitHub Desktop.
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
#!/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