diff --git a/Dockerfile b/Dockerfile index bd2dea33..3c212cde 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,9 +2,6 @@ # This image is around 50 megabytes FROM node:18-alpine3.18 AS builder -# Environment variable generation script needs bash -RUN apk add --no-cache bash - # Create app directory WORKDIR /app @@ -18,7 +15,7 @@ RUN npm ci --verbose RUN npm run build # Create environment variable js file -RUN chmod +x env.sh && ./env.sh +RUN node generate-env-browser.js ### STAGE 2: Run ### # This image is around 5 megabytes diff --git a/env.sh b/env.sh deleted file mode 100755 index 823f950d..00000000 --- a/env.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash -# (c) https://github.com/kunokdev/cra-runtime-environment-variables/blob/master/env.sh - -# Recreate config file -rm -rf ./env-config.js -touch ./env-config.js - -# Add assignment -echo "window._env_ = {" >> ./env-config.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 - value=$(printf '%s\n' "${!varname}") - # Otherwise use value from .env file - [[ -z $value ]] && value=${varvalue} - - # Append configuration property to JS file - echo " $varname: \"$value\"," >> ./env-config.js -done < .env - -echo "}" >> ./env-config.js \ No newline at end of file diff --git a/generate-env-browser.js b/generate-env-browser.js new file mode 100755 index 00000000..663dfc00 --- /dev/null +++ b/generate-env-browser.js @@ -0,0 +1,43 @@ +import fs from "node:fs"; +import process from "node:process"; + +const JS_FILE = "env-config.js"; +const ENV_FILE = ".env"; + +// Remove existing config file +if (fs.existsSync(JS_FILE)) { + fs.unlinkSync(JS_FILE); +} +// Create new file +fs.writeFileSync(JS_FILE, "", "utf-8"); + +// Add assignment +fs.appendFileSync(JS_FILE, "window._env_ = {\n"); + +// Read each line in .env file +// Each line represents key=value pairs +const envFile = fs.readFileSync(ENV_FILE, "utf-8"); +const lines = envFile.split("\n"); + +lines.forEach((line) => { + // Ignore comments, lines starting with hash + if (line.startsWith("#")) { + return; + } + // Split env variables by character `=` + if (line.includes("=")) { + const [varname, varvalue] = line.split("="); + + // Read value of current variable if exists as Environment variable + let value = process.env[varname] || varvalue; + // Otherwise use value from .env file + if (typeof value === "undefined") { + value = varvalue; + } + + // Append configuration property to JS file + fs.appendFileSync(JS_FILE, ` ${varname}: "${value}",\n`); + } +}); + +fs.appendFileSync(JS_FILE, "};"); diff --git a/package.json b/package.json index b20f8884..4bc13a5e 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "use-image": "^1.1.1" }, "scripts": { - "start": "chmod +x ./env.sh && sh ./env.sh && cp env-config.js ./public/ && vite", + "start": "node generate-env-browser.js && cp env-config.js ./public/ && vite", "test": "jest", "build": "vite build", "lint": "eslint --ignore-path .gitignore . --ext .ts,.tsx,.jsx,.js",