Create a file data.json
which contains the following JSON structure:
[{"id": "tfHxMIRS-g7pX1rRLVzm0", "name": "Asaad Saad"}, {"id": "YdjFznHRgOi_UzYbXbLO0", "name": "Theo Saad"}]
- Create a class
Names
, once instantiated, it reads the JSON file synchronously, and assigns its content to a private propertydata
. - Create a method
persist()
to write back the instance propertydata
back into thedata.json
file, and emit an eventdata_saved
to confirm that the file is written successfully. - Create a method
addName(name: string)
to add a new name object todata
, generate a new id using nanoid package, then persist the data changes to the hard drive indata.json
. - Create a method
getNames()
to return all names fromdata
. - Create a method
getNameById(id: string)
to return the name of the providedid
. - Create a method
updateNameById(id: string, new_name: string)
to update the name property of the providedid
fromdata
, then persist the changes to the hard drive indata.json
. - Create a method
deleteNameById(id: string)
to remove the name object of the givenid
fromdata
, then persist the changes to the hard drive indata.json
.
Test your code:
- create an instance of
Names
class. - listen to
data_saved
event, and print a messageData has been saved to data.json successfully
. - call all the methods and test.
Create an asynchronous function checkSystem()
that returns a promise and checks if the OS memory size is at least 8 GB and the processor has at least 4 cores (use os
core module, which has the following methods: cpus()
and totalmem()
).
- If the system doesn't have enough memory we should reject with a message:
You need at least 8 GB of RAM
- If the system doesn't have at least 4 cores, reject with this message:
Processor must have at least 4 cores
- If the system has enough specs, resolve with the following message
System is checked successfully.
Note: 1 KB is 1024 bytes, 1 MB is 1024 KB, 1 GB is 1024 MB.
- Create a folder
/input
and place 10 high-resolution photos from Pexels. - Create an empty folder
/output
. - Read Node documentation to learn about working with folders in Node using the
fs
core module. - Write a script read the images' name in both the
/input
and/output
folders, and determine the difference between them (files that exist in/input
but does not exist in/output
). - If an image exists in
/input
and does not exist in/output
, use Sharp to resize the source image to a thumbnail size of 200 width, and save the thumbnail image in the/output
folder. - Use Node Cron to schedule a cron job using the following step value
*/30 * * * * *
that runs your script every 30 seconds.
Remember to install the definately typed (DT) package as a development dependency if the package does not provide types.
Environment variables hold secret keys and sensitive configuration data that we prefer to keep in a safe place. They are usually accessed from the process.env
global object.
Update the previous exercise to send you an email with the following body:
From: Thumbnail Generator App
Subject: Status Report
Body:
This is an automated email.
The input folder has 31 thumbnails.
The output folder has 31 thumbnails.
Generated 0 new thumbnails.
Use the dotenv module to save environment variables.
Use the @sendgrid/mail module and integrate it with your application to send the status email.