node App.js add --title="List Item" --body="Sweater, pants, Jeans"
node App.js remove --title="List Item"
node App.js list
Run Command
node inspect App.js add --title="Clothes" --body="Sweater, pants"
hit in chrome
chrome://inspect
| const yargs = require('yargs') | |
| const notes = require('./notes.js') | |
| // Customize yargs version | |
| yargs.version('1.1.0') | |
| // Create add command | |
| yargs.command({ | |
| command: 'add', | |
| describe: 'Add a new note', | |
| builder: { | |
| title: { | |
| describe: 'Note title', | |
| demandOption: true, | |
| type: 'string' | |
| }, | |
| body: { | |
| describe: 'Note body', | |
| demandOption: true, | |
| type: 'string' | |
| } | |
| }, | |
| handler: function (argv) { | |
| notes.addNote(argv.title, argv.body) | |
| } | |
| }) | |
| // Create remove command | |
| yargs.command({ | |
| command: 'remove', | |
| describe: 'Remove a note', | |
| builder: { | |
| title: { | |
| describe: 'Note title', | |
| demandOption: true, | |
| type: 'string' | |
| } | |
| }, | |
| handler: function (argv) { | |
| notes.removeNote(argv.title) | |
| } | |
| }) | |
| // Create list command | |
| yargs.command({ | |
| command: 'list', | |
| describe: 'List your notes', | |
| handler: function () { | |
| notes.listNotes() | |
| } | |
| }) | |
| // Create read command | |
| yargs.command({ | |
| command: 'read', | |
| describe: 'Read a note', | |
| builder: { | |
| title: { | |
| describe: 'Note title', | |
| demandOption: true, | |
| type: 'string' | |
| } | |
| }, | |
| handler(argv) { | |
| notes.readNotes(argv.title) | |
| } | |
| }) | |
| yargs.parse() | |
| const fs = require('fs'); | |
| const chalk = require("chalk"); | |
| const addNote = function (title, body) { | |
| const notes = loadNotes() | |
| const duplicateNotes = notes.filter(function (note) { | |
| return note.title === title | |
| }) | |
| if (duplicateNotes.length === 0) { | |
| notes.push({ | |
| title: title, | |
| body: body | |
| }) | |
| saveNotes(notes) | |
| console.log('New note added!') | |
| } else { | |
| console.log('Note title taken!') | |
| } | |
| } | |
| const removeNote = function(title) { | |
| const notes = loadNotes() | |
| const notesToKeep = notes.filter(function(note) { | |
| return note.title !== title | |
| }) | |
| if(notes.length > notesToKeep.length) { | |
| console.log(chalk.green.inverse("Not Removed")) | |
| saveNotes(notesToKeep) | |
| } | |
| else { | |
| console.log(chalk.red.inverse("No note Found")) | |
| } | |
| } | |
| const listNotes = () => { | |
| const notes = loadNotes() | |
| console.log(chalk.inverse("Your Notes..")) | |
| notes.forEach((note) => { | |
| console.log(note.title) | |
| }); | |
| } | |
| const readNotes = (title) => { | |
| const notes = loadNotes() | |
| const note = notes.find((nots) => nots.title === title) | |
| if(note) { | |
| console.log(chalk.inverse(note.title)) | |
| console.log(note.body) | |
| } | |
| else{ | |
| console.log(chalk.red.inverse("Note not found..")) | |
| } | |
| } | |
| const saveNotes = function (notes) { | |
| const dataJSON = JSON.stringify(notes) | |
| fs.writeFileSync('notes.json', dataJSON) | |
| } | |
| const loadNotes = function () { | |
| try { | |
| const dataBuffer = fs.readFileSync('notes.json') | |
| const dataJSON = dataBuffer.toString() | |
| return JSON.parse(dataJSON) | |
| } catch (e) { | |
| return [] | |
| } | |
| } | |
| module.exports = { | |
| addNote: addNote, | |
| removeNote: removeNote, | |
| listNotes: listNotes, | |
| readNotes: readNotes | |
| } |
| [ | |
| { "title": "List Item", "body": "Sweater, pants, Jeans" }, | |
| { "title": "List", "body": "Sweater, pants" }, | |
| { "title": "List Item Found", "body": "Sweater, pants, Jeans" } | |
| ] |
| { | |
| "name": "abc", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "chalks": "^0.1.5", | |
| "yargs": "^15.3.1" | |
| } | |
| } |