From 113ef66748e7d8c9aa852dad2add917816cc84cc Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Sun, 11 Aug 2019 13:35:09 -0600 Subject: [PATCH] Video 24 (End of section 5) --- notes-app/app.js | 11 +++++++++-- notes-app/notes.js | 24 ++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/notes-app/app.js b/notes-app/app.js index df0b14f..28a1c62 100644 --- a/notes-app/app.js +++ b/notes-app/app.js @@ -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); } }); diff --git a/notes-app/notes.js b/notes-app/notes.js index fec9469..107a872 100644 --- a/notes-app/notes.js +++ b/notes-app/notes.js @@ -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 }