-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileman.js
37 lines (33 loc) · 997 Bytes
/
fileman.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
"use strict"
const fs = require('fs');
function ReadJSONFileasObject(filename, objParams) {
let objret;
let callback = objParams.callback;
let objdefault = objParams.default;
try {
objret = JSON.parse(fs.readFileSync(filename, 'utf8'));
if (typeof callback === 'function') callback(objret, objParams);
} catch(e) {
console.log(e);
objret = objdefault;
}
return objret;
}
function WriteObjectasJSONFile(filename, objJSON) {
return new Promise (
(resolve, reject) => {
fs.writeFile(filename, JSON.stringify(objJSON), 'utf8', function (err) {
if (err) {
throw new Error(err);// console.log(err);
} else {
resolve(filename + ' saved successfully');
}
// console.log('tx.json saved successfully');
});
}
);
}
module.exports = {
ReadJSONFileasObject
, WriteObjectasJSONFile
}