cli icon indicating copy to clipboard operation
cli copied to clipboard

format report output

Open BioPhoton opened this issue 1 year 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

We should check if prettier is installed and run it if available. That way we don't have to list an extra dependency.

matejchalk avatar Apr 18 '24 13:04 matejchalk

I believe this one was closed by the new md-build library. Cc @matejchalk

BioPhoton avatar Jul 27 '24 13:07 BioPhoton

Indeed, after #755 was merged the tables have padding and alignment to make the source more readable. In general, the output should be prettified already, so there's no need to run Prettier afterwards.

matejchalk avatar Jul 28 '24 08:07 matejchalk