-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
55 lines (51 loc) · 1.51 KB
/
helper.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
const fs = require("fs");
const path = require("path");
const xlsx = require("xlsx");
function dirCreater(inputPath) {
let isPresent = fs.existsSync(inputPath);
if (isPresent == false) {
fs.mkdirSync(inputPath);
} else {
//console.log(inputPath, "already present")
}
}
function fileHandler(inputPath, dataObj) {
let isFilePresent = fs.existsSync(inputPath);
let arr = [];
if (isFilePresent == false) {
// fileCreater(inputPath, dataObj)
arr.push(dataObj);
excelWriter(inputPath, arr);
} else {
arr = excelReader(inputPath);
arr.push(dataObj);
excelWriter(inputPath, arr);
// fileUpdater(inputPath, dataObj);
}
}
function fileCreater(playerPath, dataObj) {
let arr = [dataObj];
fs.writeFileSync(playerPath, JSON.stringify(arr));
}
function fileUpdater(playerPath, dataObj) {
let dataBuffer = fs.readFileSync(playerPath);
let arr = JSON.parse(dataBuffer);
arr.push(dataObj);
fs.writeFileSync(playerPath, JSON.stringify(arr));
}
function excelReader(filePath) {
let wb = xlsx.readFile(filePath);
let excelData = wb.Sheets["Sheet1"];
let ans = xlsx.utils.sheet_to_json(excelData);
return ans;
}
function excelWriter(filePath, json) {
let newWB = xlsx.utils.book_new();
let newWS = xlsx.utils.json_to_sheet(json);
xlsx.utils.book_append_sheet(newWB, newWS, "Sheet1");
xlsx.writeFile(newWB, filePath);
}
module.exports = {
dirCreater: dirCreater,
fileHandler: fileHandler
}