Initial commit: demo todo CLI with Claude Code setup
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
10 files changed
+193
No files matched your search
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env node
|
||||
import { load, save, add, complete, remove, format } from "./todo.js";
|
||||
|
||||
const FILE = process.env.TODO_FILE ?? "todos.json";
|
||||
const [cmd, ...args] = process.argv.slice(2);
|
||||
let todos = load(FILE);
|
||||
|
||||
switch (cmd) {
|
||||
case "add":
|
||||
todos = add(todos, args.join(" "));
|
||||
break;
|
||||
case "done":
|
||||
todos = complete(todos, Number(args[0]));
|
||||
break;
|
||||
case "rm":
|
||||
todos = remove(todos, Number(args[0]));
|
||||
break;
|
||||
case "ls":
|
||||
case undefined:
|
||||
console.log(format(todos));
|
||||
process.exit(0);
|
||||
default:
|
||||
console.error(`Unknown command: ${cmd}\nUsage: todo [ls | add <text> | done <id> | rm <id>]`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
save(FILE, todos);
|
||||
console.log(format(todos));
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
import { readFileSync, writeFileSync, existsSync } from "node:fs";
|
||||
|
||||
export function load(path) {
|
||||
if (!existsSync(path)) return [];
|
||||
return JSON.parse(readFileSync(path, "utf8"));
|
||||
}
|
||||
|
||||
export function save(path, todos) {
|
||||
writeFileSync(path, JSON.stringify(todos, null, 2) + "\n");
|
||||
}
|
||||
|
||||
export function add(todos, text) {
|
||||
const id = todos.length + 1;
|
||||
return [...todos, { id, text, done: false }];
|
||||
}
|
||||
|
||||
export function complete(todos, id) {
|
||||
return todos.map((t) => (t.id === id ? { ...t, done: true } : t));
|
||||
}
|
||||
|
||||
export function remove(todos, id) {
|
||||
return todos.filter((t) => t.id !== id);
|
||||
}
|
||||
|
||||
export function format(todos) {
|
||||
if (todos.length === 0) return "Nothing to do.";
|
||||
return todos.map((t) => `${t.done ? "[x]" : "[ ]"} ${t.id}. ${t.text}`).join("\n");
|
||||
}
|
||||
Reference in new issue
Block a user