forked from robotunicr0n/twitch-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonfile.js
44 lines (37 loc) · 1.1 KB
/
jsonfile.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
var fs = require('fs');
module.exports.spaces = 4;
function readFile(file, callback) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) {
callback(err, null);
} else {
try {
var obj = JSON.parse(data);
callback(null, obj);
} catch (err2) {
callback(err2, null);
}
}
})
}
function readFileSync(file) {
var data = fs.readFileSync(file, 'utf8');
return JSON.parse(data);
}
function writeFile(file, obj, callback) {
var str = '';
try {
str = JSON.stringify(obj, null, module.exports.spaces);
} catch (err) {
callback(err, null);
}
fs.writeFile(file, str, callback);
}
function writeFileSync(file, obj) {
var str = JSON.stringify(obj, null, module.exports.spaces);
return fs.writeFileSync(file, str); //not sure if fs.writeFileSync returns anything, but just in case
}
module.exports.readFile = readFile;
module.exports.readFileSync = readFileSync;
module.exports.writeFile = writeFile;
module.exports.writeFileSync = writeFileSync;