All files / kanbn/src/controller remove.js

93.24% Statements 69/74
94.44% Branches 17/18
100% Functions 2/2
93.24% Lines 69/74

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 751x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x   3x 3x 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       4x 4x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x   2x 2x 1x  
const kanbn = require('../main');
const utility = require('../utility');
const inquirer = require('inquirer');
 
/**
 * Remove a task
 * @param {string} taskId
 * @param {boolean} removeFile
 */
function removeTask(taskId, removeFile) {
  kanbn
  .deleteTask(taskId, removeFile)
  .then(taskId => {
    console.log(`Removed task "${taskId}"${removeFile ? ' from the index' : ' file and index entry'}`);
  })
  .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 removing
  const taskId = args._[1];
  if (!taskId) {
    utility.error('No task id specified\nTry running {b}kanbn remove "task id"{b}');
    return;
  }
 
  // Make sure the task exists
  try {
    await kanbn.taskExists(taskId);
  } catch (error) {
    utility.error(error);
    return;
  }
 
  // Get the index
  let index;
  try {
    index = await kanbn.getIndex();
  } catch (error) {
    utility.error(error);
    return;
  }
 
  // If the force flag is specified, remove the task without asking
  if (args.force) {
    removeTask(taskId, args.index);
 
  // Otherwise, prompt for confirmation first
  } else {
    inquirer.prompt([
      {
        type: 'confirm',
        message: 'Are you sure you want to remove this task?',
        name: 'sure',
        default: false
      }
    ]).then(async answers => {
      if (answers.sure) {
        removeTask(taskId, args.index);
      }
    }).catch(error => {
      utility.error(error);
    })
  }
};