This repository has been archived by the owner on Nov 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConfigManager.js
63 lines (51 loc) · 1.6 KB
/
ConfigManager.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
const fs = require('fs')
const isNullOrUndefined = require('util').isNullOrUndefined;
const pathFile = __dirname + "/config.json";
class ConfigManager{
static async getToken(){
let config = await this.getConfig();
return config.token;
}
static async getLogServer(){
let config = await this.getConfig();
return config.logServer;
}
/**
* Set the token in file
* @param {string} token
* @returns {Promise} Resolve true if token setted, else false
*/
static async setToken(token){
let config = await ConfigManager.getConfig();
config.token = token;
return this.save(config);
}
static async setLogServer(ip, db, user, password){
let config = await ConfigManager.getConfig();
config.logServer.ip = ip;
config.logServer.db = db;
config.logServer.user = user;
config.logServer.password = password;
return this.save(config);
}
static async getConfig(){
return new Promise((resolve, reject) => {
fs.readFile(pathFile, "utf8", (err, data) => {
if(err)
resolve({});
else
resolve(JSON.parse(data));
});
});
}
static async save(config){
return new Promise((resolve, reject) => {
fs.writeFile(pathFile, JSON.stringify(config, null, 4), "utf8", (err) => {
if (err)
resolve(false);
resolve(true);
})
});
}
}
module.exports.ConfigManager = ConfigManager;