homepage/src/utils/config/config.js
James Wynn e0f1aae4d5 Added support for environment variable substitution
* Only environment variables starting with HOMEPAGE_VAR_ and HOMEPAGE_FILE_
  are supported
* The value of env var HOMEPAGE_VAR_XXX will replace {{HOMEPAGE_VAR_XXX}}
  in any config
* The value of env var HOMEPAGE_FILE_XXX must be a file path, the contents
  of which will be used to replace {{HOMEPAGE_FILE_XXX}} in any config
* If a substituted value contains a variable reference it may also be
  replaced, but the behavior is non-deterministic
2023-02-23 08:50:25 -06:00

55 lines
1.6 KiB
JavaScript

/* eslint-disable no-console */
import { join } from "path";
import { existsSync, copyFile, readFileSync } from "fs";
import yaml from "js-yaml";
export default function checkAndCopyConfig(config) {
const configYaml = join(process.cwd(), "config", config);
if (!existsSync(configYaml)) {
const configSkeleton = join(process.cwd(), "src", "skeleton", config);
copyFile(configSkeleton, configYaml, (err) => {
if (err) {
console.error("error copying config", err);
throw err;
}
console.info("%s was copied to the config folder", config);
});
return true;
}
try {
yaml.load(readFileSync(configYaml, "utf8"));
return true;
} catch (e) {
return { ...e, config };
}
}
export function substituteEnvironmentVars(str) {
const homepageVarPrefix = "HOMEPAGE_VAR_";
const homepageFilePrefix = "HOMEPAGE_FILE_";
let result = str;
Object.keys(process.env).forEach(key => {
if (key.startsWith(homepageVarPrefix)) {
result = result.replaceAll(`{{${key}}}`, process.env[key]);
} else if (key.startsWith(homepageFilePrefix)) {
const filename = process.env[key];
const fileContents = readFileSync(filename, "utf8");
result = result.replaceAll(`{{${key}}}`, fileContents);
}
});
return result;
}
export function getSettings() {
checkAndCopyConfig("settings.yaml");
const settingsYaml = join(process.cwd(), "config", "settings.yaml");
const rawFileContents = readFileSync(settingsYaml, "utf8");
const fileContents = substituteEnvironmentVars(rawFileContents);
return yaml.load(fileContents) ?? {};
}