-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handling.js
40 lines (31 loc) · 1.04 KB
/
file_handling.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
'use strict';
const path = require('path');
const fs = require('fs');
function getFile(fname) {
try {
return fs.readFileSync(path.resolve(__dirname,fname));
}
catch (err) {
throw(err);
}
}
function writeResults(nodes, cumulativeNodes, nodesFileName, cumulativeNodesFileName) {
// write the nodes to a daily file
let backupFileName = cumulativeNodesFileName+".bak";
console.log(backupFileName);
fs.writeFile(nodesFileName, JSON.stringify(nodes), (err) => {
if (err) throw err;
console.log(`${nodesFileName} successfully written.`)
});
// backup and then write the cumulativeNodes
fs.copyFile(cumulativeNodesFileName, backupFileName, (err) => {
if (err) throw err;
console.log(`${cumulativeNodesFileName} was backed up successfully.`);
// write the new cumulativeNodes
fs.writeFile(cumulativeNodesFileName, JSON.stringify(cumulativeNodes), (err) => {
if (err) throw err;
console.log(`${cumulativeNodesFileName} updated successfully.`);
});
});
}
module.exports = {getFile, writeResults};