-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappendToFile.js
40 lines (37 loc) · 1.45 KB
/
appendToFile.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
const fs = require("fs");
const updateFile = (pathToTargetFile, pathForInputFileName) => {
let targetFileContent = "",
newFileContent = "";
return new Promise((resolve, reject) => {
const targetFileReadstream = fs.createReadStream(pathToTargetFile);
targetFileReadstream
.on("data", (chunk) => {
targetFileContent = targetFileContent + chunk;
targetFileContent = targetFileContent.replace("{", "").replace("}", "");
})
.on("error", (err) => reject(err))
.on("end", () => {
const inputFileReadstream = fs.createReadStream(pathForInputFileName);
inputFileReadstream
.on("data", (newData) => {
newFileContent = targetFileContent + newData;
newFileContent =
"{" + newFileContent.replace("{", "").replace("}", "") + "}";
})
.on("error", (err) => reject(err))
.on("end", () => {
fs.writeFile(pathToTargetFile, newFileContent, (err) => {
if (err) throw err;
console.log("Data appended to file");
resolve(" \r\n \r\n 🎉DONE🎉 \r\n \r\n please check target file:\r\n "+ pathToTargetFile +" \r\n \r\n ");
});
});
});
});
};
exports.updateFile = updateFile;
// (async () => {
// console.time("csv comparison time taken");
// console.log(await updateFile("./assets/2-secondary-file.csv"));
// console.timeEnd("csv comparison time taken");
// })();