1
0
forked from noxious/server

Added logging, worked on character type management

This commit is contained in:
2024-10-19 02:14:39 +02:00
parent d29420cbf3
commit 4e1e7d95ac
11 changed files with 234 additions and 32 deletions

View File

@ -1,49 +1,49 @@
import * as fs from 'fs/promises';
import { appLogger } from './logger';
import * as fs from 'fs/promises'
import { appLogger } from './logger'
export async function readJsonFile<T>(filePath: string): Promise<T> {
try {
const fileContent = await fs.readFile(filePath, 'utf-8');
return JSON.parse(fileContent) as T;
const fileContent = await fs.readFile(filePath, 'utf-8')
return JSON.parse(fileContent) as T
} catch (error) {
appLogger.error(`Error reading JSON file: ${error instanceof Error ? error.message : String(error)}`);
throw error;
appLogger.error(`Error reading JSON file: ${error instanceof Error ? error.message : String(error)}`)
throw error
}
}
export async function writeJsonFile<T>(filePath: string, data: T): Promise<void> {
try {
const jsonString = JSON.stringify(data, null, 2);
await fs.writeFile(filePath, jsonString, 'utf-8');
const jsonString = JSON.stringify(data, null, 2)
await fs.writeFile(filePath, jsonString, 'utf-8')
} catch (error) {
appLogger.error(`Error writing JSON file: ${error instanceof Error ? error.message : String(error)}`);
throw error;
appLogger.error(`Error writing JSON file: ${error instanceof Error ? error.message : String(error)}`)
throw error
}
}
export async function readJsonValue<T>(filePath: string, paramPath: string): Promise<T> {
try {
const jsonContent = await readJsonFile<any>(filePath);
const paramValue = paramPath.split('.').reduce((obj, key) => obj && obj[key], jsonContent);
const jsonContent = await readJsonFile<any>(filePath)
const paramValue = paramPath.split('.').reduce((obj, key) => obj && obj[key], jsonContent)
if (paramValue === undefined) {
throw new Error(`Parameter ${paramPath} not found in the JSON file`);
throw new Error(`Parameter ${paramPath} not found in the JSON file`)
}
return paramValue as T;
return paramValue as T
} catch (error) {
appLogger.error(`Error reading JSON parameter: ${error instanceof Error ? error.message : String(error)}`);
throw error;
appLogger.error(`Error reading JSON parameter: ${error instanceof Error ? error.message : String(error)}`)
throw error
}
}
export async function setJsonValue<T>(filePath: string, key: string, value: any): Promise<void> {
try {
const data = await readJsonFile<T>(filePath);
const updatedData = { ...data, [key]: value };
await writeJsonFile(filePath, updatedData);
const data = await readJsonFile<T>(filePath)
const updatedData = { ...data, [key]: value }
await writeJsonFile(filePath, updatedData)
} catch (error) {
appLogger.error(`Error setting JSON value: ${error instanceof Error ? error.message : String(error)}`);
throw error;
appLogger.error(`Error setting JSON value: ${error instanceof Error ? error.message : String(error)}`)
throw error
}
}
}

View File

@ -17,17 +17,17 @@ export function getPublicPath(folder: string, ...additionalSegments: string[]) {
export function doesPathExist(path: string) {
try {
fs.accessSync(path, fs.constants.F_OK);
return true;
fs.accessSync(path, fs.constants.F_OK)
return true
} catch (e) {
return false;
return false
}
}
export function createDir(path: string) {
try {
fs.mkdirSync(path, { recursive: true });
fs.mkdirSync(path, { recursive: true })
} catch (e) {
console.error(e);
console.error(e)
}
}
}