-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileIo.js
60 lines (53 loc) · 1.4 KB
/
fileIo.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
const fs = require('fs');
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
function readInitialState() {
try {
return JSON.parse(fs.readFileSync(`${getUserHome()}/.web-launcher/store.json`, 'utf8'))
} catch (err) {
switch (err.code) {
case 'ENOENT':
return []
case 'EACCES':
throw 'Error: Cannot read store file.'
default:
throw `Error: ${err}`
}
}
}
function writeState (state) {
if (!fs.existsSync(`${getUserHome()}/.web-launcher`)){
fs.mkdirSync(`${getUserHome()}/.web-launcher`);
}
return new Promise( (resolve, reject) => {
fs.writeFile(`${getUserHome()}/.web-launcher/store.json`, JSON.stringify(state), (err) => {
if (!err) {
resolve(true)
} else {
reject(err)
}
})
})
}
function deleteState () {
if (fs.existsSync(`${getUserHome()}/.web-launcher`)) {
return new Promise( (resolve, reject) => {
fs.unlink(`${getUserHome()}/.web-launcher/store.json`, (err) => {
if (!err) {
fs.rmdir(`${getUserHome()}/.web-launcher`, (err) => {
if (!err) {
resolve(true)
} else {
reject(err)
}
})
} else {
reject(err)
}
})
})
}
return Promise.resolve(true)
}
module.exports = { readInitialState, writeState, deleteState };