creating config automagically if missing

This commit is contained in:
orangecoding
2025-09-09 18:41:14 +02:00
parent 637a54e01e
commit 7372e5313f
2 changed files with 20 additions and 5 deletions

View File

@@ -3,6 +3,12 @@ import { fileURLToPath } from 'node:url';
import { readFile } from 'fs/promises';
import { createHash } from 'crypto';
import { DEFAULT_CONFIG } from './defaultConfig.js';
import fs from 'fs';
const RE_GT = />/g;
const RE_WEBP = /\/format\/webp/gi;
const RE_EXT = /\.(jpe?g|png|gif)(\?.*)?$/i;
const HTTPS_PREFIX = 'https://';
function inDevMode() {
return process.env.NODE_ENV == null || process.env.NODE_ENV !== 'production';
@@ -53,11 +59,14 @@ function buildHash(...inputs) {
}
let config = {};
export async function readConfigFromStorage() {
return JSON.parse(await readFile(new URL('../conf/config.json', import.meta.url)));
}
export async function refreshConfig() {
checkIfConfigExistsAndWriteIfNot();
try {
config = await readConfigFromStorage();
//backwards compatability...
@@ -65,14 +74,20 @@ export async function refreshConfig() {
config.demoMode ??= false;
} catch (error) {
config = { ...DEFAULT_CONFIG };
console.error('Error reading config file', error);
/* eslint-disable no-console */
console.info('Error reading config file.', error);
}
}
const RE_GT = />/g;
const RE_WEBP = /\/format\/webp/gi;
const RE_EXT = /\.(jpe?g|png|gif)(\?.*)?$/i;
const HTTPS_PREFIX = 'https://';
/**
* If the config file does not exist, we will create it.
*/
const checkIfConfigExistsAndWriteIfNot = () => {
if (!fs.existsSync(`${getDirName()}/../conf/config.json`)) {
console.info('Could not find config file. Will create one with default values now');
fs.writeFileSync(`${getDirName()}/../conf/config.json`, JSON.stringify({ ...DEFAULT_CONFIG }));
}
};
const normalizeImageUrl = (url) => {
if (typeof url !== 'string' || url.length === 0) return null;