2020-01-03 15:01:15 +01:00
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
2020-05-19 10:52:39 +02:00
|
|
|
import getAppDataPath from "appdata-path";
|
2019-10-13 22:59:28 +02:00
|
|
|
|
2020-01-03 15:01:15 +01:00
|
|
|
import Service from "./Service";
|
2020-01-10 15:02:21 +01:00
|
|
|
import Meta from "./Meta";
|
2019-10-13 22:59:28 +02:00
|
|
|
|
2020-05-19 10:52:39 +02:00
|
|
|
const configDir = Meta.isDevMode() ? getAppDataPath('tabs-app-dev') : getAppDataPath('tabs-app');
|
2019-10-13 22:59:28 +02:00
|
|
|
const configFile = path.resolve(configDir, 'config.json');
|
|
|
|
|
2020-01-03 15:01:15 +01:00
|
|
|
export default class Config {
|
2020-05-21 07:38:44 +02:00
|
|
|
public services: Service[] = [];
|
2020-06-15 15:08:52 +02:00
|
|
|
|
2020-05-21 07:38:44 +02:00
|
|
|
public updateCheckSkip?: string;
|
2020-06-15 15:08:52 +02:00
|
|
|
|
|
|
|
public startMinimized: boolean = false;
|
|
|
|
|
2020-06-15 15:45:19 +02:00
|
|
|
public bigNavBar: boolean = false;
|
|
|
|
|
2020-05-21 07:38:44 +02:00
|
|
|
public securityButton: boolean = true;
|
|
|
|
public homeButton: boolean = false;
|
|
|
|
public backButton: boolean = true;
|
|
|
|
public forwardButton: boolean = false;
|
|
|
|
public refreshButton: boolean = false;
|
2020-05-20 17:13:33 +02:00
|
|
|
|
2020-05-21 07:38:44 +02:00
|
|
|
private properties: string[] = [];
|
2020-05-20 17:13:33 +02:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
[p: string]: unknown;
|
|
|
|
|
|
|
|
public constructor() {
|
2019-10-13 22:59:28 +02:00
|
|
|
// Load data from config file
|
2020-09-29 20:43:41 +02:00
|
|
|
let data: Record<string, unknown> = {};
|
2019-10-13 22:59:28 +02:00
|
|
|
if (fs.existsSync(configDir) && fs.statSync(configDir).isDirectory()) {
|
|
|
|
if (fs.existsSync(configFile) && fs.statSync(configFile).isFile())
|
|
|
|
data = JSON.parse(fs.readFileSync(configFile, 'utf8'));
|
|
|
|
} else {
|
|
|
|
fs.mkdirSync(configDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse services
|
2020-09-29 20:43:41 +02:00
|
|
|
if (typeof data.services === 'object' && Array.isArray(data.services)) {
|
2019-10-13 22:59:28 +02:00
|
|
|
let i = 0;
|
|
|
|
for (const service of data.services) {
|
|
|
|
this.services[i] = new Service(service);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.services.length === 0) {
|
2020-09-29 20:43:41 +02:00
|
|
|
this.services.push(new Service(
|
|
|
|
'welcome',
|
|
|
|
'Welcome',
|
|
|
|
'rocket',
|
|
|
|
false,
|
2020-11-27 14:31:27 +01:00
|
|
|
'https://eternae.ink/arisu/tabs',
|
2020-09-29 20:43:41 +02:00
|
|
|
false,
|
|
|
|
));
|
2019-10-13 22:59:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:13:33 +02:00
|
|
|
this.defineProperty('updateCheckSkip', data);
|
2020-09-29 20:43:41 +02:00
|
|
|
|
2020-06-15 15:08:52 +02:00
|
|
|
this.defineProperty('startMinimized', data);
|
2020-05-20 17:13:33 +02:00
|
|
|
|
2020-06-15 15:45:19 +02:00
|
|
|
this.defineProperty('bigNavBar', data);
|
|
|
|
|
2020-05-20 17:13:33 +02:00
|
|
|
this.defineProperty('securityButton', data);
|
|
|
|
this.defineProperty('homeButton', data);
|
|
|
|
this.defineProperty('backButton', data);
|
|
|
|
this.defineProperty('forwardButton', data);
|
|
|
|
this.defineProperty('refreshButton', data);
|
2020-05-19 11:46:34 +02:00
|
|
|
|
2019-10-13 22:59:28 +02:00
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
public save(): void {
|
2020-01-04 15:56:37 +01:00
|
|
|
console.log('Saving config');
|
2019-10-13 22:59:28 +02:00
|
|
|
fs.writeFileSync(configFile, JSON.stringify(this, null, 4));
|
2020-01-10 15:02:21 +01:00
|
|
|
console.log('> Config saved to', configFile.toString());
|
2019-10-13 22:59:28 +02:00
|
|
|
}
|
2020-05-20 17:13:33 +02:00
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
public defineProperty(name: string, data: Record<string, unknown>): void {
|
2020-05-20 17:13:33 +02:00
|
|
|
if (data[name] !== undefined) {
|
2020-09-29 20:43:41 +02:00
|
|
|
this[name] = data[name];
|
2020-05-20 17:13:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.properties.push(name);
|
|
|
|
}
|
|
|
|
|
2020-09-29 20:43:41 +02:00
|
|
|
public update(data: Record<string, unknown>): void {
|
2020-05-20 17:13:33 +02:00
|
|
|
for (const prop of this.properties) {
|
|
|
|
if (data[prop] !== undefined) {
|
2020-09-29 20:43:41 +02:00
|
|
|
this[prop] = data[prop];
|
2020-05-20 17:13:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 20:43:41 +02:00
|
|
|
}
|