All files / kanbn/src/controller move.js

96.5% Statements 138/143
94.28% Branches 33/35
100% Functions 6/6
96.5% Lines 138/143

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 1447x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x 7x 7x 7x 7x 7x 7x 9x 9x 9x 9x 9x 9x 9x   9x 9x 7x 7x 15x 15x 15x 1x 1x 1x 14x 14x 14x 15x 1x 1x 1x 13x 13x 13x 13x 15x 1x 1x 1x 12x 12x 12x 12x 12x 15x       12x 15x 1x 1x 1x 11x 11x 11x 11x 15x 4x 4x 1x 1x 1x 4x 10x 10x 10x 15x 15x 5x 5x 1x 1x 1x 5x 5x 5x 9x 9x 15x 15x 15x 15x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 15x 4x 4x 7x  
const kanbn = require('../main');
const utility = require('../utility');
const inquirer = require('inquirer');
 
inquirer.registerPrompt('selectLine', require('inquirer-select-line'));
 
/**
 * Move a task interactively
 * @param {object} columns
 * @param {string} columnName
 * @param {string[]} columnNames
 * @param {string[]} sortedColumnNames
 * @param {string} taskId
 * @param {number} position
 * @return {Promise<any>}
 */
async function interactive(columns, columnName, columnNames, sortedColumnNames, taskId, position) {
  return await inquirer.prompt([
    {
      type: 'list',
      name: 'column',
      message: 'Column:',
      default: columnName,
      choices: columnNames
    },
    {
      type: 'selectLine',
      name: 'position',
      message: 'Move task:',
      default: answers => Math.max(Math.min(position, columns[answers.column].length), 0),
      choices: answers => columns[answers.column].filter(t => t !== taskId),
      placeholder: taskId,
      when: answers => sortedColumnNames.indexOf(answers.column) === -1
    }
  ]);
}
 
/**
 * Move a task
 * @param {string} taskId
 * @param {string} columnName
 * @param {?number} [position=null]
 * @param {boolean} [relative=false]
 */
function moveTask(taskId, columnName, position = null, relative = false) {
  kanbn
  .moveTask(taskId, columnName, position, relative)
  .then(taskId => {
    console.log(`Moved task "${taskId}" to column "${columnName}"`);
  })
  .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 moving
  const taskId = args._[1];
  if (!taskId) {
    utility.error('No task id specified\nTry running {b}kanbn move "task id"{b}');
    return;
  }
 
  // Make sure the task exists
  try {
    await kanbn.taskExists(taskId);
  } catch (error) {
    utility.error(error);
    return;
  }
 
  // Get the index and make sure it has some columns
  let index;
  try {
    index = await kanbn.getIndex();
  } catch (error) {
    utility.error(error);
    return;
  }
  const columnNames = Object.keys(index.columns);
  if (!columnNames.length) {
    utility.error('No columns defined in the index\nTry running {b}kanbn init -c "column name"{b}');
    return;
  }
 
  // Get column name if specified
  const currentColumnName = await kanbn.findTaskColumn(taskId);
  let columnName = currentColumnName;
  if (args.column) {
    columnName = utility.strArg(args.column);
    if (columnNames.indexOf(columnName) === -1) {
      utility.error(`Column "${columnName}" doesn't exist`);
      return;
    }
  }
 
  // Re-use sprint option for position
  const currentPosition = index.columns[currentColumnName].indexOf(taskId);
  let newPosition = args.position || args.p;
  if (newPosition) {
    newPosition = parseInt(utility.trimLeftEscapeCharacters(newPosition));
    if (isNaN(newPosition)) {
      utility.error('Position value must be numeric');
      return;
    }
  } else {
    newPosition = null;
  }
 
  // Get a list of sorted columns
  const sortedColumnNames = 'columnSorting' in index.options ? Object.keys(index.options.columnSorting) : [];
 
  // Move task interactively
  if (args.interactive) {
    interactive(
      index.columns,
      columnName,
      columnNames,
      sortedColumnNames,
      taskId,
      newPosition === null
        ? currentPosition
        : (args.relative ? (currentPosition + newPosition) : newPosition)
    )
    .then(answers => {
      moveTask(taskId, answers.column, answers.position);
    })
    .catch(error => {
      utility.error(error);
    });
 
  // Otherwise move task non-interactively
  } else {
    moveTask(taskId, columnName, newPosition, args.relative);
  }
};