All files / kanbn/src/controller comment.js

98.07% Statements 102/104
95.83% Branches 23/24
100% Functions 4/4
98.07% Lines 102/104

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 1051x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 6x 4x 4x 2x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x   4x 4x 1x 1x 7x 7x 7x 1x 1x 1x 6x 6x 6x 7x 1x 1x 1x 5x 5x 5x 5x 7x 1x 1x 1x 4x 4x 4x 4x 4x 7x 2x 2x 4x 4x 7x 1x 7x 3x 3x 4x 4x 7x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 1x  
const kanbn = require('../main');
const utility = require('../utility');
const inquirer = require('inquirer');
const getGitUsername = require('git-user-name');
 
/**
 * Add a comment interactively
 * @param {string} text
 * @param {string} author
 * @return {Promise<any>}
 */
async function interactive(text, author) {
  return await inquirer.prompt([
    {
      type: 'input',
      name: 'text',
      message: 'Comment text:',
      default: text || '',
      validate: async value => {
        if (!value) {
          return 'Comment text cannot be empty';
        }
        return true;
      }
    },
    {
      type: 'input',
      name: 'author',
      message: 'Author:',
      default: author || ''
    }
  ]);
}
 
/**
 * Add a comment
 * @param {string} taskId
 * @param {string} text
 * @param {string} author
 */
function addComment(taskId, text, author) {
  kanbn
  .comment(taskId, text, author)
  .then(taskId => {
    console.log(`Added comment to task "${taskId}"`);
  })
  .catch(error => {
    utility.error(error);
  });
}
 
module.exports = async args => {
 
  // Make sure kanbn has been initialised
  if (!await kanbn.initialised()) {
    utility.error('Kanbn has not been initialised in this folder\nTry running: {b}kanbn init{b}');
    return;
  }
 
  // Get the task that we're add a comment to
  const taskId = args._[1];
  if (!taskId) {
    utility.error('No task id specified\nTry running {b}kanbn comment "task id"{b}');
    return;
  }
 
  // Make sure the task exists
  try {
    await kanbn.taskExists(taskId);
  } catch (error) {
    utility.error(error);
    return;
  }
 
  // Get comment values from arguments
  let commentText = '', commentAuthor = '';
 
  // Text
  if (args.text) {
    commentText = utility.strArg(args.text);
  }
 
  // Author
  if (args.author && typeof args.author === 'string') {
    commentAuthor = utility.strArg(args.author);
  } else {
    commentAuthor = getGitUsername();
  }
 
  // Add comment interactively
  if (args.interactive) {
    interactive(commentText, commentAuthor)
    .then(answers => {
      addComment(taskId, answers.text, answers.author);
    })
    .catch(error => {
      utility.error(error);
    });
 
  // Otherwise add comment non-interactively
  } else {
    addComment(taskId, commentText, commentAuthor);
  }
};