-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (95 loc) · 2.91 KB
/
index.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var fs_1 = require("fs");
var commander_1 = require("commander");
var program = new commander_1.Command();
var users = JSON.parse(
fs_1.readFileSync("./todolist.json", { encoding: "utf-8" })
);
program
.name("ToDoList")
.description("Creating To do list")
.version("0.8.0");
program
.command("add")
.description("Add new TODO")
.requiredOption("-t, --title <string>", "TODOS must have titles")
.addOption(
new commander_1.Option(
"-s, --status <string>",
"status for the TODO"
).choices(["to-do", "in Progress", "Done"])
)
.action(function (options) {
var listId = 1;
if (users.length) {
listId = users[users.length - 1].id + 1;
}
users.push({
id: listId,
title: options.title,
status: options.status || "to-do",
});
fs_1.writeFileSync("./todolist.json", JSON.stringify(users, null, 2));
// console.log(users)
});
program
.command("list")
.description("Add new TODO")
.addOption(
new commander_1.Option(
"-s, --status <string>",
"status for the TODO"
).choices(["to-do", "in Progress", "Done"])
)
.action(function (options) {
var status = options.status || "to-do";
// console.log(status)
console.log(
users.filter(function (todo) {
// console.log(todo.status);
return todo.status == status;
})
);
});
program
.command("update")
.description("Update a todo")
.requiredOption("-i , --id <string>", "The ID of the TODO")
.requiredOption("-t , --title <string>", "The new title of the TODO")
.addOption(
new commander_1.Option(
"-s, --status <string>",
"status for the TODO"
).choices(["to-do", "in Progress", "Done"])
)
.action(function (options) {
if (
users.find(function (_a) {
var id = _a.id;
return id === Number(options.id);
})
) {
users = users.map(function (user) {
return user.id == options.id
? {
...user,
title: options.title,
status: options.status || user.status,
}
: user;
});
fs_1.writeFileSync("./todolist.json", JSON.stringify(users, null, 2));
} else {
console.log("wrong id");
}
});
program
.command("delete")
.description("delete a todo")
.requiredOption("-i , --id <string>", "The ID of the TODO")
.action(function (options) {
users = users.filter(function (user) {
return user.id != options.id;
});
fs_1.writeFileSync("./todolist.json", JSON.stringify(users, null, 2));
});
program.parse();