/
Config.ts
179 lines (145 loc) · 4.57 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import fs from 'fs-extra';
import path from 'path';
import {CookiesSetDetails} from 'electron';
export class Config {
get BROWSER_BUILTIN() { return 'builtin'; }
get BROWSER_EXTERNAL() { return 'external'; }
private _activeIndex = 0;
private _configPath: string;
private _configs: {[key: string]: any}[];
private _config: {[key: string]: any};
private _loginName: string;
initialize(path) {
this._configPath = path;
this._configs = fs.readJsonSync(path, {throws: false});
this._config = this._configs[this._activeIndex];
}
get activeIndex() {
return this._activeIndex;
}
switchConfig(configIndex) {
this._activeIndex = configIndex;
this._config = this._configs[configIndex];
this._loginName = null;
}
addConfigGitHub(configGitHub) {
const config = fs.readJsonSync(`${__dirname}/asset/config.json`)[0];
config.github = configGitHub;
config.database.path = `./main-${Date.now()}.db`;
this._configs.push(config);
fs.writeJsonSync(this._configPath, this._configs, {spaces: 2});
}
updateConfigGitHub(index, configGitHub) {
this._configs[index].github = configGitHub;
fs.writeJsonSync(this._configPath, this._configs, {spaces: 2});
}
updateConfig(index, config) {
this._configs[index] = config;
fs.writeJsonSync(this._configPath, this._configs, {spaces: 2});
}
deleteConfig(index) {
const config = this._configs[index];
const dbPath = path.resolve(path.dirname(this._configPath), config.database.path);
fs.removeSync(dbPath);
this._configs.splice(index, 1);
fs.writeJsonSync(this._configPath, this._configs, {spaces: 2});
}
get configs() {
return this._configs;
}
get activeConfig() {
return this._config;
}
set loginName(loginName) {
this._loginName = loginName;
}
get loginName() {
return this._loginName;
}
get accessToken() {
return this._config.github.accessToken;
}
get host() {
return this._config.github.host;
}
get webHost() {
return this._config.github.webHost;
}
get pathPrefix() {
return this._config.github.pathPrefix;
}
get apiInterval() {
// hack: response when config have not been initialized yet
if (this._config) {
return this._config.github.interval;
} else {
return 10;
}
}
get https() {
return this._config.github.https;
}
get databasePath() {
return path.resolve(path.dirname(this._configPath), this._config.database.path);
}
get databaseMax() {
return this._config.database.max;
}
get generalBrowser() {
return this._config.general.browser;
}
set generalBrowser(value) {
if ([this.BROWSER_BUILTIN, this.BROWSER_EXTERNAL].includes(value)) {
this._config.general.browser = value;
fs.writeJsonSync(this._configPath, this._configs, {spaces: 2});
} else {
throw new Error(`unknown browser: ${value}`);
}
}
get generalNotification() {
return this._config.general.notification;
}
get generalNotificationSilent() {
return this._config.general.notificationSilent;
}
get generalOnlyUnreadIssue() {
return this._config.general.onlyUnreadIssue;
}
get generalBadge() {
return this._config.general.badge;
}
get generalAlwaysOpenExternalUrlInExternalBrowser() {
return this._config.general.alwaysOpenExternalUrlInExternalBrowser;
}
get themeMainPath() {
if (!this._config.theme.main) return null;
return path.resolve(path.dirname(this._configPath), this._config.theme.main);
}
get themeBrowserPath() {
if (!this._config.theme.browser) return null;
return path.resolve(path.dirname(this._configPath), this._config.theme.browser);
}
get cookieDetails(): CookiesSetDetails[] {
if (!this._config.experimentalCookie) return [];
const rawCookies = this._config.experimentalCookie.split('\n');
const cookies: CookiesSetDetails[] = [];
for (const rawCookie of rawCookies) {
const directives = rawCookie.split(';').map(v => v.trim());
const [name, value] = directives[0].split('=');
const [, expires] = directives.find(d => d.split('=')[0]?.toLowerCase() === 'expires')?.split('=') || [null, null];
const [, domain] = directives.find(d => d.split('=')[0]?.toLowerCase() === 'domain')?.split('=') || [null, this.webHost];
cookies.push({
name,
value,
domain,
url: `http${this.https ? 's' : ''}://${domain}`,
path: '/',
httpOnly: true,
secure: true,
expirationDate: expires ? new Date(expires).getTime() / 1000 : undefined
});
}
return cookies;
}
}
export default new Config();