-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
241 lines (218 loc) · 5.29 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const fs = require("fs");
const readline = require("readline");
const PATH_TXT_TASK = "./text-files/task.txt";
const PATH_TXT_COMPLETED = "./text-files/completed.txt";
const spawn = require("child_process").spawn;
const sort = spawn("sort", ["-k1,1", PATH_TXT_TASK]);
const sort2 = spawn("sort", ["-k1,1", PATH_TXT_COMPLETED]);
let n = 0;
let m = 0;
let pending = [];
let completed = [];
let command = process.argv[2];
switch (command) {
case "help":
usage();
break;
case "ls":
listAllPending();
break;
case "add":
add();
break;
case "del":
del();
break;
case "done":
done();
break;
case "report":
report();
break;
default:
usage();
break;
}
function usage() {
console.log(`
Usage :-
> ./todolist add 2 "your item"
# Add a new item with priority "2" and item {"your item"} to the list.
> ./todolist ls
# Show incomplete priority list items sorted by priority in ascending order.
> ./todolist done INDEX
# Mark the incomplete item with the given index as complete.
> ./todolist del INDEX
# Delete the incomplete item with the given index.
> ./todolist help
# Show help.
>./todolist report
# Get report of complete and incomplete items.
`);
}
function add() {
let priority = process.argv[3];
let task = process.argv[4];
if (task == undefined) {
console.error("Error: Missing tasks string. Nothing added!");
return;
} else if (priority == undefined || isNaN(priority)) {
console.error("priority must be number");
return;
} else if (priority < 0) {
console.error("priority cant be less than 0");
return;
}
var logger = fs.createWriteStream(PATH_TXT_TASK, {
flags: "a",
});
logger.write("[" + priority + "]" + " " + task + "\n");
console.log('Added task: "' + task + '" with priority [' + priority + "]");
}
function listAllPending() {
let n = 0;
sort.stdout.on("data", function (data) {
const logger = fs.createWriteStream(PATH_TXT_TASK, {
flags: "w",
});
data
.toString()
.split("\n")
.forEach((record) => {
if (record.length != 0) {
n++;
logger.write(record + "\n");
console.info(
`${n}. ${record.slice(4, record.length)} ${record.slice(0, 3)}`
);
}
});
});
sort.on("exit", function (e) {
if (e) {
console.log("There are no pending tasks!");
}
});
}
function done() {
let index = process.argv[3];
let n = 0;
let taskCompleted = "a";
if (index == undefined || isNaN(index)) {
console.error("Error: Missing NUMBER for marking tasks as done.");
return;
} else if (index <= 0) {
console.error("Error: no incomplete item with index #0 exists.");
return;
}
sort.stdout.on("data", function (data) {
const logger = fs.createWriteStream(PATH_TXT_TASK, {
flags: "w",
});
data
.toString()
.split("\n")
.forEach((record) => {
if (record) {
n++;
if (n == index) {
taskCompleted = record;
} else {
logger.write(record + "\n");
}
}
});
});
sort.on("exit", function (e) {
if (e) {
console.log(e);
}
if (n < index) {
console.log("no incomplete item with INDEX [" + index + "] exists.");
} else {
fs.createWriteStream(PATH_TXT_COMPLETED, {
flags: "a",
}).write(taskCompleted + "\n");
console.log("Marked item as done.");
}
});
}
function del() {
let index = process.argv[3];
let n = 0;
if (index == undefined || isNaN(index)) {
console.error("Error: Missing INDEX for deleting tasks.");
return;
} else if (index <= 0) {
console.error(
"Error: task with INDEX [0] does not exist, Nothing deleted."
);
return;
}
sort.stdout.on("data", function (data) {
const logger = fs.createWriteStream(PATH_TXT_TASK, {
flags: "w",
});
data
.toString()
.split("\n")
.forEach((record) => {
if (record) {
n++;
if (n == index) {
} else {
logger.write(record + "\n");
}
}
});
});
sort.on("exit", function (err) {
if (err) {
console.error(err);
}
if (n < index) {
console.error(
"Error: task with INDEX [" +
index +
"] does not exist, Nothing deleted."
);
} else {
console.log("Deleted task with INDEX [" + index + "].");
}
});
}
function report() {
const file = readline.createInterface({
input: fs.createReadStream(PATH_TXT_TASK),
output: process.stdout,
terminal: false,
});
file.on("line", (record) => {
n++;
pending.push(record);
});
const file2 = readline.createInterface({
input: fs.createReadStream(PATH_TXT_COMPLETED),
output: process.stdout,
terminal: false,
});
file2.on("line", (line) => {
m++;
completed.push(line);
});
setTimeout(function () {
console.log("\nPending : " + n);
for (let i = 0; i < pending.length; i++) {
console.info(
`${i + 1}. ${pending[i].slice(4, pending[i].length)} ${pending[i].slice(
0,
3
)}`
);
}
console.log("\nCompleted : " + m);
for (let j = 0; j < completed.length; j++) {
console.info(`${j + 1}. ${completed[j].slice(4, completed[j].length)}`);
}
}, 15);
}