-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (69 loc) · 2.27 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
/*
Here I initialized package.json by using node init
And then to intall 'lodash' : node install lodash --save
*/
/*
Bes practice:
Never upload node mudules folder on github,
just upload the project files,
to use modules from the node modules folder ( here lodash )
package,json will be helpful
we already initialized dependecies in package.json
So, when we download the files of the project use 'npm install' command
in project folder.
It will install necessary modules according to the dependencies in the package.json
*/
const _ = require('lodash');
const yargs = require('yargs');
const notes = require('./notes.js');
const updateJsonFile = require('update-json-file');
const filePath = './data/notesData.json';
const titleOptions = {
describe: 'Title of a note',
demand: true,
alias: 't'
};
const bodyOptions = {
describe: 'Body of a note',
demand: true,
alias: 'b'
};
var argv = yargs
.command('add', 'Add a new note', {title: titleOptions, body: bodyOptions})
.command('read', 'Read an existing note', {title: titleOptions})
.command('list', 'List out all existing notes')
.command('remove', 'Remove a note', {title:titleOptions})
.help()
.argv;
var command = argv._[0];
if (command === 'add') {
var note = notes.addNote(argv.title, argv.body);
console.log("Note Added: ");
if (note) {
notes.logData(note);
} else {
console.log("The note already exist with the title: ", argv.title);
console.log("Try using another title!");
}
} else if (command === 'list') {
var allNotes = notes.getAll();
console.log("Printing " + allNotes.length +" note(s)");
allNotes.forEach((note) => notes.logData(note));
} else if (command === 'read') {
var outputNote = notes.getNote(argv.title);
console.log("Note Found: ");
if (outputNote) {
notes.logData(outputNote);
} else {
console.log(`The note with title '${argv.title}' does not exist!`);
}
} else if (command === 'remove') {
var isRemoved = notes.removeNote(argv.title);
if (isRemoved) {
console.log("The note removed succesfully!");
} else {
console.log(`The note with Title: '${argv.title}' does not exist!`);
}
} else {
console.log("Command not recognized");
}