-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (59 loc) · 1.53 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
console.log("Starting app.js.");
const fs = require('fs');
const _ = require('lodash');
const yargs = require('yargs');
const notes = require('./notes.js');
const titleHelp = {
describe: 'Title of note',
demand: true,
alias: 't'
};
const bodyHelp = {
describe: 'Body of the note',
demand: true,
alias: 'b'
}
const argv = yargs
.command('add', 'Add a new note', {
title: titleHelp,
body: bodyHelp
})
.command('list', 'List all notes')
.command('read', 'Read a note', {
title: titleHelp
})
.command('remove', 'Remove a note', {
title: titleHelp
})
.help()
.argv;
let command = argv._[0];
// console.log("command:", command);
//console.log("yargs", argv);
if(command === 'add') {
let note = notes.addNote(argv.title, argv.body);
if(note) {
console.log(`Note "${note.title}" with the body "${note.body}" is saved`);
} else {
console.log(`Note couldn't be saved!`);
}
} else if(command === 'list') {
let allNotes = notes.getAll();
console.log(`Printing ${allNotes.length} notes:`);
allNotes.forEach((note) => console.log(notes.logNote(note)));
} else if(command === 'read') {
let note = notes.readNote(argv.title);
if(note) {
console.log("Read: " + notes.logNote(note));
} else {
console.log(`Couldn't read "${argv.title}"`);
}
} else if(command === 'remove') {
if(notes.removeNote(argv.title)) {
console.log(`Title ${argv.title} was removed`)
} else {
console.log(`Title ${argv.title} was NOT removed!`);
}
} else {
console.log("Command not reconginzed");
}