-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.ts
140 lines (118 loc) · 4.05 KB
/
storage.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {MenuShortcut, Settings} from "./settings";
import {Button} from "./button";
import * as electron from "electron";
import * as path from "path";
import * as fs from "fs";
export class DataStorage {
private settings: Settings;
private readonly path: string;
private readonly data: any;
constructor(opts: any) {
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
this.path = path.join(userDataPath, opts.configName + '.json');
this.data = DataStorage.parseDataFile(this.path, {});
this.settings = this.getSettings();
}
getSettings(): Settings {
// @ts-ignore
this.settings = this.get('settings') || {}
return this.settings;
}
changeUsername(username: string): Settings {
const settings = this.getSettings();
settings.username = username;
return this.saveSettings(settings);
}
changeGuild(guild: string): Settings {
const settings = this.getSettings();
settings.guild = guild;
return this.saveSettings(settings);
}
changeServer(server: string): Settings {
const settings = this.getSettings();
settings.server = server;
return this.saveSettings(settings);
}
saveSettings(settings: Settings): Settings {
this.set('settings', settings)
return this.getSettings();
}
addButton(button: Button): Settings {
const settings = this.getSettings();
if (!settings.buttons) {
settings.buttons = [];
}
settings.buttons.push(button);
return this.saveSettings(settings);
}
deleteButton(index: number): Settings {
const settings = this.getSettings();
if (!settings.buttons) {
settings.buttons = [];
return this.saveSettings(settings);
}
settings.buttons = settings.buttons.filter(b => b.index !== index);
return this.saveSettings(settings);
}
getButtonByIndex(index: number): Button | undefined {
return this.getSettings().buttons.find(b => b.index === index);
}
getButtons(): Array<Button> {
return this.getSettings().buttons;
}
getMenuShortcuts(): Array<MenuShortcut> {
let shortcuts = this.getSettings().menuShortcuts;
if (!shortcuts) {
this.getSettings().menuShortcuts = [];
return [];
}
return shortcuts;
}
getShortcutForAction(action: string): MenuShortcut | undefined {
return this.getMenuShortcuts().find(menuShortcut => menuShortcut.action === action);
}
addMenuShortcut(menuShortcut: MenuShortcut): Settings {
let settings = this.getSettings();
if (!settings.menuShortcuts) {
settings.menuShortcuts = [];
}
if (this.getShortcutForAction(menuShortcut.action)) {
settings = this.deleteMenuShortcut(menuShortcut.action);
}
settings.menuShortcuts.push(menuShortcut);
return this.saveSettings(settings);
}
deleteMenuShortcut(action: string): Settings {
const settings = this.getSettings();
if (!settings.menuShortcuts) {
settings.menuShortcuts = [];
return this.saveSettings(settings);
}
settings.menuShortcuts = settings.menuShortcuts.filter(shortcut => shortcut.action !== action);
return this.saveSettings(settings);
}
private get(key: string) {
return this.data[key];
}
private set(key: string, val: any) {
this.data[key] = val;
fs.writeFileSync(this.path, JSON.stringify(this.data));
}
private static parseDataFile(filePath: string, defaults: any) {
try {
// @ts-ignore
return JSON.parse(fs.readFileSync(filePath));
} catch (error) {
return defaults;
}
}
getUsername(): string {
return this.getSettings().username;
}
getGuild(): string {
return this.getSettings().guild;
}
getServer(): string {
return this.getSettings().server;
}
}