mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
creating config automagically if missing
This commit is contained in:
0
conf/config.json
Executable file → Normal file
0
conf/config.json
Executable file → Normal file
25
lib/utils.js
25
lib/utils.js
@@ -3,6 +3,12 @@ import { fileURLToPath } from 'node:url';
|
|||||||
import { readFile } from 'fs/promises';
|
import { readFile } from 'fs/promises';
|
||||||
import { createHash } from 'crypto';
|
import { createHash } from 'crypto';
|
||||||
import { DEFAULT_CONFIG } from './defaultConfig.js';
|
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() {
|
function inDevMode() {
|
||||||
return process.env.NODE_ENV == null || process.env.NODE_ENV !== 'production';
|
return process.env.NODE_ENV == null || process.env.NODE_ENV !== 'production';
|
||||||
@@ -53,11 +59,14 @@ function buildHash(...inputs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let config = {};
|
let config = {};
|
||||||
|
|
||||||
export async function readConfigFromStorage() {
|
export async function readConfigFromStorage() {
|
||||||
return JSON.parse(await readFile(new URL('../conf/config.json', import.meta.url)));
|
return JSON.parse(await readFile(new URL('../conf/config.json', import.meta.url)));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function refreshConfig() {
|
export async function refreshConfig() {
|
||||||
|
checkIfConfigExistsAndWriteIfNot();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
config = await readConfigFromStorage();
|
config = await readConfigFromStorage();
|
||||||
//backwards compatability...
|
//backwards compatability...
|
||||||
@@ -65,14 +74,20 @@ export async function refreshConfig() {
|
|||||||
config.demoMode ??= false;
|
config.demoMode ??= false;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
config = { ...DEFAULT_CONFIG };
|
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;
|
* If the config file does not exist, we will create it.
|
||||||
const RE_EXT = /\.(jpe?g|png|gif)(\?.*)?$/i;
|
*/
|
||||||
const HTTPS_PREFIX = 'https://';
|
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) => {
|
const normalizeImageUrl = (url) => {
|
||||||
if (typeof url !== 'string' || url.length === 0) return null;
|
if (typeof url !== 'string' || url.length === 0) return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user