-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
100 lines (74 loc) · 1.82 KB
/
Copy pathapp.js
File metadata and controls
100 lines (74 loc) · 1.82 KB
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
const fs = require('fs')
const _ = require('lodash')
const yargs = require('yargs')
const notes = require('./notes.js')
let message
// set options for title
const titleOptions = {
describe: "Title of note",
demand: true,
alias: "t"
}
// set options for body
const bodyOptions = {
describe: 'Body of note',
demand: true,
alias: 'b'
}
// create arguments
const argv = yargs
.command('add', 'Add a new note', {
title: titleOptions,
body: bodyOptions,
})
.command('list', 'List all notes')
.command('read', 'Read a note', {
title: titleOptions,
})
.command('remove', 'Remove a note', {
title: titleOptions,
})
.help()
.argv
// get argument from cli
const command = argv._[0]
switch (command) {
// add note
case 'add':
// create note
const note = notes.addNote(argv.title, argv.body)
// create message
message = note ? 'Note created...' : `The note: ${ argv.title } already exists.`
// log note and message
notes.logNote(note)
console.log(message)
break
// list all notes
case 'list':
// get and log notes
const allNotes = notes.getAll()
console.log(`Printing ${allNotes.length} note(s).`)
allNotes.forEach(note => notes.logNote(note))
break
// read one note
case 'read':
// get note
const foundNote = notes.getNote(argv.title, argv.body)
// create message
message = foundNote ? 'Note read' : 'Note not found'
// log note and message
notes.logNote(foundNote)
console.log(message)
break
// remove note
case 'remove':
// remove note
const removedNote = notes.removeNote(argv.title)
// create and log message
message = removedNote ? `Note removed` : 'Note not found'
console.log(message)
break
default:
// error message
console.log('Command not recognized')
}