app.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const fs = require('fs');
  2. const boxen = require('boxen');
  3. const chalk = require('chalk');
  4. const _ = require('lodash');
  5. const yargs = require('yargs');
  6. const notes = require('./notes.js');
  7. const titleOptions = {
  8. describe: 'Title of note.',
  9. demand: true,
  10. alias: 't'
  11. };
  12. const bodyOptions = {
  13. describe: 'Body of the note.',
  14. demand: true,
  15. alias: 'b'
  16. };
  17. const argv = yargs
  18. .command('add', 'Add a new note.', {
  19. title: titleOptions,
  20. body: bodyOptions
  21. })
  22. .command('list', 'List all notes.')
  23. .command('read', 'Read a note.', { title: titleOptions })
  24. .command('remove', 'Remove a note.', { title: titleOptions })
  25. .locale('en')
  26. .help()
  27. .argv;
  28. let command = argv._[0];
  29. const msgInfo = {
  30. borderColor: 'green',
  31. borderStyle: 'round',
  32. float: 'left',
  33. align: 'center'
  34. };
  35. const msgInfoText = chalk.bold.green;
  36. const msgDangerText = chalk.bold.red;
  37. const msgDanger = {
  38. borderColor: 'red',
  39. borderStyle: 'round',
  40. float: 'left',
  41. align: 'center'
  42. };
  43. if (command === 'add') {
  44. let note = notes.addNote(argv.title, argv.body);
  45. if (note) {
  46. console.log(boxen(msgInfoText('Note created'), msgInfo));
  47. notes.printNote(note);
  48. } else {
  49. console.log(boxen(msgDangerText('Note already exist.'), msgDanger));
  50. }
  51. } else if (command === 'remove') {
  52. let removed = notes.removeNote(argv.title);
  53. let message = removed ? `Note: '${argv.title}' removed.` : `Note: '${argv.title}' not found.`;
  54. console.log(boxen(msgDangerText(message), msgDanger));
  55. } else if (command === 'list') {
  56. let noteList = notes.getAll();
  57. let message =`Printing ${noteList.length} notes:`;
  58. console.log(boxen(msgInfoText(message), msgInfo));
  59. notes.printNoteList(noteList);
  60. } else if (command === 'read') {
  61. let note = notes.getNote(argv.title);
  62. if (note !== undefined) {
  63. notes.printNote(note);
  64. } else {
  65. console.log(boxen(msgDangerText(`Note '${argv.title}' not found`), msgDanger));
  66. }
  67. } else {
  68. console.log(boxen(msgDangerText('Command not recognized'), msgDanger));
  69. }