forked from RainySY/chaoxing-xuexitong-autoflush
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.js
56 lines (51 loc) · 1.03 KB
/
config.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
const fs = require("fs");
class Config {
constructor(path) {
this.path = path;
this.uname = null;
this.password = null;
this.speed = null;
this.pick = null;
this.test = null;
this.pickinfos = null;
}
async write() {
const c = {
"uname": this.uname,
"password": this.password,
"speed": this.speed,
"test": this.test,
"pick": this.pick,
"pickinfos": this.pickinfos
}
const config = JSON.stringify(c, null,"\t")
return await fs.promises.writeFile(this.path,config,{
encoding: "utf8"
})
}
async read(path) {
const config = {
speed: 2,
pick: false,
test: true
}
return await fs.promises.readFile(this.path,{
encoding: "utf8"
}).then(c=>{
try {
Object.assign(config, JSON.parse(c));
for(const key in config)
this[key] = config[key]
return this;
} catch(e) {
console.error(e);
process.exit(0);
}
}).catch(e=>{
// console.error(e);
console.log("读取失败或者目录下不存在配置文件")
return this;
})
}
}
module.exports = Config;