123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- const fs = require('fs');
- const boxen = require('boxen');
- const chalk = require('chalk');
- const _ = require('lodash');
- const yargs = require('yargs');
- const notes = require('./notes.js');
- const titleOptions = {
- describe: 'Title of note.',
- demand: true,
- alias: 't'
- };
- const bodyOptions = {
- describe: 'Body of the note.',
- demand: true,
- alias: 'b'
- };
- const argv = yargs
- .command('add', 'Add a new note.', {
- title: titleOptions,
- body: bodyOptions
- })
- .command('list', 'List all notes.')
- .command('read', 'Read a note.', { title: titleOptions })
- .command('remove', 'Remove a note.', { title: titleOptions })
- .locale('en')
- .help()
- .argv;
- let command = argv._[0];
- const msgInfo = {
- borderColor: 'green',
- borderStyle: 'round',
- float: 'left',
- align: 'center'
- };
- const msgInfoText = chalk.bold.green;
- const msgDangerText = chalk.bold.red;
- const msgDanger = {
- borderColor: 'red',
- borderStyle: 'round',
- float: 'left',
- align: 'center'
- };
- if (command === 'add') {
- let note = notes.addNote(argv.title, argv.body);
- if (note) {
- console.log(boxen(msgInfoText('Note created'), msgInfo));
- notes.printNote(note);
- } else {
- console.log(boxen(msgDangerText('Note already exist.'), msgDanger));
- }
- } else if (command === 'remove') {
- let removed = notes.removeNote(argv.title);
- let message = removed ? `Note: '${argv.title}' removed.` : `Note: '${argv.title}' not found.`;
- console.log(boxen(msgDangerText(message), msgDanger));
- } else if (command === 'list') {
- let noteList = notes.getAll();
- let message =`Printing ${noteList.length} notes:`;
- console.log(boxen(msgInfoText(message), msgInfo));
- notes.printNoteList(noteList);
- } else if (command === 'read') {
- let note = notes.getNote(argv.title);
- if (note !== undefined) {
- notes.printNote(note);
- } else {
- console.log(boxen(msgDangerText(`Note '${argv.title}' not found`), msgDanger));
- }
- } else {
- console.log(boxen(msgDangerText('Command not recognized'), msgDanger));
- }
|