blob: 622d53ebc280c7940acc58171d724032131c41b7 (
plain) (
blame)
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
|
import { observable, toJS } from 'mobx';
import { pathExistsSync, outputJsonSync, readJsonSync } from 'fs-extra';
import { SETTINGS_PATH, DEFAULT_APP_SETTINGS } from '../config';
const debug = require('debug')('Settings');
export default class Settings {
@observable store = DEFAULT_APP_SETTINGS;
constructor() {
if (!pathExistsSync(SETTINGS_PATH)) {
this._writeFile();
} else {
this._hydrate();
}
}
set(settings) {
this.store = this._merge(this.store, settings);
this._writeFile();
}
get all() {
return this.store;
}
get(key) {
return this.store[key];
}
_merge(settings) {
return Object.assign(DEFAULT_APP_SETTINGS, settings);
}
_hydrate() {
this.store = this._merge(readJsonSync(SETTINGS_PATH));
debug('Hydrate store', toJS(this.store));
}
_writeFile() {
outputJsonSync(SETTINGS_PATH, this.store);
debug('Write settings file', toJS(this.store));
}
}
|