UNPKG

mydata-cli

Version:

A CLI tool for interacting with MyData API and managing data. Supports login, data retrieval, and more. Built with Node.js.

175 lines (160 loc) 4.88 kB
import { Command } from "commander"; import { api } from "../utils/api.js"; const todo = new Command("todo").description("Manage todo tasks"); // ✅ List todos with optional status filter todo .command("list") .description("List all todos (optionally filtered by status)") .option("-s, --status <status>", "Filter by status: completed or pending") .action(async (options) => { try { const res = await api.get("/api/todos"); let todos = res.data.data; if ( options.status === "completed" || options.status === "done" || options.status === "finished" || options.status === "d" || options.status === "c" || options.status === "yes" || options.status === "y" ) { todos = todos.filter((t) => t.completed); } else if ( options.status === "pending" || options.status === "not done" || options.status === "n" || options.status === "no" || options.status === "undone" || options.status === "not completed" || options.status === "notfinished" ) { todos = todos.filter((t) => !t.completed); } if (!todos.length) return console.log("No matching tasks found."); todos.forEach((t) => { console.log(`${t.completed ? "✅" : "❌"} ${t.text}`); console.log(`ID: ${t._id}\n`); }); } catch (err) { console.error("❌", err.response?.data?.error || err.message); } }); // ➕ Add new todo todo .command("add <text>") .description("Add a new todo task") .action(async (text) => { try { const res = await api.post("/api/todos", { text,completed: false }); console.log("✅ Task added:", res.data.data); } catch (err) { console.error("❌", err.response?.data?.error || err.message); } }); // ✏️ Edit text of a todo todo .command("edit <id> <text>") .description("Edit the text of a todo") .action(async (id, text) => { try { const res = await api.patch("/api/todos", { id, updates: { text }, }); console.log("✏️ Updated task:", res.data.data.text); } catch (err) { console.error("❌", err.response?.data?.error || err.message); } }); // ✔️ Mark as done todo .command("done <id>") .description("Mark a todo as completed") .action(async (id) => { try { const res = await api.patch("/api/todos", { id, updates: { completed: true }, }); console.log("✅ Marked as done:", res.data.data.text); } catch (err) { console.error("❌", err.response?.data?.error || err.message); } }); // ❌ Mark as not done todo .command("undone <id>") .description("Mark a todo as not completed") .action(async (id) => { try { const res = await api.patch("/api/todos", { id, updates: { completed: false }, }); console.log("❌ Marked as not done:", res.data.data.text); } catch (err) { console.error("❌", err.response?.data?.error || err.message); } }); // 🗑️ Delete a todo todo .command("delete <id>") .description("Delete a todo") .action(async (id) => { try { await api.delete(`/api/todos?id=${id}`); console.log("🗑️ Task deleted"); } catch (err) { console.error("❌", err.response?.data?.error || err.message); } }); // ✔️ Batch mark as done todo .command("done-many <ids...>") .description("Mark multiple tasks as completed") .action(async (ids) => { try { for (const id of ids) { await api.patch("/api/todos", { id, updates: { completed: true }, }); console.log(`✅ Marked done: ${id}`); } } catch (err) { console.error("❌", err.response?.data?.error || err.message); } }); // ❌ Batch mark as not done todo .command("undone-many <ids...>") .description("Mark multiple tasks as not completed") .action(async (ids) => { try { for (const id of ids) { await api.patch("/api/todos", { id, updates: { completed: false }, }); console.log(`❌ Marked undone: ${id}`); } } catch (err) { console.error("❌", err.response?.data?.error || err.message); } }); // 🗑️ Batch delete todo .command("delete-many <ids...>") .description("Delete multiple tasks") .action(async (ids) => { try { for (const id of ids) { await api.delete(`/api/todos?id=${id}`); console.log(`🗑️ Deleted: ${id}`); } } catch (err) { console.error("❌", err.response?.data?.error || err.message); } }); export default todo;