Skip to content

Commit

Permalink
refactor: remove markdown builder (#461)
Browse files Browse the repository at this point in the history
* refactor: remplace markdown-builder with internal code

* refactor: tweaks

* chore: add changeset

* chore: fix tests local output (ansi codes)
  • Loading branch information
mdjastrzebski authored Feb 21, 2024
1 parent 3258abb commit bb2046a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 154 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-games-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/reassure-compare": patch
---

Replace unmaintained `markdown-builder` library with internal helper functions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
"danger": "^11.3.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.2",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.7.0",
"prettier": "^3.1.1",
"prettier": "^2.8.8",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.73.1",
Expand Down
1 change: 0 additions & 1 deletion packages/compare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"homepage": "https://github.com/callstack/reassure#readme",
"dependencies": {
"@callstack/reassure-logger": "0.3.1",
"markdown-builder": "^0.9.0",
"markdown-table": "^2.0.0",
"zod": "^3.20.2"
},
Expand Down
27 changes: 13 additions & 14 deletions packages/compare/src/output/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as fs from 'fs/promises';
import * as path from 'path';
// @ts-ignore
import { headers, emphasis } from 'markdown-builder';
// @ts-ignore
import markdownTable from 'markdown-table';
import * as logger from '@callstack/reassure-logger';
import {
Expand All @@ -13,6 +11,7 @@ import {
formatCountChange,
formatDurationChange,
} from '../utils/format';
import * as md from '../utils/markdown';
import type {
AddedEntry,
CompareEntry,
Expand Down Expand Up @@ -49,38 +48,38 @@ async function writeToFile(filePath: string, content: string) {
}

function buildMarkdown(data: CompareResult) {
let result = headers.h1('Performance Comparison Report');
let result = md.header1('Performance Comparison Report');

result += `\n${buildMetadataMarkdown('Current', data.metadata.current)}`;
result += `\n${buildMetadataMarkdown('Baseline', data.metadata.baseline)}`;

if (data.errors?.length) {
result += `\n\n${headers.h3('Errors')}\n`;
result += `\n\n${md.header3('Errors')}\n`;
data.errors.forEach((message) => {
result += ` 1. 🛑 ${message}\n`;
});
}

if (data.warnings?.length) {
result += `\n\n${headers.h3('Warnings')}\n`;
result += `\n\n${md.header3('Warnings')}\n`;
data.warnings.forEach((message) => {
result += ` 1. 🟡 ${message}\n`;
});
}

result += `\n\n${headers.h3('Significant Changes To Duration')}`;
result += `\n\n${md.header3('Significant Changes To Duration')}`;
result += `\n${buildSummaryTable(data.significant)}`;
result += `\n${buildDetailsTable(data.significant)}`;
result += `\n\n${headers.h3('Meaningless Changes To Duration')}`;
result += `\n\n${md.header3('Meaningless Changes To Duration')}`;
result += `\n${buildSummaryTable(data.meaningless, true)}`;
result += `\n${buildDetailsTable(data.meaningless)}`;
result += `\n\n${headers.h3('Changes To Count')}`;
result += `\n\n${md.header3('Changes To Count')}`;
result += `\n${buildSummaryTable(data.countChanged)}`;
result += `\n${buildDetailsTable(data.countChanged)}`;
result += `\n\n${headers.h3('Added Scenarios')}`;
result += `\n\n${md.header3('Added Scenarios')}`;
result += `\n${buildSummaryTable(data.added)}`;
result += `\n${buildDetailsTable(data.added)}`;
result += `\n\n${headers.h3('Removed Scenarios')}`;
result += `\n\n${md.header3('Removed Scenarios')}`;
result += `\n${buildSummaryTable(data.removed)}`;
result += `\n${buildDetailsTable(data.removed)}`;
result += '\n';
Expand All @@ -89,11 +88,11 @@ function buildMarkdown(data: CompareResult) {
}

function buildMetadataMarkdown(name: string, metadata: PerformanceMetadata | undefined) {
return ` - **${name}**: ${formatMetadata(metadata)}`;
return ` - ${md.bold(name)}: ${formatMetadata(metadata)}`;
}

function buildSummaryTable(entries: Array<CompareEntry | AddedEntry | RemovedEntry>, collapse: boolean = false) {
if (!entries.length) return emphasis.i('There are no entries');
if (!entries.length) return md.italic('There are no entries');

const rows = entries.map((entry) => [entry.name, entry.type, formatEntryDuration(entry), formatEntryCount(entry)]);
const content = markdownTable([tableHeader, ...rows]);
Expand Down Expand Up @@ -151,7 +150,7 @@ function buildDurationDetails(title: string, entry: PerformanceEntry) {
const relativeStdev = entry.stdevDuration / entry.meanDuration;

return [
emphasis.b(title),
md.bold(title),
`Mean: ${formatDuration(entry.meanDuration)}`,
`Stdev: ${formatDuration(entry.stdevDuration)} (${formatPercent(relativeStdev)})`,
entry.durations ? `Runs: ${formatRunDurations(entry.durations)}` : '',
Expand All @@ -164,7 +163,7 @@ function buildCountDetails(title: string, entry: PerformanceEntry) {
const relativeStdev = entry.stdevCount / entry.meanCount;

return [
emphasis.b(title),
md.bold(title),
`Mean: ${formatCount(entry.meanCount)}`,
`Stdev: ${formatCount(entry.stdevCount)} (${formatPercent(relativeStdev)})`,
entry.counts ? `Runs: ${entry.counts.map(formatCount).join(' ')}` : '',
Expand Down
19 changes: 19 additions & 0 deletions packages/compare/src/utils/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function header1(text: string) {
return `# ${text}\n`;
}

export function header2(text: string) {
return `## ${text}\n`;
}

export function header3(text: string) {
return `### ${text}\n`;
}

export function bold(text: string) {
return `**${text}**`;
}

export function italic(text: string) {
return `*${text}*`;
}
2 changes: 1 addition & 1 deletion packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function log(message: string, ...args: unknown[]) {
rawConsole.log(message, ...args);
}

export function verbose(message?: string, ...args: unknown[]) {
export function verbose(message: string, ...args: unknown[]) {
if (!config.verbose || config.silent) return;

rawConsole.log(colorVerbose(message, ...args));
Expand Down
Loading

0 comments on commit bb2046a

Please sign in to comment.