-
Notifications
You must be signed in to change notification settings - Fork 6
/
settings.js
101 lines (91 loc) · 2.03 KB
/
settings.js
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
const {logger, appName, isDevelopment, settingsDB} = require('./config');
const AutoLaunch = require('auto-launch');
const autoLaunch = new AutoLaunch({
name: appName,
mac: {
useLaunchAgent: true,
},
});
let initSync = false;
const settings = {
clipboardLimit: 30,
autoStart: true,
};
/**
* @return {Promise}
*/
function sync() {
return new Promise((resolve, reject)=>{
settingsDB.find({}).exec(async (error, results) =>{
try {
if (error) {
logger.error('Error in fetching settings from DB:', error);
return reject(error);
}
for (const result of results) {
settings[result.key] = result.value;
}
initSync = true;
return resolve(settings);
} catch (error) {
logger.debug('Error in reading or apply settings: ', error);
return reject(error);
}
});
});
}
/**
* Save settings to database
* @param {String} key
* @param {Any} value
* @return {Promise}
*/
function saveToDB(key, value) {
return new Promise((resolve, reject)=>{
settingsDB.update(
{key: key},
{key, key, value: value},
{upsert: true},
(err)=> {
if (err) {
console.error(err);
logger.error('Error in in update settings: ', err);
return reject(err);
}
return resolve({
[key]: value,
});
});
});
}
/**
* Get settings from store
* @param {string} key setting key
* @return {Any} setting value
*/
async function get(key) {
if (!initSync) {
await sync();
if (!isDevelopment && settings.autoStart) {
const enabled = await autoLaunch.isEnabled();
logger.debug('autostart status: ', enabled);
if (!enabled) await autoLaunch.enable();
} else {
await autoLaunch.disable();
}
}
return settings[key];
}
/**
*
* @param {String} key
* @param {Any} value
*/
async function set(key, value) {
settings[key] = value;
await saveToDB(key, value);
}
module.exports = {
get: get,
set: set,
};