-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.js
219 lines (214 loc) · 6.58 KB
/
task.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const fs = require("fs");
const args = process.argv.slice(2);
var path=args[0];
// console.log(path);
var exists = fs.existsSync(`${path}/task.txt`);
if (!exists) {
fs.writeFileSync(`${path}/task.txt`, "");
}
var exists = fs.existsSync(`${path}/completed.txt`);
if (!exists) {
fs.writeFileSync(`${path}/completed.txt`, "");
}
const proc = args[1];
var tasks = [];
function displayableString(tasks) {
var list = tasks.map((task) => {
return `${task[0]} ${task[1]}`;
});
return list;
}
switch (proc) {
case "add":
if (args[2] === undefined || args[3] === undefined) {
console.log("Error: Missing tasks string. Nothing added!");
break;
}
if(parseInt(args[2])<0){
console.log("Error: Priority should be an integer greater than or equal to 0");
break;
}
var data = fs.readFileSync(`${path}/task.txt`, "utf8", {
encoding: "utf8",
flag: "r",
});
var list = data.split("\n");
list.pop();
var tasks = [];
list.forEach((task) => {
const priority = parseInt(task.substring(0, task.indexOf(" ")));
const title = task.substring(task.indexOf(" ") + 1);
tasks.push([priority, title]);
});
tasks.push([args[2], args[3]]);
tasks.sort((a, b) => a[0] - b[0]);
var list = displayableString(tasks);
fs.writeFileSync(`${path}/task.txt`, "");
list.forEach(async (task) => {
fs.appendFileSync(`${path}/task.txt`, task, (err) => {
if (err) throw err;
});
fs.appendFileSync(`${path}/task.txt`, "\n", (err) => {
if (err) throw err;
});
});
console.log(`Added task: "${args[3]}" with priority ${args[2]}`);
break;
case "ls":
var data = fs.readFileSync(`${path}/task.txt`, "utf8", {
encoding: "utf8",
flag: "r",
});
if (data.length === 0) {
console.log("There are no pending tasks!");
break;
}
var list = data.split("\n");
list.pop();
var tasks = [];
list.forEach((task, idx) => {
const priority = parseInt(task.substring(0, task.indexOf(" ")));
const title = task.substring(task.indexOf(" ") + 1);
tasks.push([priority, title]);
});
tasks.forEach((task, idx) => {
console.log(`${idx + 1}. ${task[1]} [${task[0]}]`);
});
break;
case "del":
var idx = args[2];
if (idx === undefined) {
console.log("Error: Missing NUMBER for deleting tasks.");
break;
}
var data = fs.readFileSync(`${path}/task.txt`, "utf8", {
encoding: "utf8",
flag: "r",
});
var list = data.split("\n");
list.pop();
if (idx > list.length || idx == 0) {
console.log(
`Error: task with index #${idx} does not exist. Nothing deleted.`,
);
break;
} else {
list.splice(idx - 1, 1);
fs.writeFileSync(`${path}/task.txt`, "");
list.forEach(async (task) => {
fs.appendFileSync(`${path}/task.txt`, task, (err) => {
if (err) throw err;
});
fs.appendFileSync(`${path}/task.txt`, "\n", (err) => {
if (err) throw err;
});
});
console.log(`Deleted task #${idx}`);
}
break;
case "done":
var idx = args[2];
if (idx === undefined) {
console.log("Error: Missing NUMBER for marking tasks as done.");
break;
}
var data = fs.readFileSync(`${path}/task.txt`, "utf8", {
encoding: "utf8",
flag: "r",
});
var list = data.split("\n");
list.pop();
var tasks = [];
list.forEach((task, idx) => {
const priority = parseInt(task.substring(0, task.indexOf(" ")));
const title = task.substring(task.indexOf(" ") + 1);
tasks.push([priority, title]);
});
if (idx > list.length || idx == 0) {
console.log(`Error: no incomplete item with index #${idx} exists.`);
break;
} else {
var temp = tasks[idx - 1];
list.splice(idx - 1, 1);
fs.writeFileSync(`${path}/task.txt`, "");
list.forEach(async (task) => {
fs.appendFileSync(`${path}/task.txt`, task, (err) => {
if (err) throw err;
});
fs.appendFileSync(`${path}/task.txt`, "\n", (err) => {
if (err) throw err;
});
});
fs.appendFileSync(`${path}/completed.txt`, temp[1], (err) => {
if (err) throw err;
});
fs.appendFileSync(`${path}/completed.txt`, "\n", (err) => {
if (err) throw err;
});
console.log(`Marked item as done.`);
}
break;
case "help":
console.log("Usage :-");
console.log(
'$ ./task add 2 hello world # Add a new item with priority 2 and text "hello world" to the list',
);
console.log(
"$ ./task ls # Show incomplete priority list items sorted by priority in ascending order",
);
console.log(
"$ ./task del INDEX # Delete the incomplete item with the given index",
);
console.log(
"$ ./task done INDEX # Mark the incomplete item with the given index as complete",
);
console.log("$ ./task help # Show usage");
console.log("$ ./task report # Statistics");
break;
case "report":
var data = fs.readFileSync(`${path}/task.txt`, "utf8", {
encoding: "utf8",
flag: "r",
});
var list = data.split("\n");
list.pop();
var tasks = [];
list.forEach((task, idx) => {
const priority = parseInt(task.substring(0, task.indexOf(" ")));
const title = task.substring(task.indexOf(" ") + 1);
tasks.push([priority, title]);
});
console.log(`Pending : ${tasks.length}`);
tasks.forEach((task, idx) => {
console.log(`${idx + 1}. ${task[1]} [${task[0]}]`);
});
var data = fs.readFileSync(`${path}/completed.txt`, "utf8", {
encoding: "utf8",
flag: "r",
});
var list = data.split("\n");
list.pop();
console.log("");
console.log(`Completed : ${list.length}`);
list.forEach((task, idx) => {
console.log(`${idx + 1}. ${task}`);
});
break;
default:
console.log("Usage :-");
console.log(
'$ ./task add 2 hello world # Add a new item with priority 2 and text "hello world" to the list',
);
console.log(
"$ ./task ls # Show incomplete priority list items sorted by priority in ascending order",
);
console.log(
"$ ./task del INDEX # Delete the incomplete item with the given index",
);
console.log(
"$ ./task done INDEX # Mark the incomplete item with the given index as complete",
);
console.log("$ ./task help # Show usage");
console.log("$ ./task report # Statistics");
break;
}