2020-01-03 15:01:15 +01:00
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import {homedir} from "os";
|
2019-10-13 22:59:28 +02:00
|
|
|
|
2020-01-03 15:01:15 +01:00
|
|
|
import Service from "./Service";
|
2019-10-13 22:59:28 +02:00
|
|
|
|
2020-01-03 15:01:15 +01:00
|
|
|
const configDir = path.resolve(homedir(), '.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 {
|
2019-10-13 22:59:28 +02:00
|
|
|
constructor() {
|
|
|
|
// Load data from config file
|
|
|
|
let data = {};
|
|
|
|
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
|
|
|
|
this.services = [];
|
|
|
|
if (typeof data.services === 'object') {
|
|
|
|
let i = 0;
|
|
|
|
for (const service of data.services) {
|
|
|
|
this.services[i] = new Service(service);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.services.length === 0) {
|
2020-01-03 15:01:15 +01:00
|
|
|
this.services.push(new Service('welcome', 'Welcome', 'fas fa-rocket', false, 'https://gitlab.com/ArisuOngaku/tabs', false));
|
2019-10-13 22:59:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
fs.writeFileSync(configFile, JSON.stringify(this, null, 4));
|
|
|
|
}
|
2020-01-03 15:01:15 +01:00
|
|
|
}
|