Project Requirements:
Develop a simple command line TypeScript Todo Project using TypeScipt, Node.js and Inquirer.
Analysis:
The TypeScript ToDo Project program will prompt you to select an option:
Please select an option:
- Add todo
- Delete todo
- Exit
If you select “Add todo”, the program will prompt you to enter a todo task. If you select “Delete todo”, the program will list all of your todo tasks and prompt you to select one to delete.
Once you have finished using the program, you can exit by pressing or by selecting “Exit” from the menu.
Shebang code:
The line #!/usr/bin/env node
in TypeScript code is called a shebang. It tells the operating system to use the Node.js interpreter to run the script. This is a common convention in Node.js scripts.
Import modules:
The `inquirer` module is imported from the `inquirer` package.
You need to install the inquirer module first using the npm install inquirer command.
Define Interface:
Define an interface called ansType
with two properties: menuOpt
of type string
and todo
of type string
.
Declare Variables:
Declare the following variables:
todos
: A list of TODO items (type:string[]
)loop
: A boolean variable that controls the main loop of the program (type:boolean
)answers1
: The user’s response to the main menu prompt (type:ansType
)answers2
: The user’s response to the “Enter What to Do?” prompt (type:ansType
)answers3
: The user’s response to the “Please select TODO for delete:” prompt (type:ansType
)
Function startLoop:
Define an asynchronous function called startLoop()
, which will start the main loop of the program. The loop will continue to run while the loop
variable is true
. Within the loop, the displayMenuItem()
function will be called, which will display the main menu to the user and prompt them to select an option.
Call the startLoop()
function to start the main loop of the program.
Function displayMainMenu:
Define an asynchronous function called displayMenuItem()
, which will display the main menu to the user and prompt them to select an option.
Prompt the user to select a menu item using the inquirer.prompt()
function. The inquirer.prompt()
function returns a promise, which represents the eventual completion or failure of the function.
Wait for the user to select a menu item before continuing.
Switch on the user’s selection:
- If the user selects “Add ToDo item”, call the
addTodo()
function. - If the user selects “Delete ToDo item”, call the
deleteTodo()
function. - If the user selects “Exit”, set the
loop
variable tofalse
to exit the main loop of the program.
Log a message to the console to indicate that the program is exiting.
Function addTodo:
Define an asynchronous function called addTodo()
, which will add a new TODO item to the todos
list.
Prompt the user to enter a TODO item using the inquirer.prompt()
function. The inquirer.prompt()
function returns a promise, which represents the eventual completion or failure of the function.
Wait for the user to enter a TODO item before continuing.
Add the user’s input to the todos
list.
Log the updated todos
list to the console.
Function deleteTodo:
Define an asynchronous function called deleteTodo()
, which will delete a TODO item from the todos
list.
If the todos
list is not empty, prompt the user to select a TODO item to delete using the inquirer.prompt()
function. The inquirer.prompt()
function returns a promise, which represents the eventual completion or failure of the function.
Wait for the user to select a TODO item to delete before continuing.
Iterate over the todos
list and delete the TODO item that matches the user’s selection.
If the TODO item was deleted successfully, log the updated todos
list to the console.
Otherwise, log a message to the console indicating that there are no TODO items to delete.