-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.mjs
More file actions
78 lines (69 loc) · 2.11 KB
/
todo.mjs
File metadata and controls
78 lines (69 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { throws } from "assert";
import { Command } from "commander";
import fs from "fs";
import { json } from "stream/consumers";
const program = new Command();
let count = 0;
function readData() {
const data = fs.readFileSync("todos.json", "utf-8");
return JSON.parse(data);
}
program
.name("todo-list")
.description("CLI command to manage a todo list")
.version("0.0.1");
program
.command("add")
.description("Add an item to todo list")
.argument("<string>", "Name of the item")
.action((str, options) => {
const todo = {
id: count++,
item: str,
};
const todos = readData();
todos.push(todo);
const jsonTodo = JSON.stringify(todos, null, 4);
fs.writeFile("todos.json", jsonTodo, "utf-8", (err) => {
console.log("Task added successfully!");
});
});
program
.command("remove")
.description("Removes an item from todo")
.argument("<string>", "Item to remove")
.action((str, options) => {
let todos = readData();
todos = todos.filter((todo) => todo["item"] !== str);
const jsonTodo = JSON.stringify(todos);
fs.writeFile("todos.json", jsonTodo, "utf-8", (err) => {
console.log("Task removed successfully!");
});
});
program
.command("update")
.description("Updates an existing task")
.argument("<old>", "Write name of previous item \nEg. 'meeting at 4'")
.argument("<new>", "Write updated one \nEg. 'meeting at 5'")
.action((str1, str2) => {
let todos = readData();
todos.forEach((todo) => {
if (todo["item"] === str1) {
todo["item"] = str2;
}
});
const jsonTodo = JSON.stringify(todos);
fs.writeFile("todos.json", jsonTodo, "utf-8", (err) => {
console.log("Task updated successfully!");
});
});
program
.command('read')
.description('Displays the todo list')
.action(() => {
const todos = readData();
todos.forEach(todo => {
console.log(todo.item);
})
})
program.parse();