-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.js
65 lines (56 loc) · 1.51 KB
/
store.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
const electron = require('electron');
const path = require('path');
const fs = require('fs');
const store = module.exports
store.store = (opts) => {
const userDataPath = (electron.app || electron.remote.app).getPath('userData');
const filePath = path.join(userDataPath, opts.configName + '.json');
const data = parseDataFile(filePath, opts.defaults);
const get = (key) => {
return data[key];
};
const set = (key, val) => {
data[key] = val;
console.log(path, data);
fs.writeFileSync(path, JSON.stringify(data));
};
return {
get,
set,
};
};
store.parseDataFile = (filePath) => {
checkFile(filePath)
filePath = filePath + '/mirrors.json'
try {
return JSON.parse(fs.readFileSync(filePath));
} catch (error) {
return error;
}
};
store.saveDataFile = (filePath, data, version, archi) => {
checkFile(filePath)
filePath = filePath + '/mirrors.json';
const json = {
mirrors: data,
info: { downloadedAt: new Date().getTime(), version, archi }
}
try{
fs.writeFileSync(filePath, JSON.stringify(json));
}
catch (e){
return e;
}
}
function checkFile(filePath){
filePath = filePath + '/mirrors.json';
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
// Create the file
fs.writeFile(filePath, JSON.stringify({}), (err) => {
if (err) throw err;
});
} else {
}
});
}