cli icon indicating copy to clipboard operation
cli copied to clipboard

format report output

Open BioPhoton opened this issue 10 months ago • 1 comments

User story

As a reader of the CLI reports I want to be able to easily consume the generated information. Formatting is key here. Even if many IDEs support a markdown viewer or auto-format JSON files, it still helps to be able to read the reports in a plain editor.

The current output of the markdown format is not formatted.

Acceptance criteria

  • [ ] implement helper form text formatting by wrapping prettier
    • [ ] accept the content to format and a optional argument for the format
  • [ ] format all markdown formats accordingly
  • [ ] ensure formatting is given over snapshot tests

Implementation details

Prettier wrapper:

// packages/utils/src/lib/formatting.ts
import { format, Options } from 'prettier';

export function formatText(
  content: string,
  parser: Options['parser'] = 'typescript'
) {
  return format(content, {
    parser
  }).trim();
}

Markdown helper:

// packages/utils/src/lib/report/md/table.ts
import { formatText } from '../formatting';

export function tableHtml(data: (string | number)[][]): string {
  if (data.length === 0) {
    throw new Error("Data can't be empty");
  }

  const tableContent = data.map((arr, index) => {
    if (index === 0) {
      const headerRow = arr.map(s => `<th>${s}</th>`).join('');
      return `<tr>${headerRow}</tr>`;
    }
    const row = arr.map(s => `<td>${s}</td>`).join('');
    return `<tr>${row}</tr>`;
  });
  return formatText(`<table>${tableContent.join('')}</table>`;
}

BioPhoton avatar Apr 17 '24 11:04 BioPhoton