-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
195 lines (175 loc) · 5.37 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
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
import fs from 'fs';
import path from 'path';
const taskFilePath = path.join(process.cwd(), 'tasks.json');
const colors = {
reset: "\x1b[0m",
green: "\x1b[32m",
red: "\x1b[31m",
yellow: "\x1b[33m",
cyan: "\x1b[36m",
}
function readTasks() {
if (fs.existsSync(taskFilePath)) {
const data = fs.readFileSync(taskFilePath);
return JSON.parse(data);
}
return [];
}
function writeTasks(tasks) {
fs.writeFileSync(taskFilePath, JSON.stringify(tasks, null, 2), "utf-8");
}
function getNextId(tasks) {
const ids = tasks.map(task => task.id);
return ids.length ? Math.max(...ids) + 1 : 1;
}
function listTasks(status) {
const tasks = readTasks();
let filteredTasks = tasks;
if (status) {
if (status.toLowerCase() === 'done') {
filteredTasks = tasks.filter(task => task.completed);
}
else if (status.toLowerCase() === 'todo') {
filteredTasks = tasks.filter(task => !task.completed && !task.inProgress);
}
else if (status.toLowerCase() === 'inprogress') {
filteredTasks = tasks.filter(task => task.inProgress);
}
else {
console.log(`${colors.red}Invalid status. Please use todo, inprogress or done${colors.reset}`);
return;
}
}
if (filteredTasks.length) {
console.log(`${colors.cyan}Tasks${colors.reset}`);
filteredTasks.forEach(task => {
const status = task.completed ? 'done' : task.inProgress ? 'in progress' : 'todo';
console.log(`${colors.cyan}${task.id}${colors.reset}: ${task.description} - ${status}`);
});
}
else {
console.log(`${colors.yellow}No tasks found${colors.reset}`);
}
}
function addTask(description) {
const tasks = readTasks();
const newTask = {
id: getNextId(tasks),
description: description,
completed: false,
inProgress: false,
};
tasks.push(newTask);
writeTasks(tasks);
console.log(`${colors.green} Task added successfully ${colors.reset}`);
}
function updateTask(id, description) {
const tasks = readTasks();
const task = tasks.find(task => task.id === parseInt(id));
if (task) {
task.description = description;
writeTasks(tasks);
console.log(`${colors.green} Task updated successfully ${colors.reset}`);
}
else {
console.log(`${colors.red} Task not found ${colors.reset}`);
}
}
function deleteTask(id) {
const tasks = readTasks();
newTasks = tasks.filter(task => tasks.id !== parseInt(id));
if (newTasks.length < tasks.length) {
writeTasks(newTasks);
console.log(`${colors.green} Task deleted successfully ${colors.reset}`);
}
else {
console.log(`${colors.red} Task not found ${colors.reset}`);
}
}
function markDone(id) {
const tasks = readTasks();
const task = tasks.find(task => task.id === parseInt(id));
if (task) {
task.completed = true;
task.inProgress = false;
writeTasks(tasks);
console.log(`${colors.green} Task marked as done ${colors.reset}`);
}
else {
console.log(`${colors.red} Task not found ${colors.reset}`);
}
}
function markProgress(id) {
const tasks = readTasks();
const task = tasks.find(task => task.id === parseInt(id));
if (task) {
task.inProgress = true;
task.completed = false;
writeTasks(tasks);
console.log(`${colors.green} Task marked as in progress ${colors.reset}`);
}
else {
console.log(`${colors.red} Task not found ${colors.reset}`);
}
}
const args = process.argv.slice(2);
if (args.includes("add")) {
const taskDescription = args.slice(1).join(" ");
if (!taskDescription) {
console.log(`${colors.red}Please provide a description${colors.reset}`);
}
else {
addTask(taskDescription);
}
}
else if (args.includes("list")) {
const status = args[1];
listTasks(status);
}
else if (args.includes("update")) {
const id = args[1];
const description = args.slice(2).join(" ");
if (!id || !description) {
console.log(`${colors.red}Please provide an id and a description${colors.reset}`);
}
else {
updateTask(id, description);
}
}
else if (args.includes("delete")) {
const id = args[1];
if (!id) {
console.log(`${colors.red}Please provide an id${colors.reset}`);
}
else {
deleteTask(id);
}
}
else if (args.includes("done")) {
const id = args[1];
if (!id) {
console.log(`${colors.red}Please provide an id${colors.reset}`);
}
else {
markDone(id);
}
}
else if (args.includes("inprogress")) {
const id = args[1];
if (!id) {
console.log(`${colors.red}Please provide an id${colors.reset}`);
}
else {
markProgress(id);
}
}
else {
console.log(`${colors.cyan} Usage node index.js <command> [arguments] ${colors.reset}`);
console.log(`${colors.cyan} Commands: ${colors.reset}`);
console.log(`${colors.cyan} add <description> ${colors.reset}`);
console.log(`${colors.cyan} list [status] ${colors.reset}`);
console.log(`${colors.cyan} update <id> <description> ${colors.reset}`);
console.log(`${colors.cyan} delete <id> ${colors.reset}`);
console.log(`${colors.cyan} done <id> ${colors.reset}`);
console.log(`${colors.cyan} inprogress <id> ${colors.reset}`);
}