Working with the list command

Now, with the list command, I'll remove the console.log statement and call notes.getAll, as shown here:

console.log('Starting app.js');

const fs = require('fs');
const _ = require('lodash');
const yargs = require('yargs');

const notes = require('./notes.js');

const argv = yargs.argv;
var command = process.argv[2];
console.log('Command:', command);
console.log('Process', process.argv);
console.log('Yargs', argv);

if (command === 'add') {
notes.addNote(argv.title, argv.body);
} else if (command === 'list') {
notes.getAll();
} else if (command === 'read') {
console.log('Reading note');
} else if (command === 'remove') {
console.log('Removing note');
} else {
console.log('Command not recognized');
}

At some point, notes.getAll will return all of the notes. Now, getAll doesn't take any arguments since it will return all of the notes regardless of the title. The read command will require a title, and remove will also require the title of the note you want to remove.

For now, we can create the getAll function. Inside notes.js, we'll go through that process again. We'll start by making a variable, calling it getAll, and setting it equal to an arrow function, which we've used before. We start with our arguments list, then we set up the arrow (=>), which is the equal sign and the greater than sign. Next, we specify the statements we want to run. Inside our code block, we'll run console.log(Getting all notes), as shown here:

var getAll = () => {
console.log('Getting all notes');
};

The last step to the process after adding that semicolon will be to add getAll to the exports, as shown in the following code block:

module.exports = {
addNote,
getAll
};
Remember that in ES6, if you have a property whose name is identical to the value, which is a variable, you can simply remove the value variable and the colon.

Now that we have getAll in notes.js in place, and we've wired it up in app.js, we can run things over in Terminal. In this case, we'll run the list command:

node app.js list

In the preceding code output, you can see at the bottom that Getting all notes prints to the screen. Now that we have this in place, we can remove console.log('Process', process.argv) from the command variable in app.js. The resultant code will look like the following code block:

console.log('Starting app.js');

const fs = require('fs');
const _ = require('lodash');
const yargs = require('yargs');

const notes = require('./notes.js');

const argv = yargs.argv;
var command = process.argv[2];
console.log('Command:', command);
console.log('Yargs', argv);

if (command === 'add') {
notes.addNote(argv.title, argv.body);
} else if (command === 'list') {
notes.getAll();
} else if (command === 'read') {
console.log('Reading note');
} else if (command === 'remove') {
console.log('Removing note');
} else {
console.log('Command not recognized');
}

We will keep the yargs log around since we'll be exploring the other ways and methods to use yargs throughout the chapter.

Now that we have the list command in place, next, I'd like you to create a method for the read and remove commands.