-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
239 lines (186 loc) · 7.61 KB
/
index.js
File metadata and controls
239 lines (186 loc) · 7.61 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import process from 'process';
import fs from 'fs'
function list(taskStatus) {
try {
const data = fs.readFileSync('tasks.json', 'utf8');
const tasks = JSON.parse(data);
if (taskStatus === "todo"){
const taskstate = tasks.filter(t => t.status === "todo")
if (taskstate.length === 0){
console.log("This task status does not exisit")
}
taskstate.forEach(task => {
console.log([task.id], task.description, "--", task.status)
});
} else if (taskStatus === "in-progress"){
const taskstate = tasks.filter(t => t.status === "in-progress")
if (taskstate.length === 0){
console.log("This task status does not exisit")
}
taskstate.forEach(task => {
console.log([task.id], task.description, "--", task.status)
});
} else if (taskStatus === "done"){
const taskstate = tasks.filter(t => t.status === "done")
if (taskstate.length === 0){
console.log("This task status does not exisit")
}
taskstate.forEach(task => {
console.log([task.id], task.description, "--", task.status)
});
} else {
tasks.forEach(task => {
console.log([task.id], task.description, "--", task.status);
});
}
if (tasks.length === 0){
console.log("There are no tasks available to list. Use 'add' followed by your task description to create a new task entry!");
}
} catch (error) {
console.error("There is an error:", error.message);
process.exit(1);
}
}
function add(addDescription) {
if (!addDescription || !addDescription.trim()){
console.log("To add a task, please enter a description!");
process.exit(1);
}
const data = fs.readFileSync('tasks.json', 'utf8');
const tasks = JSON.parse(data);
if (tasks){
try {
let createdAt = new Date().toISOString();
const updatedAt = new Date().toISOString();
if (tasks.length === 0){
const newTask = {
"id": 1,
"description": addDescription,
"status": "todo",
"createdAt": createdAt,
"updatedAt": updatedAt
}
tasks.push(newTask);
fs.writeFileSync('./tasks.json', JSON.stringify(tasks, null, 2));
console.log("Task added successfully:", newTask);
}
if (tasks.length > 0) {
const largestId = Math.max(...tasks?.map(t => t.id))
const addNewId = largestId + 1
const newTask = {
"id": addNewId,
"description": addDescription,
"status": "todo",
"createdAt": createdAt,
"updatedAt": updatedAt
}
tasks.push(newTask);
fs.writeFileSync('./tasks.json', JSON.stringify(tasks, null, 2));
console.log("Task added successfully:", newTask);
}
} catch (error) {
console.error("There is an error:", error.message);
}
}
}
function update({id, updateDescription}) {
if (!id) {
console.log("\nProvide the 'id' of the task you would like to update followed by the new task 'description'\n")
return list();
}
if (!updateDescription) {
console.log("\nProvide the 'description' of the new task\n")
return list();
}
try {
const data = fs.readFileSync('tasks.json', 'utf8');
const tasks = JSON.parse(data);
const index = tasks.findIndex((t => t.id === id))
if (id && index === -1 ){
console.log("This task id has not been found")
return list();
}
const updatedTask = {
id: tasks[index].id,
description: updateDescription,
status: tasks[index].status,
createdAt: tasks[index].createdAt,
updatedAt: new Date().toISOString()
}
tasks[index] = updatedTask
fs.writeFileSync('./tasks.json', JSON.stringify(tasks, null, 2));
console.log("Task updated successfully:", updatedTask);
} catch(error) {
console.error("There is an error:", error.message);
}
}
function updateStatus(id){
if (!id) {
console.log("Please provide the 'id' to update the status from the following tasks");
return list();
}
try {
const data = fs.readFileSync('tasks.json', 'utf8');
const tasks = JSON.parse(data);
const index = tasks.findIndex((t => t.id === id))
if (id && index === -1 ){
console.log("This task id has not been found")
return list();
}
const statusChange = command.slice(5);
const updatedStatus = {
id: tasks[index].id,
description: tasks[index].description,
status: statusChange,
createdAt: tasks[index].createdAt,
updatedAt: new Date().toISOString()
}
tasks[index] = updatedStatus
fs.writeFileSync('./tasks.json', JSON.stringify(tasks, null, 2));
console.log("Task updated successfully:", updatedStatus);
} catch(error){
console.error(error.message)
}
}
function deleteTask(id) {
if (!id){
console.log("Please provide an id of the task you would like to delete")
list();
process.exit(1);
}
try {
const data = fs.readFileSync('tasks.json', 'utf8');
const tasks = JSON.parse(data);
const index = tasks.findIndex((t => t.id === id))
if (id && index === -1 ){
console.log("This task id has not been found, select from exisiting one")
return list();
}
const updatedTask = tasks.filter(t => t.id !==id)
fs.writeFileSync('./tasks.json', JSON.stringify(updatedTask, null, 2));
console.log("Task deleted successfully");
} catch(error) {
console.log("There is an error:", error.message)
}
}
const argumentList = process.argv.slice(2)
const command = argumentList[0]
const id = Number(argumentList[1])
const addDescription = argumentList.slice(1).join(" ")
const updateDescription = argumentList.slice(2).join(" ")
const taskStatus = argumentList.slice(1).join(" ")
if (command === "add"){
add(addDescription);
} else if (command === "list"){
list(taskStatus);
} else if (command === "update"){
update({ id, updateDescription });
} else if (command === "mark-in-progress" || command === "mark-done" || command === "mark-todo"){
updateStatus(id);
} else if (command === "delete") {
deleteTask(id);
} else if (command === "--help") {
console.log(" Your Task Manager:",`\n`, `\n`,"Please select the following commands:",`\n`,"-'list' (to list all)" ,`\n`, "-'list mark-[status]' (to can sort and change by the following status: 'todo', 'in-progess', 'done')",`\n`,"-'add' (provide a descriprion) ", `\n`,"-'update' (provide the exisiting id and new description) ", `\n`,"-'delete' (provide the id)" )
} else {
console.log(" Your Task Manager:",`\n`, `\n`,"Please select the following commands:",`\n`,"-'list' (to list all)" ,`\n`, "-'list mark-[status]' (to can sort and change by the following status: 'todo', 'in-progess', 'done')",`\n`,"-'add' (provide a descriprion) ", `\n`,"-'update' (provide the exisiting id and new description) ", `\n`,"-'delete' (provide the id)" )
}