1 Commits

Author SHA1 Message Date
647bc1c2ba fix: Made class abstract 2025-06-09 12:59:13 +02:00
2 changed files with 30 additions and 8 deletions

View File

@@ -1,7 +1,6 @@
import { project } from "@/utility";
import { PathOrFileDescriptor, readFileSync, writeFileSync } from "node:fs"; import { PathOrFileDescriptor, readFileSync, writeFileSync } from "node:fs";
export class FileHandler<T> { export abstract class FileHandler<T> {
protected _value: T protected _value: T
public get value(): T { public get value(): T {
return this._value; return this._value;
@@ -13,8 +12,7 @@ export class FileHandler<T> {
constructor(public path: PathOrFileDescriptor, public settings?: { constructor(public path: PathOrFileDescriptor, public settings?: {
defaultContent?: T, defaultContent?: T,
name?: string, name?: string
project?: (keyof T)[] | { [key in keyof T]: any}
}) { }) {
try { try {
this._value = JSON.parse(readFileSync(path, 'utf-8')) this._value = JSON.parse(readFileSync(path, 'utf-8'))
@@ -32,11 +30,17 @@ export class FileHandler<T> {
} }
private save() { private save() {
writeFileSync(this.path, JSON.stringify(project(this._value, this.settings.project), undefined, 2)) writeFileSync(this.path, JSON.stringify(this.construct(this._value), undefined, 2))
} }
public reload() { public reload() {
this._value = JSON.parse(readFileSync(this.path, { encoding: "utf-8" })) this._value = JSON.parse(readFileSync(this.path, { encoding: "utf-8" }))
console.log(`Reloaded ${this.settings.name}`); console.log(`Reloaded ${this.settings.name}`);
} }
/**
* Method that makes sure that object is the interface.
* @param value Input object
*/
abstract construct(value: T | any): T
} }

View File

@@ -1,5 +1,3 @@
import { project } from "@/utility";
import { readFileSync, writeFileSync } from "node:fs";
import { FileHandler } from "./filehandler"; import { FileHandler } from "./filehandler";
export interface IUSettings { export interface IUSettings {
@@ -22,6 +20,26 @@ export interface IUSettings {
} }
class UOptions extends FileHandler<IUSettings> { class UOptions extends FileHandler<IUSettings> {
construct(value: IUSettings | any): IUSettings {
return {
keyrooms: value.keyrooms ?? [],
rooms: value.rooms ?? [],
cleanThings: value.cleanThings ?? [],
menu: {
defaultItems: {
sn: value.menu.defaultItems.sn ?? [],
kol: value.menu.defaultItems.kol ?? []
}
},
security: {
loginTimeout: {
attempts: value.security.loginTimeout.attempts ?? 0,
time: value.security.loginTimeout.time ?? 0,
lockout: value.security.loginTimeout.lockout ?? 0
}
}
}
}
constructor() { constructor() {
const defaultSettings: IUSettings = { const defaultSettings: IUSettings = {
keyrooms: [], keyrooms: [],
@@ -41,7 +59,7 @@ class UOptions extends FileHandler<IUSettings> {
} }
} }
} }
super("./config/usettings.json", {defaultContent: defaultSettings, name: "user settings", project: ['cleanThings', 'keyrooms', 'menu', 'rooms', 'security']}) super("./config/usettings.json", {defaultContent: defaultSettings, name: "user settings"})
} }
} }