All files / kanbn/src/controller rename.js

96.15% Statements 100/104
92.3% Branches 24/26
100% Functions 4/4
96.15% Lines 100/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 1057x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 4x 5x 5x 5x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x   5x 7x 7x 7x 11x 11x 11x 1x 1x 1x 10x 10x 10x 11x 1x 1x 1x 9x 9x 9x 9x 11x 1x 1x 1x 8x 8x 8x 8x 8x 11x       8x 8x 8x 11x 4x 4x 8x 8x 11x 5x 5x 4x 5x 5x 1x 5x 5x 5x 11x 3x 3x 7x  
const kanbn = require('../main');
const utility = require('../utility');
const inquirer = require('inquirer');
 
/**
 * Rename a task interactively
 * @param {object} taskData
 * @return {Promise<any>}
 */
async function interactive(taskData) {
  return await inquirer.prompt([
    {
      type: 'input',
      name: 'name',
      message: 'Task name:',
      default: taskData.name || '',
      validate: async value => {
        if (!value) {
          return 'Task name cannot be empty';
        }
        return true;
      }
    }
  ]);
}
 
/**
 * Rename a task
 * @param {string} taskId
 * @param {string} newTaskName
 * @param {string} currentTaskName
 */
function renameTask(taskId, newTaskName, currentTaskName) {
 
  // Check if the new name is the same as the current name
  if (newTaskName === currentTaskName) {
    utility.error(`Task "${taskId}" already has the name "${newTaskName}"`);
    return;
  }
 
  // New name is different to current name, so rename the task
  kanbn
  .renameTask(taskId, newTaskName)
  .then(newTaskId => {
    console.log(`Renamed task "${taskId}" to "${newTaskId}"`);
  })
  .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 renaming
  const taskId = args._[1];
  if (!taskId) {
    utility.error('No task id specified\nTry running {b}kanbn rename "task id"{b}');
    return;
  }
 
  // Make sure the task exists
  try {
    await kanbn.taskExists(taskId);
  } catch (error) {
    utility.error(error);
    return;
  }
 
  // Get the current task data
  let taskData;
  try {
    taskData = await kanbn.getTask(taskId);
  } catch (error) {
    utility.error(error);
    return;
  }
  const currentTaskName = taskData.name;
 
  // Get new task name from arguments
  if (args.name) {
    taskData.name = utility.strArg(args.name);
  }
 
  // Rename task interactively
  if (args.interactive) {
    interactive(taskData)
    .then(answers => {
      renameTask(taskId, answers.name, currentTaskName);
    })
    .catch(error => {
      utility.error(error);
    });
 
  // Otherwise rename task non-interactively
  } else {
    renameTask(taskId, taskData.name, currentTaskName);
  }
};