diff --git a/conf/config.json b/conf/config.json old mode 100755 new mode 100644 diff --git a/lib/utils.js b/lib/utils.js index 54d7715..3f180c7 100755 --- a/lib/utils.js +++ b/lib/utils.js @@ -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;