2022-09-07 16:53:24 +03:00
|
|
|
/* eslint-disable no-console */
|
2022-09-06 08:39:25 +03:00
|
|
|
import { join } from "path";
|
2023-04-04 22:47:17 -07:00
|
|
|
import { existsSync, readFileSync, copyFileSync } from "fs";
|
2022-09-07 16:53:24 +03:00
|
|
|
|
2023-02-23 09:51:28 -08:00
|
|
|
import cache from "memory-cache";
|
2022-09-05 20:14:14 +03:00
|
|
|
import yaml from "js-yaml";
|
2022-08-24 10:44:35 +03:00
|
|
|
|
2023-02-23 09:51:28 -08:00
|
|
|
const cacheKey = "homepageEnvironmentVariables";
|
|
|
|
const homepageVarPrefix = "HOMEPAGE_VAR_";
|
|
|
|
const homepageFilePrefix = "HOMEPAGE_FILE_";
|
|
|
|
|
2022-08-24 10:44:35 +03:00
|
|
|
export default function checkAndCopyConfig(config) {
|
|
|
|
const configYaml = join(process.cwd(), "config", config);
|
|
|
|
if (!existsSync(configYaml)) {
|
|
|
|
const configSkeleton = join(process.cwd(), "src", "skeleton", config);
|
2023-04-04 22:47:17 -07:00
|
|
|
try {
|
|
|
|
copyFileSync(configSkeleton, configYaml)
|
|
|
|
console.info("%s was copied to the config folder", config);
|
|
|
|
} catch (err) {
|
2022-09-07 16:53:24 +03:00
|
|
|
console.error("error copying config", err);
|
2022-08-25 13:16:03 +03:00
|
|
|
throw err;
|
2023-04-04 22:47:17 -07:00
|
|
|
}
|
2022-09-18 16:41:01 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
yaml.load(readFileSync(configYaml, "utf8"));
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return { ...e, config };
|
2022-08-24 10:44:35 +03:00
|
|
|
}
|
|
|
|
}
|
2022-09-05 20:14:14 +03:00
|
|
|
|
2023-02-23 09:51:28 -08:00
|
|
|
function getCachedEnvironmentVars() {
|
|
|
|
let cachedVars = cache.get(cacheKey);
|
|
|
|
if (!cachedVars) {
|
|
|
|
// initialize cache
|
|
|
|
cachedVars = Object.entries(process.env).filter(([key, ]) => key.includes(homepageVarPrefix) || key.includes(homepageFilePrefix));
|
|
|
|
cache.put(cacheKey, cachedVars);
|
|
|
|
}
|
|
|
|
return cachedVars;
|
|
|
|
}
|
2023-02-23 08:50:25 -06:00
|
|
|
|
2023-02-23 09:51:28 -08:00
|
|
|
export function substituteEnvironmentVars(str) {
|
2023-02-23 08:50:25 -06:00
|
|
|
let result = str;
|
2023-02-23 09:51:28 -08:00
|
|
|
if (result.includes('{{')) { // crude check if we have vars to replace
|
|
|
|
const cachedVars = getCachedEnvironmentVars();
|
|
|
|
cachedVars.forEach(([key, value]) => {
|
|
|
|
if (key.startsWith(homepageVarPrefix)) {
|
|
|
|
result = result.replaceAll(`{{${key}}}`, value);
|
|
|
|
} else if (key.startsWith(homepageFilePrefix)) {
|
|
|
|
const filename = value;
|
|
|
|
const fileContents = readFileSync(filename, "utf8");
|
|
|
|
result = result.replaceAll(`{{${key}}}`, fileContents);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-02-23 08:50:25 -06:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-18 16:41:01 +03:00
|
|
|
export function getSettings() {
|
2022-09-05 20:14:14 +03:00
|
|
|
checkAndCopyConfig("settings.yaml");
|
|
|
|
|
2022-09-06 08:39:25 +03:00
|
|
|
const settingsYaml = join(process.cwd(), "config", "settings.yaml");
|
2023-02-23 08:50:25 -06:00
|
|
|
const rawFileContents = readFileSync(settingsYaml, "utf8");
|
|
|
|
const fileContents = substituteEnvironmentVars(rawFileContents);
|
2023-02-08 00:19:33 -08:00
|
|
|
return yaml.load(fileContents) ?? {};
|
2023-02-23 08:50:25 -06:00
|
|
|
}
|