1
0

Video 24 (End of section 5)

This commit is contained in:
Jason Williams 2019-08-11 13:35:09 -06:00
parent 15a11f8ac9
commit 113ef66748
2 changed files with 25 additions and 10 deletions

View File

@ -55,8 +55,15 @@ yargs.command({
yargs.command({
command: 'read',
describe: 'Read a note',
handler() {
console.log('Reading a note');
builder: {
title: {
describe: 'Note title',
demandOption: true,
type: 'string'
}
},
handler(argv) {
notes.readNote(argv.title);
}
});

View File

@ -1,15 +1,11 @@
const fs = require('fs');
const chalk = require('chalk');
const getNotes = () => {
return 'Your notes...'
}
const addNote = (title, body) => {
const notes = loadNotes();
const duplicateNotes = notes.filter((note) => note.title === title);
const duplicateNote = notes.find((note) => note.title === title);
if (duplicateNotes.length === 0) {
if (!duplicateNote) {
notes.push({
title: title,
body: body
@ -48,6 +44,18 @@ const saveNotes = (notes) => {
fs.writeFileSync('notes.json', dataJSON);
}
const readNote = (title) => {
const notes = loadNotes();
const note = notes.find((note) => note.title === title);
if (note) {
console.log(chalk.cyan.underline(note.title));
console.log(note.body);
} else {
console.log(chalk.bgRed.bold('No note found!'));
}
}
const loadNotes = () => {
try {
const dataBuffer = fs.readFileSync('notes.json');
@ -59,8 +67,8 @@ const loadNotes = () => {
}
module.exports = {
getNotes: getNotes,
addNote: addNote,
removeNote: removeNote,
listNotes: listNotes
listNotes: listNotes,
readNote: readNote
}