Build a To-Do List CLI with File Storage
Turn small commands into a real tool you can use every day
process.argv command line and reading/writing files with the fs module -- to build a real to-do list program. You will type commands like node todo.js add "Buy milk" in the terminal, and your program will remember your tasks in a JSON file, even after you close it.
- read a command and its argument from
process.argv, - load tasks from a JSON file and save them back,
- build four actions:
add,list,done, anddelete, - handle the very first run, when the file does not exist yet,
- put it all together into one small, tidy program.
1. What are we building?
Imagine a sticky note on your fridge where you write down chores: "Buy milk", "Walk the dog", "Finish homework". When a chore is done, you cross it out. When it no longer matters, you throw the note away. Our program is that fridge note, but it lives in your terminal.
Analogy. Think of the program as a small helper at a coat check. You walk up and say a short command -- "add this", "show my list", "mark number 2 done" -- and the helper writes everything in a little notebook kept in a drawer. The notebook is our JSON file. Every time you come back, the helper opens the same notebook, so nothing is forgotten.
Here is what using the finished tool looks like in the terminal:
node todo.js add "Buy milk"
node todo.js add "Walk the dog"
node todo.js list
node todo.js done 0
node todo.js delete 1
Each line is one command. The word right after todo.js (add, list, done, delete) tells the program what to do. That is the whole idea of a CLI -- a command-line interface.