-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote.js
106 lines (87 loc) · 3.21 KB
/
note.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
// Requiring fs module
const fs = require("fs");
// Storing the JSON format data in myObject
var data = fs.readFileSync("data.json");
var myObject = JSON.parse(data);
var args = process.argv.slice(2);
var ERROR = `
Invalid Syntax!!!
+----------------------------------------------------------------+
| How to use? - |
| |
| ADD Note: |
| node note.js add --title="TITLE" --body="BODY" |
| |
| DELETE Note: |
| node note.js remove --title="TITLE" |
| |
| LIST Notes: |
| node note.js list |
| |
| READ Notes: |
| node note.js read --title="TITLE" |
| |
+----------------------------------------------------------------+
`
//===============================================================================================================================================================
try{
if (args[0] === "add") {
// User Input
var title = args[1];
var body = args[2];
var titleArr = title.split("=");
var bodyArr = body.split("=");
let newData = {
title: titleArr[1],
body: bodyArr[1],
};
// Adding the new data to our object
myObject.push(newData);
// Writing to our JSON file
var newData2 = JSON.stringify(myObject, null, 2);
fs.writeFile("data.json", newData2, (err) => {
// Error checking
if (err) throw err;
console.log("New Note Created!");
});
} else if (args[0] === "remove") {
var title = args[1];
var titleArr = title.split("=");
fs.readFile("data.json", (err, data) => {
if (err) throw err;
obj = JSON.parse(data);
var filtered = obj.filter((item) => {
return item.title !== titleArr[1];
});
console.log(filtered);
var newData2 = JSON.stringify(filtered);
fs.writeFile("data.json", newData2, (err) => {
// Error checking
if (err) throw err;
console.log("Note removed!");
});
});
} else if (args[0] === "list") {
fs.readFile("data.json", (err, data) => {
if (err) throw err;
obj = JSON.parse(data);
console.log("Your Notes:");
for (let i = 0; i < obj.length; i++) {
console.log(obj[i].title);
}
})
} else if (args[0] === "read") {
var title = args[1];
var titleArr = title.split("=");
fs.readFile("data.json", (err, data) => {
if (err) throw err;
obj = JSON.parse(data);
var result = obj.filter(function(e){return e.title == titleArr[1]})
console.log(result[0].body);
})
} else {
console.log(ERROR);
}
}catch(err){
console.log(ERROR)
}