-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
80 lines (76 loc) · 2.3 KB
/
Copy pathapp.js
File metadata and controls
80 lines (76 loc) · 2.3 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
// console.log("Starting app.js...");
const fs = require('fs');//Loading the File system module
const os = require('os');//Loading the operating system module
const _ = require('lodash');//_ is the common name for Lodash Utility Library
const yargs = require('yargs');
const notes = require('./notes.js');//Loading own files
var titleOptions = {
describe: 'Title of the note',
demand: true,//To make the argument required
alias: 't'//Let you provide a shortcut
}
var bodyOptions = {
describe: 'Body of the note',
demand: true,
alias: 'b'
}
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;
var command = process.argv[2];//For yargs use argv._[0]
if(command === 'add'){
// console.log('Adding new note...');
var note = notes.addNewNote(argv.title, argv.body);
// console.log(note);
if(note){
console.log(`Note '${argv.title}' created successfully!`);
notes.logNote(note);
}
else{
console.log(`Duplicate entry! Note with title '${argv.title}' already exist!`);
}
}
else if(command === 'list'){
// console.log('Listing all notes...');
var allNotes = notes.getAllNotes();
var noteText = (allNotes.length > 1) ? 'notes' : 'note';
console.log(`Printing ${allNotes.length} ${noteText}`);
if(allNotes.length === 0){
console.log('Note is empty!');
}
else{
allNotes.forEach((note) => notes.logNote(note));
console.log('Done!');
}
}
else if(command === 'read'){
// console.log('Reading note...');
var note = notes.getNote(argv.title);
if(note){
console.log(`Reading Note '${argv.title}'...`);
notes.logNote(note);
}
else{
console.log(`Note does not exist!`);
}
}
else if(command === 'remove'){
// console.log('Removing note...');
var noteRemoved = notes.removeNote(argv.title);
var message = noteRemoved ? `Note '${argv.title}' deleted successfully!` : `Note '${argv.title}' not found!`;
console.log(message);
}
else{
console.log('\'' + process.argv[2] + '\' is not recognized');
}