-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.ts
112 lines (105 loc) · 5.54 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SeekHub
// Copyright (C) 2022 Oscar
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import { red, cyan } from "https://deno.land/[email protected]/fmt/colors.ts";
import { dirname, fromFileUrl } from "https://deno.land/[email protected]/path/mod.ts";
import { renderer, special, debug as debugHandler } from "./utils.ts"
const netDefaults: [string, number] = ["127.0.0.1", 2000];
export const apiEndpoint = "/api"
const file2SaveConfig = "config.json";
export const configKeys = ["hostname", "port", "id", "masterKey", "tempKey", "sessionTime", "publicAPI", "setup", "title", "name", "navTitle", "categories", "items", "extraInfo", "legalNotice"];
export type categoryStructure = { tag: string, name: string };
export type itemStructure = { id: number, type: string, image: string, name: string, description: string, price: string, allergens: string };
export type configStructure = [string, boolean | string | number | string[] | categoryStructure[] | itemStructure[] | { text: string }[] | null];
export class config {
static data: configStructure[] = [
["hostname", null],
["port", null],
["id", null],
["masterKey", null],
["tempKey", null],
["sessionTime", null],
["publicAPI", false],
["setup", false],
["title", null],
["name", null],
["navTitle", null],
["categories", null],
["items", null],
["extraInfo", null],
["legalNotice", null]
];
static async fetchConfig() {
try {
const retrievedConfigFile: configStructure[] = JSON.parse(await Deno.readTextFile(fromFileUrl(`${dirname(Deno.mainModule)}/${file2SaveConfig}`)));
config.data = retrievedConfigFile;
{
const configKeysNotInData = configKeys;
for (const dataIndex in config.data) {
try {
for (const configKeysNotInDataKey in configKeysNotInData) {
if (configKeysNotInData[configKeysNotInDataKey] == config.data[dataIndex][0]) delete configKeysNotInData[configKeysNotInDataKey];
}
// deno-lint-ignore no-empty
} catch { }
}
for (const configKeysNotInDataKey in configKeysNotInData) config.data.push([configKeysNotInData[configKeysNotInDataKey], null]);
await config.updateConfig(undefined, false)
}
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
debugHandler.tell(cyan("Regenerating the config file..."));
await config.updateConfig(undefined, false);
}
}
}
static async updateConfig(data2Update: configStructure | undefined, fetch = true) {
if (data2Update != undefined)
for (const dataIndex in config.data) {
if (config.data[dataIndex][0] === data2Update[0]) {
try {
config.data[dataIndex][1] = data2Update[1];
await Deno.writeTextFile(fromFileUrl(`${dirname(Deno.mainModule)}/${file2SaveConfig}`), JSON.stringify(config.data));
} catch (e) {
debugHandler.tell(red(`Error while trying to update config file (${e})`));
}
break
}
} else {
try {
await Deno.writeTextFile(fromFileUrl(`${dirname(Deno.mainModule)}/${file2SaveConfig}`), JSON.stringify(config.data));
} catch (e) {
debugHandler.tell(red(`Error while trying to update config file (${e})`));
}
}
if (fetch) await config.fetchConfig();
renderer.main.clearMasterPool();
}
static getData(key: string, force = false) {
for (const dataIndex in config.data) {
if (config.data[dataIndex][0] === key) {
if (config.data[dataIndex][0] === "navTitle") return special.formatNavbar((config.data[dataIndex][1] as string));
if (config.data[dataIndex][0] === "hostname" && config.data[dataIndex][1] === null) return netDefaults[0];
if (config.data[dataIndex][0] === "port" && config.data[dataIndex][1] === null) return netDefaults[1];
if (((config.data[dataIndex][0] === "setup") || (config.data[dataIndex][0] === "publicAPI")) && config.data[dataIndex][1] === null) return false;
if (((config.data[dataIndex][0] === "categories") || (config.data[dataIndex][0] === "items") || (config.data[dataIndex][0] === "extraInfo")) && config.data[dataIndex][1] === null) return [];
if (config.data[dataIndex][1] === null) { if (!force) throw new Error(`The value of the data requested is not defined! (${config.data[dataIndex][0]})`); else return config.data[dataIndex][1]; }
return config.data[dataIndex][1];
}
}
throw new Error(`The data requested doesnt't exist! (${key})`);
}
}