env.sh 834 B

1234567891011121314151617181920212223242526272829
  1. #!/bin/bash
  2. # Recreate config file
  3. rm -rf ./build/env-config.js
  4. touch ./build/env-config.js
  5. # Add assignment
  6. echo "window._env_ = {" >> ./build/env-config.js
  7. # Read each line in .env file
  8. # Each line represents key=value pairs
  9. while read -r line || [[ -n "$line" ]];
  10. do
  11. # Split env variables by character `=`
  12. if printf '%s\n' "$line" | grep -q -e '='; then
  13. varname=$(printf '%s\n' "$line" | sed -e 's/=.*//')
  14. varvalue=$(printf '%s\n' "$line" | sed -e 's/^[^=]*=//')
  15. fi
  16. # Read value of current variable if exists as Environment variable
  17. value=$(printf '%s\n' "${!varname}")
  18. # Otherwise use value from .env file
  19. [[ -z $value ]] && value=${varvalue}
  20. # Append configuration property to JS file
  21. echo " $varname: \"$value\"," >> ./build/env-config.js
  22. done < ./build/.env
  23. echo "}" >> ./build/env-config.js