-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
58 lines (28 loc) · 1.32 KB
/
helpers.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
56
//// Here will will write our File System Reading and Writing Functionality
const fs = require("node:fs")
// console.log("Node's File System: " , fs)
// console.log("The Keys for the Node FS: ", Object.keys(fs))
// console.log("The Function readFileSync: ", Object.keys(fs.readFileSync))
// console.log("The Function writeFileSync: ", Object.keys(fs.writeFileSync))
//// readFileSync( "pathToTheFile", "howToReadTheFile")
// const path = "./data"
// const fileName = "products.json"
// const collection = fs.readFileSync( `${path}/${fileName}` , "utf-8" )
// // condition ? trueAction : falseAction ~ Similar to if/else
// // if (condition){ trueAction } else { falseAction }
// const resultOfTernary = collection ? JSON.parse(collection) : [];
// console.log( "Data from JSON File We Are Reading: " , resultOfTernary )
function readJSONFile(path, fileName){
const collection = fs.readFileSync( `${path}/${fileName}` , "utf-8" )
// is it an empty JSON File or NOT
return collection ? JSON.parse(collection) : [];
}
//// writeFileSync( "path" , data, { encoding: "utf-8" } )
function writeJSONFile(path, fileName, data){
data = JSON.stringify(data)
fs.writeFileSync( `${path}/${fileName} `, data , { encoding: "utf-8" } )
}
module.exports = {
readJSONFile,
writeJSONFile
}