-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (32 loc) · 1.08 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
const { log } = require("console");
const fs = require("fs")
console.log(process.argv);
let [,,command]= process.argv;
if (command == "create") {
let [, , , title] = process.argv;
let todo = JSON.parse(fs.readFileSync("./todo.json", "utf8"));
todo.push({ title: title });
fs.writeFileSync("./todo.json", JSON.stringify(todo));
} else if (command == "list") {
console.log(JSON.parse(fs.readFileSync("./todo.json", "utf8")));
}
else if(command == "update"){
let todo = JSON.parse(fs.readFileSync("./todo.json", "utf8"));
let [, , , search] = process.argv;
for(let i =0; i<todo.length; i++){
if(todo[i].title == search){
todo[i].title= process.argv[4];
}
}
fs.writeFileSync("./todo.json", JSON.stringify(todo));
} else if(command=="delete"){
let todo = JSON.parse(fs.readFileSync("./todo.json", "utf8"));
let [, , , search] = process.argv;
let new_todo=[];
for(let i =0; i<todo.length; i++){
if(!(todo[i].title == search)){
new_todo.push(todo[i]);
}
}
fs.writeFileSync("./todo.json", JSON.stringify(new_todo));
}