-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
37 lines (32 loc) · 1.01 KB
/
utils.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
const fs = require("fs");
const { logMsg } = require("./logger");
const subjectLinks = require("./subjectLinks");
const dailyCheckLogFile = "./public/dailyLogDetails.txt";
const dateOptions = { hour: "numeric", minute: "numeric", hour12: true };
const writeObjToFile = (obj) => {
fs.writeFile(dailyCheckLogFile, JSON.stringify(obj), (err) => {
console.log("Object stored to dailyCheckLog");
});
};
const initialObj = {
timesChecked: 0,
timesMarked: 0,
subjectsMarked: [],
manuallyMarked: [],
nextCheckAt: new Date().toLocaleTimeString("en-US", dateOptions),
subjectsLeft: Object.keys(subjectLinks),
};
const readObjFromFile = () => {
const executor = (resolve, reject) => {
fs.readFile(dailyCheckLogFile, (err, data) => {
if (err) {
logMsg("error in reading obj");
resolve(initialObj);
} else {
resolve(JSON.parse(data) || initialObj);
}
});
};
return new Promise(executor);
};
module.exports = { dateOptions, writeObjToFile, readObjFromFile, initialObj };