Skip to content

Commit 76f18be

Browse files
committed
refactor(utils): break up large files
1 parent 04455ae commit 76f18be

File tree

6 files changed

+124
-124
lines changed

6 files changed

+124
-124
lines changed

packages/utils/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export {
7272
} from './lib/reports/flatten-plugins';
7373
export { generateMdReport } from './lib/reports/generate-md-report';
7474
export { generateMdReportsDiff } from './lib/reports/generate-md-reports-diff';
75+
export { loadReport } from './lib/reports/load-report';
7576
export { logStdoutSummary } from './lib/reports/log-stdout-summary';
7677
export { scoreReport } from './lib/reports/scoring';
7778
export { sortReport } from './lib/reports/sorting';
@@ -80,11 +81,7 @@ export {
8081
ScoredGroup,
8182
ScoredReport,
8283
} from './lib/reports/types';
83-
export {
84-
calcDuration,
85-
compareIssueSeverity,
86-
loadReport,
87-
} from './lib/reports/utils';
84+
export { calcDuration, compareIssueSeverity } from './lib/reports/utils';
8885
export { isSemver, normalizeSemver, sortSemvers } from './lib/semver';
8986
export * from './lib/text-formats';
9087
export {

packages/utils/src/lib/reports/generate-md-reports-diff.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import {
55
TableRow,
66
md,
77
} from 'build-md';
8-
import { AuditDiff, ReportsDiff } from '@code-pushup/models';
8+
import { ReportsDiff } from '@code-pushup/models';
99
import { pluralize, pluralizeToken } from '../formatting';
1010
import { HIERARCHY } from '../text-formats';
1111
import { objectToEntries } from '../transform';
1212
import { DiffOutcome } from './types';
1313
import {
14-
colorByScoreDiff,
15-
formatDiffNumber,
14+
formatScoreChange,
1615
formatScoreWithColor,
17-
getDiffMarker,
16+
formatValueChange,
1817
scoreMarker,
1918
} from './utils';
2019

@@ -208,28 +207,6 @@ function createGroupsOrAuditsDetails<T extends 'group' | 'audit'>(
208207
);
209208
}
210209

