-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (94 loc) · 2.9 KB
/
index.js
File metadata and controls
118 lines (94 loc) · 2.9 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
const { doesNotMatch } = require("assert");
const fs = require("fs")
function readTasks() {
const jsonText = fs.readFileSync("tasks.json", "utf-8")
if(!jsonText) return [];
const jsArr = JSON.parse(jsonText)
return jsArr;
}
function writeTasks(tasks) {
const inputTasts = JSON.stringify(tasks)
fs.writeFileSync("tasks.json", inputTasts)
}
let command = process.argv[2]
let arg1 = process.argv[3]
let arg2 = process.argv[4]
if(command === "add") {
let arr = readTasks()
let task = {
id: arr.length === 0 ? 1: Math.max(...arr.map(task => task.id)) + 1,
description: arg1,
status: "todo",
createdAt: new Date().toISOString(),
updatedAt: new Date(),
}
arr.push(task)
writeTasks(arr)
console.log(`Task added successfully (ID: ${task.id})`)
}
if(command === "list") {
let tasks = readTasks()
if (tasks.length === 0) return "No tasks found"
let arg = arg1
if(arg) {
tasks = tasks.filter((task) => task.status === arg)
}
for(let i = 0; i < tasks.length; i++) {
console.log(tasks[i].id, "|", tasks[i].description, "|", tasks[i].status)
}
}
if(command === "update") {
let tasks = readTasks()
const taskId = Number(arg1)
for(let i = 0; i < tasks.length; i++) {
if(taskId == tasks[i].id) {
tasks[i].description = arg2
tasks[i].updatedAt = new Date().toISOString()
console.log(`Task updated successfully (ID: ${taskId})`)
}
}
writeTasks(tasks)
}
if(command === "delete") {
let tasks = readTasks()
const taskId = Number(arg1)
const newArr = tasks.filter((task) => task.id !== taskId)
writeTasks(newArr)
console.log(`Task deleted successfully (ID: ${taskId})`)
}
if(command === "mark-in-progress") {
let tasks = readTasks()
const taskId = Number(arg1)
for(let i = 0; i < tasks.length; i++) {
if(taskId === tasks[i].id) {
tasks[i].status = "in-progress"
tasks[i].updatedAt = new Date().toDateString()
console.log(`Task status updated successfully (ID: ${taskId})`)
}
}
writeTasks(tasks)
}
if(command === "mark-done") {
let tasks = readTasks()
const taskId = Number(arg1)
for(let i = 0; i < tasks.length; i++) {
if(taskId === tasks[i].id) {
tasks[i].status = "done"
tasks[i].updatedAt = new Date().toDateString()
console.log(`Task status updated successfully (ID: ${taskId})`)
}
}
writeTasks(tasks)
}
if(command === "mark-todo"){
let tasks = readTasks()
const taskId = Number(arg1)
for(let i = 0; i < tasks.length; i++) {
if(taskId === tasks[i].id) {
tasks[i].status = "todo"
tasks[i].updatedAt = new Date().toDateString()
console.log(`Task status updated successfully (ID: ${taskId})`)
}
}
writeTasks(tasks)
}