( files: string[], persistConfig: Required<PersistConfig>, )
| 12 | } from '@code-pushup/utils'; |
| 13 | |
| 14 | export async function mergeDiffs( |
| 15 | files: string[], |
| 16 | persistConfig: Required<PersistConfig>, |
| 17 | ): Promise<string> { |
| 18 | const results = await Promise.allSettled( |
| 19 | files.map(async file => { |
| 20 | const json = await readJsonFile(file).catch((error: unknown) => { |
| 21 | throw new Error( |
| 22 | `Failed to read JSON file ${file} - ${stringifyError(error)}`, |
| 23 | ); |
| 24 | }); |
| 25 | const result = await reportsDiffSchema.safeParseAsync(json); |
| 26 | if (!result.success) { |
| 27 | throw new Error( |
| 28 | `Invalid reports diff in ${file} - ${result.error.message}`, |
| 29 | ); |
| 30 | } |
| 31 | return { ...result.data, file }; |
| 32 | }), |
| 33 | ); |
| 34 | results.filter(isPromiseRejectedResult).forEach(({ reason }) => { |
| 35 | logger.warn(`Skipped invalid report diff - ${stringifyError(reason)}`); |
| 36 | }); |
| 37 | const diffs = results |
| 38 | .filter(isPromiseFulfilledResult) |
| 39 | .map(({ value }) => value); |
| 40 | |
| 41 | const labeledDiffs = diffs.map(diff => ({ |
| 42 | ...diff, |
| 43 | label: diff.label || path.basename(path.dirname(diff.file)), // fallback is parent folder name |
| 44 | })); |
| 45 | |
| 46 | const markdown = generateMdReportsDiffForMonorepo(labeledDiffs); |
| 47 | |
| 48 | const { outputDir, filename } = persistConfig; |
| 49 | const outputPath = path.join(outputDir, `${filename}-diff.md`); |
| 50 | await ensureDirectoryExists(outputDir); |
| 51 | await writeFile(outputPath, markdown); |
| 52 | |
| 53 | return outputPath; |
| 54 | } |
no test coverage detected