-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
80 lines (68 loc) · 1.73 KB
/
app.js
File metadata and controls
80 lines (68 loc) · 1.73 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
const fs = require("fs");
const _ = require("lodash");
const yargs = require("yargs");
const notes = require("./notes");
const yargsOptions = {
title: {
describe: "Title of note",
demand: true,
alias: "t"
},
body: {
describe: "Body of note",
demand: true,
alias: "b"
}
};
//fetch yargs parsed argvs
const argv = yargs
.command("add", "Add a new note", {
title: yargsOptions.title,
body: yargsOptions.body
})
.command("remove", "Remove a note", {
title: yargsOptions.title
})
.command("read", "read a note", {
title: yargsOptions.title
})
.command("list", "List all notes", {})
.help().argv;
//fetch process from yargs _ property
const command = argv._[0];
console.log(`Process: ${process.argv}`);
console.log(`yargs: `, argv);
switch (true) {
case command === "add":
const noteAdded = notes.addNote(argv.title, argv.body);
if (noteAdded) {
console.log("Note Created");
notes.logNotes(noteAdded);
} else {
console.log("Duplicate found");
}
break;
case command === "list":
const allNotes = notes.getAll();
console.log(`Printing ${notes.length} note(s).`);
for (note in allNotes) {
notes.logNotes(allNotes[note]);
}
break;
case command === "remove":
const noteRemoved = notes.removeNote(argv.title);
const message = noteRemoved ? "Note removed" : "Note not removed";
console.log(message);
break;
case command === "read":
const noteSelected = notes.readNote(argv.title);
if (noteSelected) {
console.log("Note Selected");
notes.logNotes(noteSelected);
} else {
console.log(`${argv.title} does not exist`);
}
break;
default:
console.log("command not recognized");
}