-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
180 lines (149 loc) · 4.1 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
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
'use strict';
// package references
const yargs = require('yargs');
// project references
const NoteService = require('./NoteService');
const noteService = new NoteService();
// build commands
const titleOptions = (demand) => {
return {
'alias': 't',
'describe': 'A value that describes a note',
'type': 'string',
'demand': demand
};
};
const idOptions = (demand) => {
return {
'describe': 'A value that uniquely identifies a note',
'type': 'string',
'demand': demand
};
};
const contentOptions = {
'alias': 'c',
'describe': 'The body or content of the note',
'type': 'string',
'demand': true
};
const tagOptions = (demand) => {
return {
'describe': 'Tags that can be used to add metadata to a note',
'type': 'string',
'demand': demand
};
};
const commands = yargs
.command('add', 'Add new note', {
title: titleOptions(true),
content: contentOptions,
tags: tagOptions(false)
})
.command('find', 'Find note', {
title: titleOptions(false),
id: idOptions(false),
tag: tagOptions(false)
})
.command('list', 'List all notes')
.command('remove', 'Remove note', {
id: idOptions(true)
})
.command('tag', 'Tag an existing note', {
id: idOptions(true),
tags: tagOptions(true)
})
.command('update', 'Update existing note', {
id: idOptions(true),
title: titleOptions(true),
content: contentOptions,
tags: tagOptions(false)
})
.help()
.argv;
// process commands
const command = commands._[0];
switch (command) {
case 'add':
{
noteService
.addNote({
title: commands.title,
content: commands.content,
tags: commands.tags
})
.then(id => {
if (id) {
console.log(`Inserted note : ${id}`);
}
})
.catch(error => console.log(error.message));
break;
}
case 'find':
{
if (commands.id) {
noteService
.findNoteById(commands.id)
.then(note => console.log(JSON.stringify(note, null, 2)))
.catch(error => console.log(error.message));
} else if (commands.title) {
noteService
.findNotesByTitle(commands.title)
.then(notes => console.log(JSON.stringify(notes, null, 2)))
.catch(error => console.log(error.message));
} else if (commands.tag) {
noteService
.findNotesByTag(commands.tag)
.then(notes => console.log(JSON.stringify(notes, null, 2)))
.catch(error => console.log(error.message));
}
break;
}
case 'list':
{
noteService
.listNotes()
.then(notes => {
if (notes) {
console.log(JSON.stringify(notes, null, 2));
} else {
console.log('There are currently no notes to display');
}
})
.catch(error => console.log(error));
break;
}
case 'remove':
{
noteService
.removeNote(commands.id)
.then(() => console.log('Note Removed'))
.catch(error => console.log(error));
break;
}
case 'tag':
{
noteService
.tagNote(commands.id, commands.tags)
.then(() => console.log('Tagged ...'))
.catch(error => console.log(error));
break;
}
case 'update':
{
noteService
.updateNote({
id: commands.id,
title: commands.title,
content: commands.content,
tags: commands.tags
})
.then(() => console.log('Updated ...'))
.catch(error => console.log(error));
break;
}
default:
{
console.log(`The command '${command}' is not supported`);
}
}