Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
30 changes: 0 additions & 30 deletions env.sh

This file was deleted.

43 changes: 43 additions & 0 deletions generate-env-browser.js
Original file line number Diff line number Diff line change
@@ -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, "};");
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down