-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60c96e7
commit 3e62d65
Showing
5 changed files
with
2,822 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const fs=require('fs'); | ||
const _ = require('lodash'); | ||
const yargs = require('yargs'); | ||
|
||
const notes = require('./notes.js'); | ||
|
||
var argv = yargs.argv; | ||
var command = argv._[0]; | ||
|
||
|
||
// console.log('Process', process.argv); | ||
// console.log('Yargs', argv); | ||
|
||
if(command == 'add'){ | ||
var note = notes.addNote(argv.title, argv.body); | ||
if(note){ | ||
console.log('Note Created'); | ||
notes.logNote(note); | ||
}else{ | ||
console.log("Note title taken"); | ||
} | ||
}else if(command === 'list'){ | ||
var allNotes = notes.getAll(); | ||
console.log(`Printing ${allNotes.length} note(s)`); | ||
allNotes.forEach(note => notes.logNote(note)); | ||
}else if(command === 'read'){ | ||
var note = notes.getNote(argv.title); | ||
if(note){ | ||
console.log('Note Found'); | ||
notes.logNote(note); | ||
}else{ | ||
console.log("Note Not Found"); | ||
} | ||
}else if(command === 'remove'){ | ||
var removed = notes.removeNote(argv.title); | ||
if(removed){ | ||
console.log('Removed Note'); | ||
console.log('--'); | ||
console.log(`Title: ${argv.title}`); | ||
console.log(`Body: ${argv.body}`); | ||
}else{ | ||
console.log('Note Not Found'); | ||
} | ||
|
||
}else{ | ||
console.log('Command not recognised'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"title":"Secret1","body":"Note 1"},{"title":"Secret2","body":"Note 2"},{"title":"Secret3","body":"Note 2"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const fs = require('fs'); | ||
|
||
var fetchNotes = () => { | ||
try{ | ||
var notesString = fs.readFileSync('notes-data.json'); | ||
return JSON.parse(notesString); | ||
}catch(e){ | ||
return []; | ||
} | ||
} | ||
|
||
var saveNote = (notes) => { | ||
fs.writeFileSync('notes-data.json',JSON.stringify(notes)); | ||
} | ||
|
||
var addNote = (title, body) => { | ||
notes = []; | ||
note = { | ||
title, | ||
body | ||
} | ||
|
||
notes = fetchNotes(); | ||
|
||
var duplicateNotes = notes.filter((note) => note.title === title); | ||
|
||
if(duplicateNotes.length === 0){ | ||
notes.push(note); | ||
saveNote(notes); | ||
return note; | ||
} | ||
} | ||
|
||
var getAll = () => { | ||
return fetchNotes(); | ||
} | ||
|
||
var getNote = (title) =>{ | ||
var notes = fetchNotes(); | ||
|
||
var filteredNotes = notes.filter((note) => note.title === title); | ||
|
||
return filteredNotes[0]; | ||
} | ||
|
||
var removeNote = (title) => { | ||
var notes = fetchNotes(); | ||
|
||
var filteredNotes = notes.filter((note) => note.title !== title); | ||
|
||
saveNote(filteredNotes); | ||
|
||
return notes.length !== filteredNotes.length; | ||
|
||
} | ||
|
||
var logNote = (note) => { | ||
debugger; | ||
console.log('--'); | ||
console.log(`Title: ${note.title}`); | ||
console.log(`Body: ${note.body}`); | ||
} | ||
|
||
module.exports = { | ||
addNote, | ||
getAll, | ||
getNote, | ||
removeNote, | ||
logNote | ||
} |
Oops, something went wrong.