211-
function formatScoreChange(diff: number): InlineText {
212-
const marker = getDiffMarker(diff);
213-
const text = formatDiffNumber(Math.round(diff * 1000) / 10); // round with max 1 decimal
214-
return colorByScoreDiff(`${marker} ${text}`, diff);
215-
}
216-
217-
function formatValueChange({
218-
values,
219-
scores,
220-
}: Pick<AuditDiff, 'values' | 'scores'>): InlineText {
221-
const marker = getDiffMarker(values.diff);
222-
const percentage =
223-
values.before === 0
224-
? values.diff > 0
225-
? Number.POSITIVE_INFINITY
226-
: Number.NEGATIVE_INFINITY
227-
: Math.round((100 * values.diff) / values.before);
228-
// eslint-disable-next-line no-irregular-whitespace
229-
const text = `${formatDiffNumber(percentage)} %`;
230-
return colorByScoreDiff(`${marker} ${text}`, scores.diff);
231-
}
232-
233210
function summarizeUnchanged(
234211
token: 'category' | 'group' | 'audit',
235212
{ changed, unchanged }: { changed: unknown[]; unchanged: unknown[] },
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { join } from 'node:path';
2+
import {
3+
Format,
4+
PersistConfig,
5+
Report,
6+
reportSchema,
7+
} from '@code-pushup/models';
8+
import {
9+
ensureDirectoryExists,
10+
readJsonFile,
11+
readTextFile,
12+
} from '../file-system';
13+
14+
type LoadedReportFormat<T extends Format> = T extends 'json' ? Report : string;
15+
16+
export async function loadReport<T extends Format>(
17+
options: Required<Omit<PersistConfig, 'format'>> & {
18+
format: T;
19+
},
20+
): Promise<LoadedReportFormat<T>> {
21+
const { outputDir, filename, format } = options;
22+
await ensureDirectoryExists(outputDir);
23+
const filePath = join(outputDir, `${filename}.${format}`);
24+
25+
if (format === 'json') {
26+
const content = await readJsonFile(filePath);
27+
return reportSchema.parse(content) as LoadedReportFormat<T>;
28+
}
29+
30+
const text = await readTextFile(filePath);
31+
return text as LoadedReportFormat<T>;
32+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { vol } from 'memfs';
2+
import { Report } from '@code-pushup/models';
3+
import { MEMFS_VOLUME, REPORT_MOCK, reportMock } from '@code-pushup/test-utils';
4+
import { loadReport } from './load-report';
5+
6+
describe('loadReport', () => {
7+
it('should load a valid JSON report', async () => {
8+
vol.fromJSON(
9+
{
10+
[`report.json`]: JSON.stringify(reportMock()),
11+
[`report.md`]: 'test-42',
12+
},
13+
MEMFS_VOLUME,
14+
);
15+
16+
await expect(
17+
loadReport({
18+
outputDir: MEMFS_VOLUME,
19+
filename: 'report',
20+
format: 'json',
21+
}),
22+
).resolves.toEqual(reportMock());
23+
});
24+
25+
it('should load a markdown file', async () => {
26+
vol.fromJSON(
27+
{
28+
[`report.dummy.md`]: 'test-7',
29+
[`report.json`]: '{"test":42}',
30+
[`report.md`]: 'test-42',
31+
},
32+
MEMFS_VOLUME,
33+
);
34+
35+
await expect(
36+
loadReport({
37+
outputDir: MEMFS_VOLUME,
38+
format: 'md',
39+
filename: 'report',
40+
}),
41+
).resolves.toBe('test-42');
42+
});
43+
44+
it('should throw for an invalid JSON report', async () => {
45+
vol.fromJSON(
46+
{
47+
[`report.json`]: JSON.stringify({
48+
...REPORT_MOCK,
49+
plugins: [{ ...REPORT_MOCK.plugins[0]!, slug: '-Invalid_slug' }],
50+
} satisfies Report),
51+
},
52+
MEMFS_VOLUME,
53+
);
54+
55+
await expect(
56+
loadReport({
57+
outputDir: MEMFS_VOLUME,
58+
filename: 'report',
59+
format: 'json',
60+
}),
61+
).rejects.toThrow('slug has to follow the pattern');
62+
});
63+
});

packages/utils/src/lib/reports/utils.ts

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import { InlineText, md } from 'build-md';
2-
import { join } from 'node:path';
32
import {
3+
AuditDiff,
44
AuditReport,
55
CategoryRef,
66
IssueSeverity as CliIssueSeverity,
7-
Format,
87
Group,
98
Issue,
10-
PersistConfig,
11-
Report,
12-
reportSchema,
139
} from '@code-pushup/models';
14-
import {
15-
ensureDirectoryExists,
16-
readJsonFile,
17-
readTextFile,
18-
} from '../file-system';
1910
import { SCORE_COLOR_RANGE } from './constants';
2011
import { ScoredReport, SortableAuditReport, SortableGroup } from './types';
2112

@@ -105,6 +96,28 @@ export function severityMarker(severity: 'info' | 'warning' | 'error'): string {
10596
return 'ℹ️';
10697
}
10798

99+
export function formatScoreChange(diff: number): InlineText {
100+
const marker = getDiffMarker(diff);
101+
const text = formatDiffNumber(Math.round(diff * 1000) / 10); // round with max 1 decimal
102+
return colorByScoreDiff(`${marker} ${text}`, diff);
103+
}
104+
105+
export function formatValueChange({
106+
values,
107+
scores,
108+
}: Pick<AuditDiff, 'values' | 'scores'>): InlineText {
109+
const marker = getDiffMarker(values.diff);
110+
const percentage =
111+
values.before === 0
112+
? values.diff > 0
113+
? Number.POSITIVE_INFINITY
114+
: Number.NEGATIVE_INFINITY
115+
: Math.round((100 * values.diff) / values.before);
116+
// eslint-disable-next-line no-irregular-whitespace
117+
const text = `${formatDiffNumber(percentage)} %`;
118+
return colorByScoreDiff(`${marker} ${text}`, scores.diff);
119+
}
120+
108121
export function calcDuration(start: number, stop?: number): number {
109122
return Math.round((stop ?? performance.now()) - start);
110123
}
@@ -261,26 +274,6 @@ export function compareIssueSeverity(
261274
return levels[severity1] - levels[severity2];
262275
}
263276

264-
type LoadedReportFormat<T extends Format> = T extends 'json' ? Report : string;
265-
266-
export async function loadReport<T extends Format>(
267-
options: Required<Omit<PersistConfig, 'format'>> & {
268-
format: T;
269-
},
270-
): Promise<LoadedReportFormat<T>> {
271-
const { outputDir, filename, format } = options;
272-
await ensureDirectoryExists(outputDir);
273-
const filePath = join(outputDir, `${filename}.${format}`);
274-
275-
if (format === 'json') {
276-
const content = await readJsonFile(filePath);
277-
return reportSchema.parse(content) as LoadedReportFormat<T>;
278-
}
279-
280-
const text = await readTextFile(filePath);
281-
return text as LoadedReportFormat<T>;
282-
}
283-
284277
export function throwIsNotPresentError(
285278
itemName: string,
286279
presentPlace: string,

packages/utils/src/lib/reports/utils.unit.test.ts

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { vol } from 'memfs';
21
import { describe, expect, it } from 'vitest';
3-
import { AuditReport, Issue, IssueSeverity, Report } from '@code-pushup/models';
4-
import { MEMFS_VOLUME, REPORT_MOCK, reportMock } from '@code-pushup/test-utils';
2+
import { AuditReport, Issue, IssueSeverity } from '@code-pushup/models';
53
import { SCORE_COLOR_RANGE } from './constants';
64
import { ScoredReport, SortableAuditReport, SortableGroup } from './types';
75
import {
@@ -21,7 +19,6 @@ import {
2119
getSortableAuditByRef,
2220
getSortableGroupByRef,
2321
getSortedGroupAudits,
24-
loadReport,
2522
scoreMarker,
2623
severityMarker,
2724
} from './utils';
@@ -367,65 +364,6 @@ describe('compareIssueSeverity', () => {
367364
});
368365
});
369366

370-
describe('loadReport', () => {
371-
it('should load a valid JSON report', async () => {
372-
vol.fromJSON(
373-
{
374-
[`report.json`]: JSON.stringify(reportMock()),
375-
[`report.md`]: 'test-42',
376-
},
377-
MEMFS_VOLUME,
378-
);
379-
380-
await expect(
381-
loadReport({
382-
outputDir: MEMFS_VOLUME,
383-
filename: 'report',
384-
format: 'json',
385-
}),
386-
).resolves.toEqual(reportMock());
387-
});
388-
389-
it('should load a markdown file', async () => {
390-
vol.fromJSON(
391-
{
392-
[`report.dummy.md`]: 'test-7',
393-
[`report.json`]: '{"test":42}',
394-
[`report.md`]: 'test-42',
395-
},
396-
MEMFS_VOLUME,
397-
);
398-
399-
await expect(
400-
loadReport({
401-
outputDir: MEMFS_VOLUME,
402-
format: 'md',
403-
filename: 'report',
404-
}),
405-
).resolves.toBe('test-42');
406-
});
407-
408-
it('should throw for an invalid JSON report', async () => {
409-
vol.fromJSON(
410-
{
411-
[`report.json`]: JSON.stringify({
412-
...REPORT_MOCK,
413-
plugins: [{ ...REPORT_MOCK.plugins[0]!, slug: '-Invalid_slug' }],
414-
} satisfies Report),
415-
},
416-
MEMFS_VOLUME,
417-
);
418-
419-
await expect(
420-
loadReport({
421-
outputDir: MEMFS_VOLUME,
422-
filename: 'report',
423-
format: 'json',
424-
}),
425-
).rejects.toThrow('slug has to follow the pattern');
426-
});
427-
});
428-
429367
describe('compareCategoryAuditsAndGroups', () => {
430368
it('should sort audits by weight and score', () => {
431369
const mockAudits = [

0 commit comments

Comments
 (0)