In this assignment, students will create a Node.js application that uses the built-in “fs” and “readline” modules to interact with files and directories. This program will read data from a file or directory specified by the user, analyze the content, and generate a report in the console.
To begin this assignment, make sure you have installed both Visual Studio Code and Node (ie: you should be able to run programs written in JavaScript from the Integrated Terminal in Visual Studio Code using the command “node filename.js“)
(Assignment Folder)
planets
– Earth.txt
– Jupiter.txt
– Mars.txt
– Mercury.txt
– Neptune.txt
– Saturn.txt
– Uranus.txt
– Venus.txt
a1.js
milky-way.txt
solar.txt
With our files / folders in place, we can begin editing a1.js. The first thing we must do is to read the files and folder(s) in the CURRENT (i.e. assignment folder) and list them in terminal. This can be done using the built-in “fs” module as mentioned in the notes.
If the user run the a1.js using Node .js, we must process to read the current (‘.’) directory to get the files and folders in the current directory and show a menu in terminal so the user can work with the menu in next step. The following is what we should show in terminal:
NOTE: If the directory cannot be read, output the error to the console using “console.log(err.message);”
The second thing we must do is determine whether the user wishes to analyze a file or directory. This can be done using the “readline” module as mentioned in the notes. The prompts for the user should be the following (sample user responses in red):
NOTE: For now, we will simply allow the user to enter a file or directory NAME from the menu created in the last step (one case at a time):
If the user enter a file name (e.g. “solar.txt” or “milky-way.txt”), then we must process the file name that the user entered (“solar.txt” from the example above) to generate the following report.
NOTE: If the file cannot be read, output the error to the console using “console.log(err.message);”
Assuming that the following report was generated from the provided “solar.txt” file, the user should see the below information in the console (instead of the “TODO” output created in Step 3):
HINTS: To determine a path is file of directory, we use:
To get the contents of the file as a string without any newline characters, the following code may be used:
Similarly, to get an array of words from the file contents (string), the below code can be used:
If the user enters a directory name (e.g. “planets” for directory planets), then we must show all files and folders in this sub directory (similar to Step 2) and show them in one line and separated with comma (,).
The sample output in terminal:
Using Node.js process ‘exit’ event (handler) to output the current directory in operating system to terminal when the Node.js process is about to exit, i.e., show the following (as example) in terminal when the program stops after analyzing a file, showing files in a directory, or showing an error message:
Hard coding will result in penalties or a zero-mark for the assignment. So, make sure that there is not hard coded code in your assignment 1. For example, if we change the file and directory names in the project (as shown below), the a1.js must keep working:
(Assignment Folder)
innerPlanets
– Earth.txt
– Mars.txt
– Mercury.txt
– Venus.txt
outerPlanets
– Jupiter.txt
– Neptune.txt
– Saturn.txt
– Uranus.txt
a1.js
space.txt
Reviews
There are no reviews yet.