Skip to content

Commit

Permalink
End of Lesson (Fakerjs + fs Read and Write)
Browse files Browse the repository at this point in the history
  • Loading branch information
SDBoahen committed Jun 1, 2023
0 parents commit c6771df
Show file tree
Hide file tree
Showing 4,518 changed files with 24,525 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Empty file added data/products.json
Empty file.
1 change: 1 addition & 0 deletions data/products.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"_id":"f0beaf53-1a36-41c1-aa9c-6d9dd1e96980","name":"Gorgeous Gloves","description":"Andy shoes are designed to keeping in mind durability as well as trends, the most stylish range of shoes & sandals","brand":"Bahringer Inc","price":"$71.00","currency":"USD","inStock":false,"attributes":{"material":"Concrete","color":"orchid"}}]
58 changes: 58 additions & 0 deletions helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//// 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
}


34 changes: 34 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// console.log(":)")



const { createRandomProduct } = require("./products.js")

const { writeJSONFile, readJSONFile } = require("./helpers.js")


//// run: function to run our entire Command Line App

function run(){

console.log("Welcome to Our Products App!✨\n\n\n\n")

// console.log( "Us Creating a Random Product (from the require): ", createRandomProduct() )


let productsData = readJSONFile( "./data" , "products.json" )
// if: file is empty -> Give us an empty array; Save it to our productsData variable
// else: give us the existing array (collection); Save it to our productsData variable

productsData.push( createRandomProduct() )


writeJSONFile( "./data" , "products.json", productsData )


}

run()


//////// process object + process.argv
18 changes: 18 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

443 changes: 443 additions & 0 deletions node_modules/@faker-js/faker/CHANGELOG.md

Large diffs are not rendered by default.

133 changes: 133 additions & 0 deletions node_modules/@faker-js/faker/CHANGELOG_old.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions node_modules/@faker-js/faker/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c6771df

Please sign in to comment.