53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import path from "path";
|
|
import fs from "fs";
|
|
import UserI from "../types/UserI";
|
|
import { ServerInfoI } from "../types/ServerInfoI";
|
|
import MessageI from "../types/MessageI";
|
|
|
|
interface ConfigManagerI {
|
|
userManager?: {
|
|
users: UserI[];
|
|
authCodes: Date[];
|
|
};
|
|
messages?: MessageI[];
|
|
serverInfo?: ServerInfoI;
|
|
}
|
|
|
|
export default class ConfigManager {
|
|
configPath = path.join(process.env.STORAGE || "/app/storage", "config.json")
|
|
public userManager: {
|
|
users: UserI[];
|
|
authCodes: Date[];
|
|
} = {
|
|
users: [],
|
|
authCodes: [],
|
|
}
|
|
public messages: MessageI[] = []
|
|
public serverInfo?: ServerInfoI = undefined
|
|
|
|
constructor() {
|
|
this.load()
|
|
setInterval(() => this.save(), 10000)
|
|
}
|
|
save() {
|
|
const r: ConfigManagerI = { userManager: this.userManager, messages: this.messages, serverInfo: this.serverInfo }
|
|
fs.writeFileSync(this.configPath, JSON.stringify(r))
|
|
}
|
|
load() {
|
|
const r = { userManager: this.userManager }
|
|
if (!fs.existsSync(this.configPath)) {
|
|
fs.writeFileSync(this.configPath, JSON.stringify(r))
|
|
}
|
|
try {
|
|
const t: ConfigManagerI = JSON.parse(String(fs.readFileSync(this.configPath)))
|
|
if (!!t?.userManager)
|
|
this.userManager = { ...this.userManager, ...t.userManager }
|
|
if (!!t?.messages)
|
|
this.messages = t.messages
|
|
if (!!t?.serverInfo)
|
|
this.serverInfo = t.serverInfo
|
|
} catch (error) {
|
|
console.log(error)
|
|
}
|
|
}
|
|
} |