11import ansis from 'ansis' ;
22import { mkdir , stat , writeFile } from 'node:fs/promises' ;
3+ import path from 'node:path' ;
34import type { Format , PersistConfig , Report } from '@code-pushup/models' ;
45import {
5- type MultipleFileResults ,
66 type ScoredReport ,
77 createReportPath ,
88 directoryExists ,
9+ formatBytes ,
910 generateMdReport ,
10- logMultipleFileResults ,
11+ logger ,
1112 stringifyError ,
1213} from '@code-pushup/utils' ;
1314
15+ type FileSize = {
16+ file : string ;
17+ size : number ;
18+ } ;
19+
1420export async function persistReport (
1521 report : Report ,
1622 sortedScoredReport : ScoredReport ,
1723 options : Required < Omit < PersistConfig , 'skipReports' > > ,
18- ) : Promise < MultipleFileResults > {
24+ ) : Promise < FileSize [ ] > {
1925 const { outputDir, filename, format } = options ;
2026
21- // collect physical format outputs
27+ // format report
2228 const results = format . map (
2329 ( reportType ) : { format : Format ; content : string } => {
2430 switch ( reportType ) {
@@ -47,7 +53,7 @@ export async function persistReport(
4753 }
4854
4955 // write relevant format outputs to file system
50- return Promise . allSettled (
56+ return Promise . all (
5157 results . map ( result =>
5258 persistResult (
5359 createReportPath ( { outputDir, filename, format : result . format } ) ,
@@ -57,20 +63,25 @@ export async function persistReport(
5763 ) ;
5864}
5965
60- function persistResult ( reportPath : string , content : string ) {
66+ function persistResult ( reportPath : string , content : string ) : Promise < FileSize > {
6167 return (
6268 writeFile ( reportPath , content )
6369 // return reportPath instead of void
6470 . then ( ( ) => stat ( reportPath ) )
65- . then ( stats => [ reportPath , stats . size ] as const )
71+ . then ( ( stats ) : FileSize => ( { file : reportPath , size : stats . size } ) )
6672 . catch ( ( error : unknown ) => {
6773 throw new Error (
68- `Failed to persist report in ${ ansis . bold ( reportPath ) } - ${ stringifyError ( error ) } ` ,
74+ `Failed to save report in ${ ansis . bold ( reportPath ) } - ${ stringifyError ( error ) } ` ,
6975 ) ;
7076 } )
7177 ) ;
7278}
7379
74- export function logPersistedResults ( persistResults : MultipleFileResults ) {
75- logMultipleFileResults ( persistResults , 'Generated reports' ) ;
80+ export function logPersistedReport ( reportFiles : FileSize [ ] ) {
81+ logger . info ( `Persisted report to file system:` ) ;
82+ reportFiles . forEach ( ( { file, size } ) => {
83+ const name = ansis . bold ( path . relative ( process . cwd ( ) , file ) ) ;
84+ const suffix = ansis . gray ( `(${ formatBytes ( size ) } )` ) ;
85+ logger . info ( `• ${ name } ${ suffix } ` ) ;
86+ } ) ;
7687}
0 commit comments