Override config directory with env var.

Until this change, the config directory was assumed
to be located at '/config'. This patch retains that
default behaviour, but enables users/devs to override
that behaviour by setting the HOMEPAGE_CONFIG_DIR
variable.
This commit is contained in:
Jon Seager 2023-07-10 15:44:43 +01:00
parent 9c0bd8b07a
commit ca396ce96b
No known key found for this signature in database
8 changed files with 28 additions and 24 deletions

View file

@ -1,6 +1,6 @@
/* eslint-disable no-console */
import { join } from "path";
import { existsSync, readFileSync, copyFileSync } from "fs";
import { copyFileSync, existsSync, mkdirSync, readFileSync } from "fs";
import cache from "memory-cache";
import yaml from "js-yaml";
@ -9,8 +9,14 @@ const cacheKey = "homepageEnvironmentVariables";
const homepageVarPrefix = "HOMEPAGE_VAR_";
const homepageFilePrefix = "HOMEPAGE_FILE_";
export const CONF_DIR = process.env.HOMEPAGE_CONFIG_DIR ? process.env.HOMEPAGE_CONFIG_DIR : join(process.cwd(), "config");
export default function checkAndCopyConfig(config) {
const configYaml = join(process.cwd(), "config", config);
if (!existsSync(CONF_DIR)) {
mkdirSync(CONF_DIR, { recursive: true });
}
const configYaml = join(CONF_DIR, config);
if (!existsSync(configYaml)) {
const configSkeleton = join(process.cwd(), "src", "skeleton", config);
try {
@ -62,7 +68,7 @@ export function substituteEnvironmentVars(str) {
export function getSettings() {
checkAndCopyConfig("settings.yaml");
const settingsYaml = join(process.cwd(), "config", "settings.yaml");
const settingsYaml = join(CONF_DIR, "settings.yaml");
const rawFileContents = readFileSync(settingsYaml, "utf8");
const fileContents = substituteEnvironmentVars(rawFileContents);
const initialSettings = yaml.load(fileContents) ?? {};
@ -79,6 +85,5 @@ export function getSettings() {
})
}
}
return initialSettings
}