Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible improvement on Docker support #111

Open
lextm opened this issue May 24, 2022 · 1 comment
Open

Possible improvement on Docker support #111

lextm opened this issue May 24, 2022 · 1 comment

Comments

@lextm
Copy link

lextm commented May 24, 2022

Currently the steps documented for Docker support do not quite match real world setup,

  • It's not quite common to use a Node image for production deployment.
  • yarn start clearly is using the debug build.

To support a commonly used base image such as nginx, which does not have Node installed, the challenge is how to generate __ENV.js on the fly without Node.

I attached a shell script below that works in a similar way,

#!/bin/sh

# react-env.sh

scriptname="./__ENV.js"
sourcename="$1"

if [ -z "$sourcename" ]; then
  current=$(eval "echo \"\$ENVIRONMENT\"")
  if [ -z "$current" ]; then
    sourcename=".env"
  else
    sourcename=".env.$current"
  fi
fi

# Recreate config file
rm -rf "$scriptname"
touch "$scriptname"

# Add assignment 
echo "window.__ENV = {" >> "$scriptname"

# Read each line in .env file
# Each line represents key=value pairs
while read -r line || [ -n "$line" ];
do
  if printf '%s\n' "$line" | grep -q -e '^#.*'; then
    continue
  fi

  [ -z "$line" ] && continue

  # 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
  env=$(eval "echo \"\$$varname\"")
  value=$(printf '%s\n' "$env")
  # Otherwise use value from .env file
  [ -z "$value" ] && value=${varvalue}
  
  # Append configuration property to JS file
  echo "  $varname: \"$value\"," >> "$scriptname"
done < "$sourcename"

echo "}" >> "$scriptname"

It is written in POSIX sh syntax, as many base images don't have bash.

@christian-draeger
Copy link

would be really nice to see this as a build in feature of react-env

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants