2019-08-11 18:15:16 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const chalk = require('chalk');
|
|
|
|
|
2019-08-11 18:56:43 +00:00
|
|
|
const getNotes = () => {
|
2019-08-11 18:15:16 +00:00
|
|
|
return 'Your notes...'
|
|
|
|
}
|
|
|
|
|
2019-08-11 18:56:43 +00:00
|
|
|
const addNote = (title, body) => {
|
2019-08-11 18:15:16 +00:00
|
|
|
const notes = loadNotes();
|
2019-08-11 18:56:43 +00:00
|
|
|
const duplicateNotes = notes.filter((note) => note.title === title);
|
2019-08-11 18:15:16 +00:00
|
|
|
|
|
|
|
if (duplicateNotes.length === 0) {
|
|
|
|
notes.push({
|
|
|
|
title: title,
|
|
|
|
body: body
|
|
|
|
});
|
|
|
|
|
|
|
|
saveNotes(notes);
|
|
|
|
console.log(chalk.bgGreen.bold('New note added!'));
|
|
|
|
} else {
|
|
|
|
console.log(chalk.bgRed.bold('Note title taken!'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 18:56:43 +00:00
|
|
|
const removeNote = (title) => {
|
2019-08-11 18:15:16 +00:00
|
|
|
const notes = loadNotes();
|
2019-08-11 18:56:43 +00:00
|
|
|
const notesToKeep = notes.filter((note) => note.title !== title);
|
2019-08-11 18:15:16 +00:00
|
|
|
|
|
|
|
if (notes.length > notesToKeep.length) {
|
|
|
|
console.log(chalk.bgGreen.bold('Note removed!'));
|
|
|
|
saveNotes(notesToKeep);
|
|
|
|
} else {
|
|
|
|
console.log(chalk.bgRed.bold('No note found!'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-11 18:56:43 +00:00
|
|
|
const saveNotes = (notes) => {
|
2019-08-11 18:15:16 +00:00
|
|
|
const dataJSON = JSON.stringify(notes);
|
|
|
|
fs.writeFileSync('notes.json', dataJSON);
|
|
|
|
}
|
|
|
|
|
2019-08-11 18:56:43 +00:00
|
|
|
const loadNotes = () => {
|
2019-08-11 18:15:16 +00:00
|
|
|
try {
|
|
|
|
const dataBuffer = fs.readFileSync('notes.json');
|
|
|
|
const dataJSON = dataBuffer.toString();
|
|
|
|
return JSON.parse(dataJSON);
|
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getNotes: getNotes,
|
|
|
|
addNote: addNote,
|
|
|
|
removeNote: removeNote
|
|
|
|
}
|