All files / kanbn/src/controller validate.js

100% Statements 94/94
88.88% Branches 32/36
100% Functions 2/2
100% Lines 94/94

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 951x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 2x 2x 3x 3x 3x 3x 2x 2x 3x 1x 1x 1x 1x 3x 1x 1x 14x 14x 14x 1x 1x 1x 13x 13x 13x 13x 13x 14x 1x 1x 1x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 10x 14x 4x 4x 4x 4x 1x 1x 1x 4x 1x 1x 1x 4x 1x 1x 1x 4x 4x 1x 1x 1x 1x 6x 6x 6x 6x 6x 14x 1x 1x 1x 14x 3x 3x 3x 2x 1x  
const kanbn = require('../main');
const utility = require('../utility');
const yaml = require('yamljs');
 
/**
 * Show a list of tasks whose dates disagree with the column they're in
 * @param {object[]} drift
 * @param {boolean} json
 */
function showDrift(drift, json) {
  if (json) {
    console.log(JSON.stringify(drift, null, 2));
    return;
  }
  const fixable = drift.filter(d => d.fixable);
  console.log(
    `${drift.length} ${drift.length === 1 ? 'task has' : 'tasks have'} dates that don't match ` +
    `${drift.length === 1 ? 'its' : 'their'} column:`
  );
  for (const item of drift) {
    console.log(`  ${item.task}: ${item.message}`);
  }
  if (fixable.length) {
    console.log(utility.replaceTags(
      `\nRun {b}kanbn validate --fix{b} to fill in ${fixable.length === 1 ? 'the missing date' : 'the missing dates'}.`
    ));
  }
}
 
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;
  }
 
  // Validate kanbn files
  let result;
  try {
    result = await kanbn.validate(args.save);
  } catch (error) {
    utility.error(error);
    return;
  }
  if (result !== true) {
    utility.error(
      `${result.length} errors found in task files:\n${(
        args.json
          ? JSON.stringify(result, null, 2)
          : yaml.stringify(result, 4, 2)
      )}`
    );
    return;
  }
 
  // Backfill missing started and completed dates
  if (args.fix) {
    let fixed;
    try {
      fixed = await kanbn.fixDateDrift();
    } catch (error) {
      utility.error(error);
      return;
    }
    if (!fixed.length) {
      console.log('Everything OK, nothing to fix');
      return;
    }
    if (args.json) {
      console.log(JSON.stringify(fixed, null, 2));
      return;
    }
    console.log(`Filled in ${fixed.length} missing ${fixed.length === 1 ? 'date' : 'dates'}:`);
    for (const item of fixed) {
      console.log(`  ${item.task}: ${item.field} set to ${item.date.toISOString()} (from ${item.source})`);
    }
    return;
  }
 
  // Report tasks whose dates disagree with their column
  let drift;
  try {
    drift = await kanbn.findDateDrift();
  } catch (error) {
    utility.error(error);
    return;
  }
  if (drift.length) {
    showDrift(drift, args.json);
    return;
  }
  console.log('Everything OK');
};