-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
208 lines (197 loc) · 5.41 KB
/
app.js
File metadata and controls
208 lines (197 loc) · 5.41 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
#!/usr/bin/env node
// Get arguments (first two are 'node' and 'app.js')
const args = process.argv.slice(2);
const fs=require('fs');
if (!fs.existsSync("data.json")) {
fs.writeFileSync("data.json", "[]");
}
const stats = fs.statSync("data.json");
const chalk=require('chalk');
const color_text=(color,text)=>{
const colors = {
black: chalk.black,
red: chalk.red,
green: chalk.green,
yellow: chalk.yellow,
blue: chalk.blue,
magenta: chalk.magenta,
cyan: chalk.cyan,
white: chalk.white,
redBright: chalk.redBright,
greenBright: chalk.greenBright,
yellowBright: chalk.yellowBright,
blueBright: chalk.blueBright,
magentaBright: chalk.magentaBright,
cyanBright: chalk.cyanBright,
whiteBright: chalk.whiteBright
};
if(colors[color]) console.log(colors[color](text));
else console.log(text);
};
let rawData;
let data=[];
let id;
if(stats.size!=0){
rawData = fs.readFileSync("data.json");
data = JSON.parse(rawData);
id=data.length+1;
}
else id=1;
const operation = args[0]; // add, update, delete, list
if(operation=="add"){
const task=args[1];
const priority=args[2];
if(args.length!=3){
console.log("Usage: task <add> <task> <priority:high,medium,low>");
process.exit(1);
}
else{
const now = new Date();
const formatted = `${now.getDate()}/${now.getMonth() + 1}/${now.getFullYear()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`;
let data1=data;
let tasks={
id:id,
task:task,
status:"todo",
priority:priority,
createdAt:formatted,
updatedAt:null
};
data1.push(tasks);
fs.writeFileSync("data.json",JSON.stringify(data1,null,2));
console.log("Task added successfully! Task Id:",id);
id++;
}
}
else if(operation=="delete"){
const idToDelete=parseInt(args[1]);
if(args.length!=2){
console.log("Usage: task <delete> <task_id>");
process.exit(1);
}
if(data.length==0){
console.log("No tasks to be deleted!!");
process.exit(1);
}
else{
data = data.filter(task => task.id !== idToDelete);
fs.writeFileSync("data.json", JSON.stringify(data, null, 2));
console.log("Deleted task with id",idToDelete);
}
}
else if(operation=="update"){
if(args.length!=4){
console.log("Usage: task <update> <task_id> <attribute> <data>");
process.exit(1);
}
const idToUpdate=parseInt(args[1]);
const attributeToUpdate=args[2];
const updateData=args[3];
const now = new Date();
const formatted = `${now.getDate()}/${now.getMonth() + 1}/${now.getFullYear()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`;
let updated=false;
data=data.map(task => {
if(task.id==idToUpdate){
task[attributeToUpdate]=updateData;
task.updatedAt=formatted;
updated=true;
}
return task;
});
if(updated){
fs.writeFileSync("data.json", JSON.stringify(data, null, 2));
console.log("Data updated successfully for task id",idToUpdate);
}
else console.log("No such id found in the data!!");
}
else if(operation=="list"){
if(args.length!=2){
console.log("Usage: task <list> <all>|<done>|<todo>|<in-progress>");
process.exit(1);
}
const subcmd=args[1];
let listTasks=[]
if(subcmd=="all") for(let tasks of data) listTasks.push([tasks.id,tasks.task,tasks.status,tasks.priority]);
else if(subcmd=="done"){
for(let tasks of data){
if(tasks.status=="done"){
listTasks.push([tasks.id,tasks.task,tasks.status,tasks.priority]);
}
}
}
else if(subcmd=="todo"){
for(let tasks of data){
if(tasks.status=="todo"){
listTasks.push([tasks.id,tasks.task,tasks.status,tasks.priority]);
}
}
}
else if(subcmd=="in-progress"){
for(let tasks of data){
if(tasks.status=="in-progress"){
listTasks.push([tasks.id,tasks.task,tasks.status,tasks.priority]);
}
}
}
else{
console.log("Usage: task <list> <all>|<done>|<todo>|<in-progress>")
process.exit(1);
}
console.log("Found",listTasks.length,"tasks!");
for(let i of listTasks){
let color;
if(i[2]=="done") color="green";
else if(i[3]=="high") color="redBright";
else if(i[3]=="medium") color="yellow";
else if(i[3]=="low") color="cyan"
color_text(color,i);
}
}
else if(operation=="done"){
if(args.length!=2){
console.log("Usage: task <done> <task_id>");
process.exit(1);
}
else{
const task_id=parseInt(args[1]);
let updated=false;
data=data.map(task => {
if(task.id==task_id){
task.status="done";
updated=true;
}
return task;
});
if(updated){
fs.writeFileSync("data.json", JSON.stringify(data, null, 2));
console.log("Task with task id",task_id,"completed successfully!!");
}
else console.log("No such id found in the data!!");
}
}
else if(operation=="todo"){
if(args.length!=2){
console.log("Usage: task <todo> <task_id>");
process.exit(1);
}
else{
const task_id=parseInt(args[1]);
let updated=false;
data=data.map(task => {
if(task.id==task_id){
task.status="todo";
updated=true;
}
return task;
});
if(updated){
fs.writeFileSync("data.json", JSON.stringify(data, null, 2));
console.log("Status of the task with task id",task_id,"has been changed!!");
}
else console.log("No such id found in the data!!");
}
}
else{
console.log("Usage: task <add>|<update>|<delete>|<list>|<done>|<todo> <....>")
process.exit(1);
}