-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
68 lines (57 loc) · 1.59 KB
/
util.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
const fs = require('fs');
/* UTIL FUCTIONS */
clear = function(){
process.stdout.write('\u001b[2J\u001b[0;0H');
}
read = function(path){
try{
let rawdata = fs.readFileSync(path);
return JSON.parse(rawdata);
} catch(err){
console.error("Parse error: No settings.json or config.json found, if there is a config.json file, the path to settings.json might be incorrect, else run setup.bat again");
throw err;
}
}
write = function(data,filename){
let sJson = JSON.stringify(data,null,4);
fs.writeFileSync(filename, sJson,{encoding:'utf8',flag:'w'});
}
revertToBackup = function(path){
console.log("Reading backup into memory");
return read(path+".backup");
}
overwriteBackup = function(path){
fs.copyFile(path, path+".backup", (err) => {
if (err) throw err;
});
}
overwriteFile = function(js,path){
js.profiles.list = getAllObjects(js.profiles.list);
write(js,path);
}
setProfileSetting = function(js,path,term,settingName,value){
if(settingName === undefined || value === undefined) {
console.error("No setting or value specified");
return;
}
if(term === undefined){
js.profiles.list.forEach(p => {
p[settingName] = value;
})
} else {
js.profiles.list.find(p => p.name === term.name)[settingName] = value;
}
overwriteFile(js,path);
}
getAllObjects = function(list){
return list.filter(p => typeof p === 'object')
}
module.exports = {
getAllObjects,
setProfileSetting,
overwriteFile,
revertToBackup,
write,
read,
clear
}