-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
25 lines (23 loc) · 1.36 KB
/
index.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
const { createRandomProduct, randomProductFactory } = require('./products')
const { writeJsonFile, readJsonFile } = require('./helpers')
const run = () => {
// products is an array of objects OR if products.json is empty, then it is an empty array []
let products = readJsonFile("./data", "products.json")
if(process.argv[3]){
// console.log( randomProductFactory(process.argv[3]) )
// productsToAdd gets assigned the return value of the function call: randomProductFactory(process.argv[3]), which returns an ARRAY of objects
const productsToAdd = randomProductFactory(process.argv[3])
// We use the spread operator to push ALL OF THE CONTENTS of the productsToAdd array into the products array, as opposed to pushing the entire array giving us a nested array
products.push(...productsToAdd)
// Puts the products array into the file that we specify below: './data/products.json'
writeJsonFile('./data', "products.json", products)
} else {
// Create a new product, returns an object representing a product
const newProduct = createRandomProduct()
// Push the new products into the products array
products.push(newProduct)
// Replace the contents of the products.json file with products(the array we created)
writeJsonFile('./data', 'products.json', products)
}
}
run()