Skip to content

Commit 029371f

Browse files
committed
refactor(core,ci,utils): extract report path creation
1 parent 823ade1 commit 029371f

File tree

7 files changed

+71
-22
lines changed

7 files changed

+71
-22
lines changed

packages/ci/src/lib/cli/persist.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
persistConfigSchema,
1010
uploadConfigSchema,
1111
} from '@code-pushup/models';
12-
import { objectFromEntries } from '@code-pushup/utils';
12+
import { createReportPath, objectFromEntries } from '@code-pushup/utils';
1313

1414
export type EnhancedPersistConfig = Pick<CoreConfig, 'persist' | 'upload'>;
1515

@@ -27,12 +27,12 @@ export function persistedFilesFromConfig(
2727
const dir = path.isAbsolute(outputDir)
2828
? outputDir
2929
: path.join(directory, outputDir);
30-
const name = isDiff ? `${filename}-diff` : filename;
30+
const suffix = isDiff ? 'diff' : undefined;
3131

3232
return objectFromEntries(
3333
DEFAULT_PERSIST_FORMAT.map(format => [
3434
format,
35-
path.join(dir, `${name}.${format}`),
35+
createReportPath({ outputDir: dir, filename, format, suffix }),
3636
]),
3737
);
3838
}

packages/core/src/lib/compare.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { writeFile } from 'node:fs/promises';
22
import { createRequire } from 'node:module';
3-
import path from 'node:path';
43
import {
54
type Format,
65
type PersistConfig,
@@ -12,6 +11,7 @@ import {
1211
import {
1312
type Diff,
1413
calcDuration,
14+
createReportPath,
1515
ensureDirectoryExists,
1616
generateMdReportsDiff,
1717
readJsonFile,
@@ -58,7 +58,12 @@ export async function compareReportFiles(
5858

5959
return Promise.all(
6060
format.map(async fmt => {
61-
const outputPath = path.join(outputDir, `${filename}-diff.${fmt}`);
61+
const outputPath = createReportPath({
62+
outputDir,
63+
filename,
64+
format: fmt,
65+
suffix: 'diff',
66+
});
6267
const content = reportsDiffToFileContent(diff, fmt);
6368
await ensureDirectoryExists(outputDir);
6469
await writeFile(outputPath, content);

packages/core/src/lib/implementation/persist.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { mkdir, stat, writeFile } from 'node:fs/promises';
2-
import path from 'node:path';
3-
import type { PersistConfig, Report } from '@code-pushup/models';
2+
import type { Format, PersistConfig, Report } from '@code-pushup/models';
43
import {
54
type MultipleFileResults,
65
type ScoredReport,
6+
createReportPath,
77
directoryExists,
88
generateMdReport,
99
logMultipleFileResults,
@@ -30,20 +30,22 @@ export async function persistReport(
3030
const { outputDir, filename, format } = options;
3131

3232
// collect physical format outputs
33-
const results = format.map(reportType => {
34-
switch (reportType) {
35-
case 'json':
36-
return {
37-
format: 'json',
38-
content: JSON.stringify(report, null, 2),
39-
};
40-
case 'md':
41-
return {
42-
format: 'md',
43-
content: generateMdReport(sortedScoredReport, { outputDir }),
44-
};
45-
}
46-
});
33+
const results = format.map(
34+
(reportType): { format: Format; content: string } => {
35+
switch (reportType) {
36+
case 'json':
37+
return {
38+
format: 'json',
39+
content: JSON.stringify(report, null, 2),
40+
};
41+
case 'md':
42+
return {
43+
format: 'md',
44+
content: generateMdReport(sortedScoredReport, { outputDir }),
45+
};
46+
}
47+
},
48+
);
4749

4850
if (!(await directoryExists(outputDir))) {
4951
try {
@@ -58,7 +60,7 @@ export async function persistReport(
5860
return Promise.allSettled(
5961
results.map(result =>
6062
persistResult(
61-
path.join(outputDir, `${filename}.${result.format}`),
63+
createReportPath({ outputDir, filename, format: result.format }),
6264
result.content,
6365
),
6466
),

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export {
2727
} from './lib/execute-process.js';
2828
export {
2929
crawlFileSystem,
30+
createReportPath,
3031
directoryExists,
3132
ensureDirectoryExists,
3233
fileExists,

packages/utils/src/lib/file-system.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { bold, gray } from 'ansis';
22
import { type Options, bundleRequire } from 'bundle-require';
33
import { mkdir, readFile, readdir, rm, stat } from 'node:fs/promises';
44
import path from 'node:path';
5+
import type { Format, PersistConfig } from '@code-pushup/models';
56
import { formatBytes } from './formatting.js';
67
import { logMultipleResults } from './log-results.js';
78
import { ui } from './logging.js';
@@ -84,6 +85,21 @@ export async function importModule<T = unknown>(options: Options): Promise<T> {
8485
return mod as T;
8586
}
8687

88+
export function createReportPath({
89+
outputDir,
90+
filename,
91+
format,
92+
suffix,
93+
}: Omit<Required<PersistConfig>, 'format'> & {
94+
format: Format;
95+
suffix?: string;
96+
}): string {
97+
return path.join(
98+
outputDir,
99+
suffix ? `${filename}-${suffix}.${format}` : `${filename}.${format}`,
100+
);
101+
}
102+
87103
export function pluginWorkDir(slug: string): string {
88104
return path.join('node_modules', '.code-pushup', slug);
89105
}

packages/utils/src/lib/file-system.unit.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { MEMFS_VOLUME } from '@code-pushup/test-utils';
66
import {
77
type FileResult,
88
crawlFileSystem,
9+
createReportPath,
910
ensureDirectoryExists,
1011
filePathToCliArg,
1112
findLineNumberInText,
@@ -29,6 +30,29 @@ describe('ensureDirectoryExists', () => {
2930
});
3031
});
3132

33+
describe('createReportPath', () => {
34+
it('should create report.json path', () => {
35+
expect(
36+
createReportPath({
37+
outputDir: '.code-pushup',
38+
filename: 'report',
39+
format: 'json',
40+
}),
41+
).toMatchPath('.code-pushup/report.json');
42+
});
43+
44+
it('should create report-diff.md path', () => {
45+
expect(
46+
createReportPath({
47+
outputDir: '.code-pushup',
48+
filename: 'report',
49+
format: 'md',
50+
suffix: 'diff',
51+
}),
52+
).toMatchPath('.code-pushup/report-diff.md');
53+
});
54+
});
55+
3256
describe('logMultipleFileResults', () => {
3357
it('should call logMultipleResults with the correct arguments', () => {
3458
const logMultipleResultsSpy = vi.spyOn(

packages/utils/vitest.unit.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export default defineConfig({
3131
'../../testing/test-setup/src/lib/reset.mocks.ts',
3232
'../../testing/test-setup/src/lib/extend/ui-logger.matcher.ts',
3333
'../../testing/test-setup/src/lib/extend/markdown-table.matcher.ts',
34+
'../../testing/test-setup/src/lib/extend/path.matcher.ts',
3435
],
3536
},
3637
});

0 commit comments

Comments
 (0)