( report: Report, sortedScoredReport: ScoredReport, options: Required<Omit<PersistConfig, 'skipReports'>>, )
| 18 | }; |
| 19 | |
| 20 | export async function persistReport( |
| 21 | report: Report, |
| 22 | sortedScoredReport: ScoredReport, |
| 23 | options: Required<Omit<PersistConfig, 'skipReports'>>, |
| 24 | ): Promise<FileSize[]> { |
| 25 | const { outputDir, filename, format } = options; |
| 26 | |
| 27 | // format report |
| 28 | const results = format.map( |
| 29 | (reportType): { format: Format; content: string } => { |
| 30 | switch (reportType) { |
| 31 | case 'json': |
| 32 | return { |
| 33 | format: 'json', |
| 34 | content: JSON.stringify(report, null, 2), |
| 35 | }; |
| 36 | case 'md': |
| 37 | return { |
| 38 | format: 'md', |
| 39 | content: generateMdReport(sortedScoredReport, { outputDir }), |
| 40 | }; |
| 41 | } |
| 42 | }, |
| 43 | ); |
| 44 | |
| 45 | if (!(await directoryExists(outputDir))) { |
| 46 | try { |
| 47 | await mkdir(outputDir, { recursive: true }); |
| 48 | } catch (error) { |
| 49 | throw new Error( |
| 50 | `Failed to create output directory in ${ansis.bold(outputDir)} - ${stringifyError(error)}`, |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // write relevant format outputs to file system |
| 56 | return Promise.all( |
| 57 | results.map(result => |
| 58 | persistResult( |
| 59 | createReportPath({ outputDir, filename, format: result.format }), |
| 60 | result.content, |
| 61 | ), |
| 62 | ), |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | function persistResult(reportPath: string, content: string): Promise<FileSize> { |
| 67 | return ( |
no test coverage detected