-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (152 loc) · 5.05 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
import fs from 'fs';
import inquirer from 'inquirer';
const filePath = './example.json';
// this function will create file with json extension if not exists ---------->
const initializingFile = () => {
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, JSON.stringify([]), 'utf8');
}
};
// read the data from file and return an array of objects -->
const readFileData = () => {
const fileData = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(fileData);
};
// rewrite the updated file data
const writeFileData = (tasks) => {
fs.writeFileSync(filePath, JSON.stringify(tasks), 'utf8');
};
const addNewTask = (task) => {
const tasks = readFileData();
const newTask = {
id: tasks.length + 1,
task: task,
status: "todo",
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
tasks.push(newTask);
writeFileData(tasks);
console.log(`Task successfully added to the list (ID: ${newTask.id})`);
};
const updateFileData = (id, updatedTask) => {
id = parseInt(id);
const tasks = readFileData();
if (tasks.length >= id) {
const filteredData = tasks.find((task) => task.id === id);
filteredData.task = updatedTask;
filteredData.updatedAt = new Date().toISOString();
writeFileData(tasks);
console.log(`Task updated successfully! (ID: ${filteredData.id})`);
} else {
console.log("Please enter a valid Task-ID!");
}
};
const deleteTask = (id) => {
id = parseInt(id);
const tasks = readFileData();
if (tasks.length >= id) {
const filteredData = tasks.filter((task) => task.id !== id);
filteredData.forEach((task, index) => {
task.id = index + 1;
});
writeFileData(filteredData);
console.log(`Task deleted successfully (ID: ${id})`);
} else {
console.log("Please enter a valid Task-ID!");
}
};
const editTaskStatus = (id, status) => {
id = parseInt(id);
const tasks = readFileData();
if (tasks.length >= id) {
const filteredData = tasks.find((task) => task.id === id);
filteredData.status = status;
filteredData.updatedAt = new Date().toISOString();
writeFileData(tasks);
console.log(`Status updated successfully! (Task-ID: ${filteredData.id})`);
} else {
console.log("Please enter a valid Task-ID!");
}
};
const displayTaskStatusBased = (status = "all") => {
const tasks = readFileData();
if (status === "all") {
console.log(`List of Tasks: ${status}\n`, tasks);
} else {
const filteredData = tasks.filter((task) => task.status === status);
console.log(`List of Tasks (${status}):\n`, filteredData);
}
};
(async function () {
initializingFile();
const { action } = await inquirer.prompt([
{
type: 'list',
name: 'action',
message: 'What would you like to do?',
choices: [
'Add Task',
'Update Task',
'Delete Task',
'Mark Task In-Progress',
'Mark Task Done',
'List Tasks',
'Exit'
],
},
]);
switch (action) {
case 'Add Task': {
const { task } = await inquirer.prompt([
{ type: 'input', name: 'task', message: 'Enter task description:' },
]);
addNewTask(task);
break;
}
case 'Update Task': {
const { id, updatedTask } = await inquirer.prompt([
{ type: 'input', name: 'id', message: 'Enter Task ID to update:' },
{ type: 'input', name: 'updatedTask', message: 'Enter new description:' },
]);
updateFileData(id, updatedTask);
break;
}
case 'Delete Task': {
const { id } = await inquirer.prompt([
{ type: 'input', name: 'id', message: 'Enter Task ID to delete:' },
]);
deleteTask(id);
break;
}
case 'Mark Task In-Progress': {
const { id } = await inquirer.prompt([
{ type: 'input', name: 'id', message: 'Enter Task ID to mark as in-progress:' },
]);
editTaskStatus(id, 'in-progress');
break;
}
case 'Mark Task Done': {
const { id } = await inquirer.prompt([
{ type: 'input', name: 'id', message: 'Enter Task ID to mark as done:' },
]);
editTaskStatus(id, 'done');
break;
}
case 'List Tasks': {
const { status } = await inquirer.prompt([
{
type: 'list',
name: 'status',
message: 'Select status to filter tasks:',
choices: ['all', 'todo', 'in-progress', 'done'],
},
]);
displayTaskStatusBased(status);
break;
}
case 'Exit':
console.log('Goodbye!');
process.exit(0);
}
})();