Jam3/adviser

View on GitHub
src/core/formatters/base.js

Summary

Maintainability
A
2 hrs
Test Coverage
/**
 * @fileoverview Base Terminal formatter
 *
 */

'use strict';

const chalk = require('chalk');
const stripAnsi = require('strip-ansi');
const table = require('text-table');
const hyperlinker = require('hyperlinker');
const supportsHyperlinks = require('supports-hyperlinks');
const pluralize = require('pluralize');

const groupBy = require('../../utils/groupBy');

/**
 * Creates a result output for the terminal
 * @param {Object} results
 * @param {Array} rules
 * @returns {String} Summary output
 */
function base(results, rules) {
  let output = `\n  ${chalk.blue('Output')} \n\n`;
  let summaryColor = 'yellow';

  if (results.items.length === 0) {
    return;
  }

  const resultsByPlugins = groupBy(results.items, 'pluginName');

  Object.keys(resultsByPlugins).forEach(pluginName => {
    output += `  Plugin: ${chalk.underline(pluginName)}\n`;
    const tableData = [];

    resultsByPlugins[pluginName].forEach(result => {
      const rule = rules.find(rule => rule.id === result.ruleName && rule.pluginName === pluginName);

      let messageType = chalk.yellow('warning');

      if (rule.severity === 'error') {
        messageType = chalk.red('error');
        summaryColor = 'red';
      }

      let ruleDocUrl = result.ruleName;

      if (supportsHyperlinks.stdout) {
        ruleDocUrl = hyperlinker(chalk.dim(result.ruleName || ''), rule.core.meta.docsUrl);
      }

      tableData.push([
        '',
        messageType,
        result.params.message.replace(/([^ ])\.$/u, '$1'),
        ruleDocUrl,
        `${chalk.white.bgRed(rule.executionDuration + 'ms')}`
      ]);
    });

    output += `${table(tableData, {
      align: ['l'],
      stringLength(str) {
        return stripAnsi(str).length;
      }
    })
      .split('\n')
      .map(el => el.replace(/(\d+)\s+(\d+)/u, (m, p1, p2) => chalk.dim(`${p1}:${p2}`)))
      .join('\n')}\n\n`;
  });

  const total = results.total.errors + results.total.warnings;

  if (total > 0) {
    output +=
      '  ' +
      chalk[summaryColor].bold(
        [
          '\u2716 ',
          total,
          pluralize(' problem', total),
          ' (',
          results.total.errors,
          pluralize(' error', results.total.errors),
          ', ',
          results.total.warnings,
          pluralize(' warning', results.total.warnings),
          ')\n'
        ].join('')
      );
  }

  return total > 0 ? output : '';
}

module.exports = base;