Capstone: todo CLI
The spec
The todo CLI manages one list: add items, mark them done, remove them, and print what is left. All state lives in a single JSON file next to the script.
Keep the scope tight on purpose: four commands and one file store. A small surface leaves room to finish parsing, storage, and tests in one sitting. The program builds one todo and prints its title and flag.
The finished tool answers the list command with the rows it holds:
node todo.js list
1. Buy milk
type Todo = { id: number; title: string; done: boolean; }; const one: Todo = { id: 1, title: "Buy milk", done: false }; console.log(one.title, one.done);
Buy milk false
Parse argv
Node exposes raw arguments in process.argv, with the first two entries reserved for the binary and the script path. Slice from index two to read only what the user typed.
Map the first word to a command and fall back to list when nothing is given. Unknown words print usage, so the tool teaches itself on every mistake. Started with no arguments, the program falls back to list and prints it.
function parseArgs(argv: Array<string>): string { const head: string | undefined = argv[0]; if (head === undefined) { return "list"; } return head; } const action: string = parseArgs(process.argv.slice(2)); console.log(action);
list
JSON file store
The store loads the JSON file into an array of todos and writes the whole array back after each change. Full file rewrites stay simple and safe at this size.
Read with readFileSync and parse into unknown before guarding, then stringify on save. Sync calls keep the CLI code linear because the process exits right after. The program writes a two-row file, loads it, saves it back and prints how many rows it read.
import { readFileSync, writeFileSync } from "node:fs"; type Todo = { id: number; title: string; done: boolean; }; function loadTodos(path: string): Array<Todo> { const raw: string = readFileSync(path, "utf8"); return JSON.parse(raw) as Array<Todo>; } function saveTodos(path: string, todos: Array<Todo>): void { writeFileSync(path, JSON.stringify(todos)); } writeFileSync("todos.json", JSON.stringify([ { id: 1, title: "Buy milk", done: false }, { id: 2, title: "Ship it", done: true }, ])); const todos = loadTodos("todos.json"); saveTodos("todos.json", todos); console.log(loadTodos("todos.json").length);
2
Assert tests for the store
Test the argument parser and the store with the built in assert module. Each strictEqual call pins one behavior, and any failure throws with a clear diff.
Runtime background lives in the JavaScript section, which covers the event loop and modules the CLI relies on. Run the file with node and watch every assertion pass before wiring the real commands. The program proves both parser cases and prints its pass line.
import assert from "node:assert"; function parseArgs(argv: Array<string>): string { const head: string | undefined = argv[0]; if (head === undefined) { return "list"; } return head; } assert.strictEqual(parseArgs([]), "list"); assert.strictEqual(parseArgs(["add"]), "add"); console.log("store tests pass");
store tests pass
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which entries of
process.argvbelong to the user?Answer
The ones from index two onwards
The first two are the binary and the script path, so the command starts at index two.
-
Why rewrite the whole JSON file after a change?
Answer
The file is small, and a full rewrite stays simple and safe
At this size the whole-array write is the simplest correct store.
-
Why read the file and parse into
unknownbefore using it?Answer
The file may hold anything, so the shape must be proven
A file is outside data like any other, so a guard or a deliberate assertion protects the rest of the program.
-
What does one
strictEqualassertion pin?Answer
One behavior, and a failure throws with a diff
One assertion per behavior keeps a failure pointed at exactly one promise.
-
What does this program print?
function parseArgs(argv: Array<string>): string { const head: string | undefined = argv[0]; return head === undefined ? "list" : head; } console.log(parseArgs([]), parseArgs(["add"]));Answer
list add
The empty list falls back to list, and the given word is returned as it is.
-
What does this program print?
function parseArgs(argv: Array<string>): string { return argv[0] ?? "list"; } console.log(parseArgs(["done", "3"]));Answer
done
Only the first word picks the command, and the rest is left for later steps.
-
What does this program print?
import { writeFileSync, readFileSync } from "node:fs"; type Todo = { id: number; title: string; done: boolean }; const rows: Array<Todo> = [ { id: 1, title: "Buy milk", done: false }, { id: 2, title: "Ship it", done: true }, ]; writeFileSync("t.json", JSON.stringify(rows)); const back = JSON.parse(readFileSync("t.json", "utf8")) as Array<Todo>; console.log(back.length, back[0].title);Answer
2 Buy milk
The round trip keeps both rows and their order, so the first title comes back first.
-
What does this program print?
const todos = [ { title: "Buy milk", done: false }, { title: "Ship it", done: true }, ]; const left = todos.filter((todo) => !todo.done); console.log(left.length, left[0].title);Answer
1 Buy milk
The filter keeps the one todo that is not done, and its title is printed.
-
What does the compiler say about this program?
type Todo = { id: number; title: string; done: boolean }; const one: Todo = { id: 1, title: "Buy milk" }; console.log(one.title);Answer
error TS2741: Property 'done' is missing in type '{ id: number; title: string; }' but required in type 'Todo'.
The type requires all three fields, and the object leaves one out.
-
What does the compiler say about this program?
type Todo = { id: number; title: string; done: boolean }; const one: Todo = { id: "1", title: "Buy milk", done: false }; console.log(one);Answer
error TS2322: Type 'string' is not assignable to type 'number'.
The id is declared as a number, and the text in the object does not fit it.
Article author: Arthur Isaev