1
0
Fork 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({ yargs.command({
command: 'read', command: 'read',
describe: 'Read a note', describe: 'Read a note',
handler() { builder: {
console.log('Reading a note'); 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 fs = require('fs');
const chalk = require('chalk'); const chalk = require('chalk');
const getNotes = () => {
return 'Your notes...'
}
const addNote = (title, body) => { const addNote = (title, body) => {
const notes = loadNotes(); 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({ notes.push({
title: title, title: title,
body: body body: body
@ -48,6 +44,18 @@ const saveNotes = (notes) => {
fs.writeFileSync('notes.json', dataJSON); 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 = () => { const loadNotes = () => {
try { try {
const dataBuffer = fs.readFileSync('notes.json'); const dataBuffer = fs.readFileSync('notes.json');
@ -59,8 +67,8 @@ const loadNotes = () => {
} }
module.exports = { module.exports = {
getNotes: getNotes,
addNote: addNote, addNote: addNote,
removeNote: removeNote, removeNote: removeNote,
listNotes: listNotes listNotes: listNotes,
readNote: readNote
} }