fix: Made class abstract

This commit is contained in:
2025-06-09 12:59:13 +02:00
parent aac85e3679
commit 647bc1c2ba
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";
export class FileHandler<T> {
export abstract class FileHandler<T> {
protected _value: T
public get value(): T {
return this._value;
@@ -13,8 +12,7 @@ export class FileHandler<T> {
constructor(public path: PathOrFileDescriptor, public settings?: {
defaultContent?: T,
name?: string,
project?: (keyof T)[] | { [key in keyof T]: any}
name?: string
}) {
try {
this._value = JSON.parse(readFileSync(path, 'utf-8'))
@@ -32,11 +30,17 @@ export class FileHandler<T> {
}
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() {
this._value = JSON.parse(readFileSync(this.path, { encoding: "utf-8" }))
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";
export interface IUSettings {
@@ -22,6 +20,26 @@ export interface 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() {
const defaultSettings: IUSettings = {
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"})
}
}