-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
13 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/bin/bash | ||
|
||
# Convert the env files generated by the downloadEnv container into js objects. | ||
# Run by the frontend container prior to nginx startup | ||
tupaia_env_dir=${TUPAIA_ENV_DIR:-/env/packages} | ||
tupaia_package_dir=${TUPAIA_PACKAGE_DIR:-/home/ubuntu/tupaia/packages} | ||
|
||
function env-js() { | ||
# Read $1 and write to $2 as a js object `window._env__` | ||
# Adapted from | ||
# https://www.freecodecamp.org/news/how-to-implement-runtime-environment-variables-with-create-react-app-docker-and-nginx-7f9d42a91d70 | ||
env_src=$1 | ||
env_js=$2 | ||
|
||
echo "window.env = {" > "$env_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 | ||
# otherwise use value from .env file | ||
value=${!varname:-$varvalue} | ||
# Append configuration property to JS file | ||
echo " $varname: \"$value\"," >> "$env_js" | ||
done < "$env_src" | ||
|
||
echo "}" >> "$env_js" | ||
} | ||
|
||
|
||
for p in "$tupaia_package_dir"/*; do | ||
package=$(basename "$p") | ||
if [ -f "$tupaia_env_dir/$package/.env" ]; then | ||
echo "creating $p/.env-config.js" | ||
env-js "$tupaia_env_dir/$package/.env" "$p/.env-config.js" | ||
fi | ||
done |