From f8d548fb36a7bce00db20483b18237a0e0e56835 Mon Sep 17 00:00:00 2001
From: flashwave <me@flash.moe>
Date: Fri, 11 Apr 2025 19:53:37 +0000
Subject: [PATCH] Switch to a .env file instead of config.json.

---
 .env.example               | 13 +++++++++++++
 .gitignore                 |  1 +
 Makefile                   |  9 +++++----
 README.md                  | 24 +++++++++++++++++-------
 build.js                   |  9 +++------
 config/config.example.json |  6 ------
 6 files changed, 39 insertions(+), 23 deletions(-)
 create mode 100644 .env.example
 delete mode 100644 config/config.example.json

diff --git a/.env.example b/.env.example
new file mode 100644
index 0000000..b404daf
--- /dev/null
+++ b/.env.example
@@ -0,0 +1,13 @@
+# Values contained below are also the defaults.
+
+# Enable debug mode
+# Primarily turns off minification of output styles, might also display more info in the console
+#AMI_DEBUG=1
+
+# Application title
+# Sets the title of the application, might be overridden for the window title by the common settings below once they're loaded
+#AMI_TITLE="Flashii Chat"
+
+# Common configuration location
+# Contains further information shared by the other chat client
+#FUTAMI_URL="//futami.flashii.net/common.json"
diff --git a/.gitignore b/.gitignore
index 4f5c8df..b9e009c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,4 @@
 /node_modules
 /public/assets
 /public/index.html
+/.env
diff --git a/Makefile b/Makefile
index ced0b74..a001949 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,7 @@ NODE_EXE := $(shell which node)
 NPM_EXE := $(shell which npm)
 
 BUILD_EXE := build.js
+ENV_FILE := .env
 
 all: install build
 
@@ -12,15 +13,15 @@ update:
 	${NPM_EXE} update --save
 
 build:
-	${NODE_EXE} ${BUILD_EXE}
+	${NODE_EXE} --env-file-if-exists ${ENV_FILE} ${BUILD_EXE}
 
 rebuild-css:
-	${NODE_EXE} ${BUILD_EXE} css
+	${NODE_EXE} --env-file-if-exists ${ENV_FILE} ${BUILD_EXE} css
 
 rebuild-html:
-	${NODE_EXE} ${BUILD_EXE} html
+	${NODE_EXE} --env-file-if-exists ${ENV_FILE} ${BUILD_EXE} html
 
 rebuild-js:
-	${NODE_EXE} ${BUILD_EXE} js
+	${NODE_EXE} --env-file-if-exists ${ENV_FILE} ${BUILD_EXE} js
 
 .PHONY: all install update build rebuild-css rebuild-html rebuild-js
diff --git a/README.md b/README.md
index a6810af..1cc6c69 100644
--- a/README.md
+++ b/README.md
@@ -4,17 +4,27 @@ Chat client forked from the original Sock Chat client. Maintained for compatibil
 
 ## Configuration
 
-Configuration consists of a `config.json` file that contains the various URLs and a default chat title (the unified one is taken from whatever the remote `common.json` supplies).
-Make sure you create the file before building. An example is also supplied.
+Configuration consists of a `.env` file that contains the various URLs and a default chat title (the unified one is taken from whatever the remote `common.json` supplies).
+The defaults supplied by `build.js` are what are used for the production instance. Make sure you modify them as needed!
 
 ## Building
 
-Ami uses a build script written in Node.js; make sure you have NPM available and then run the following commands.
+Ami uses a build script written in Node.js and a Makefile to make life easier.
+I recommend using something like `nvm` with a Node version of at least `v22.14.0 (LTS)`.
 
 ```sh
-# Installs packages without updating package.json or package-lock.json
-npm ci
+# Runs the package install and build recipes
+make [all]
 
-# Runs the build process, make sure to run this after updates or changes as well
-node build.js
+# Installs package versions expected by package-lock.json
+make install
+
+# Runs the build process
+make build
+
+# Updates latest versions of packages expected by package.json
+make update
+
+# Rebuild a specific type of asset for speedier development
+make rebuild-css / rebuild-html / rebuild-js
 ```
diff --git a/build.js b/build.js
index aa1f737..500e692 100644
--- a/build.js
+++ b/build.js
@@ -4,8 +4,7 @@ const fs = require('fs');
 const exec = require('util').promisify(require('child_process').exec);
 
 (async () => {
-    const config = JSON.parse(fs.readFileSync(pathJoin(__dirname, 'config/config.json')));
-    const isDebug = fs.existsSync(pathJoin(__dirname, '.debug'));
+    const isDebug = !!process.env.AMI_DEBUG;
 
     const env = {
         root: __dirname,
@@ -16,13 +15,11 @@ const exec = require('util').promisify(require('child_process').exec);
         housekeep: [ pathJoin(__dirname, 'public', 'assets') ],
         vars: {
             html: {
-                title: config.title,
+                title: process.env.AMI_TITLE ?? 'Flashii Chat',
             },
             build: {
                 FUTAMI_DEBUG: isDebug,
-                FUTAMI_URL: config.common_url,
-                MAMI_URL: config.modern_url,
-                AMI_URL: config.compat_url,
+                FUTAMI_URL: process.env.FUTAMI_URL ?? '//futami.flashii.net/common.json',
                 GIT_HASH: (await exec('git log --pretty="%H" -n1 HEAD')).stdout,
             },
         },
diff --git a/config/config.example.json b/config/config.example.json
deleted file mode 100644
index dabc3fe..0000000
--- a/config/config.example.json
+++ /dev/null
@@ -1,6 +0,0 @@
-{
-    "title": "Flashii Chat",
-    "common_url": "//futami.flashii.net/common.json",
-    "modern_url": "//chat.flashii.net",
-    "compat_url": "//sockchat.flashii.net"
-}