-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (112 loc) · 2.96 KB
/
app.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
const fs = require('fs');
const yargs = require('yargs');
const chalk = require('chalk');
let command = yargs.argv._[0];
let parameters = yargs.argv;
let notesList = {
title: parameters.title,
body: parameters.body
};
let obj = [];
let flag = 0;
switch(command) {
case 'add':
fs.readFile('./notes.json', 'utf8', function(err, data) {
if(err) throw err;
if(data) {
obj = JSON.parse(data);
}
else {
obj = [];
}
for (let i = 0; i < obj.length; i++) {
if(obj[i].title === notesList.title) {
flag = 1;
console.log(chalk.bgRed('!!!Title Already Taken!!!'));
}
}
if(!flag) {
obj.push(notesList);
console.log(chalk.bgGreenBright.black('New Note Created!'));
}
fs.writeFileSync('./notes.json', JSON.stringify(obj));
});
break;
case 'list':
fs.readFile('./notes.json', 'utf8', function(err, data) {
if(err) throw err;
if(data) {
obj = JSON.parse(data);
}
else {
obj = [];
}
if(obj.length === 0) {
console.log(chalk.bgRed('!!!Nothing To Show!!!'));
} else {
console.log(chalk.bgYellowBright.black.bold('YOUR NOTES: '));
for (let i = 0; i < obj.length; i++) {
console.log(chalk.greenBright(obj[i].title));
}
}
});
break;
case 'read':
fs.readFile('./notes.json', 'utf8', function(err, data) {
if(err) throw err;
if(data) {
obj = JSON.parse(data);
}
else {
obj = [];
console.log(chalk.bgRed('!!!Nothing To Show!!!'));
}
for (let i = 0; i < obj.length; i++) {
if(obj[i].title === parameters.title) {
flag = 0;
console.log(chalk.bgYellowBright.black.bold(obj[i].title + ":"));
console.log(chalk.greenBright(obj[i].body));
break;
}
else {
flag = 1;
}
}
if(flag || obj.length === 0) {
console.log(chalk.bgRed('!!!Note Not Found!!!'));
}
});
break;
case 'remove':
fs.readFile('./notes.json', 'utf8', function(err, data) {
if(err) throw err;
if(data) {
obj = JSON.parse(data);
}
else {
obj = [];
}
let index = null;
for (let i = 0; i < obj.length; i++) {
if(obj[i].title === parameters.title) {
flag = 1;
index = i;
break;
}
else {
flag = 0;
}
}
if(flag) {
obj.splice(index, 1);
console.log(chalk.bgGreenBright.black('Note Removed!'));
fs.writeFileSync('./notes.json', JSON.stringify(obj));
} else {
console.log(chalk.bgRed('!!!Note Not Found!!!'));
}
});
break;
default:
console.log(chalk.bgRed('!!!Enter Valid Command!!!'));
break;
}