@@ -61,35 +61,56 @@ export function outputPurlsShallowScore(
6161 logger . log ( txt )
6262}
6363
64- function formatReportCard ( artifact : DedupedArtifact , color : boolean ) : string {
64+ function formatReportCard (
65+ artifact : DedupedArtifact ,
66+ colorize : boolean ,
67+ ) : string {
6568 const scoreResult = {
6669 'Supply Chain Risk' : Math . floor ( ( artifact . score ?. supplyChain ?? 0 ) * 100 ) ,
6770 Maintenance : Math . floor ( ( artifact . score ?. maintenance ?? 0 ) * 100 ) ,
6871 Quality : Math . floor ( ( artifact . score ?. quality ?? 0 ) * 100 ) ,
6972 Vulnerabilities : Math . floor ( ( artifact . score ?. vulnerability ?? 0 ) * 100 ) ,
7073 License : Math . floor ( ( artifact . score ?. license ?? 0 ) * 100 ) ,
7174 }
72- const alertString = getAlertString ( artifact . alerts , ! color )
75+ const alertString = getAlertString ( artifact . alerts , { colorize } )
7376 if ( ! artifact . ecosystem ) {
7477 debugFn ( 'notice' , 'miss: artifact ecosystem' , artifact )
7578 }
7679 const purl = `pkg:${ artifact . ecosystem } /${ artifact . name } ${ artifact . version ? '@' + artifact . version : '' } `
7780
81+ // Calculate proper padding based on longest label.
82+ const maxLabelLength = Math . max (
83+ ...Object . keys ( scoreResult ) . map ( label => label . length ) ,
84+ )
85+ const labelPadding = maxLabelLength + 2 // +2 for ": "
86+
7887 return [
79- 'Package: ' + ( color ? colors . bold ( purl ) : purl ) ,
88+ 'Package: ' + ( colorize ? colors . bold ( purl ) : purl ) ,
8089 '' ,
8190 ...Object . entries ( scoreResult ) . map (
8291 score =>
83- `- ${ score [ 0 ] } :` . padEnd ( 20 , ' ' ) +
84- ` ${ formatScore ( score [ 1 ] , ! color , true ) } ` ,
92+ `- ${ score [ 0 ] } :` . padEnd ( labelPadding , ' ' ) +
93+ ` ${ formatScore ( score [ 1 ] , { colorize } ) } ` ,
8594 ) ,
8695 alertString ,
8796 ] . join ( '\n' )
8897}
8998
90- function formatScore ( score : number , noColor = false , pad = false ) : string {
91- const padded = String ( score ) . padStart ( pad ? 3 : 0 , ' ' )
92- if ( noColor ) {
99+ type FormatScoreOptions = {
100+ colorize ?: boolean | undefined
101+ padding ?: number | undefined
102+ }
103+
104+ function formatScore (
105+ score : number ,
106+ options ?: FormatScoreOptions | undefined ,
107+ ) : string {
108+ const { colorize, padding = 3 } = {
109+ __proto__ : null ,
110+ ...options ,
111+ } as FormatScoreOptions
112+ const padded = String ( score ) . padStart ( padding , ' ' )
113+ if ( ! colorize ) {
93114 return padded
94115 }
95116 if ( score >= 80 ) {
@@ -101,61 +122,63 @@ function formatScore(score: number, noColor = false, pad = false): string {
101122 return colors . red ( padded )
102123}
103124
125+ type AlertStringOptions = {
126+ colorize ?: boolean | undefined
127+ }
128+
104129function getAlertString (
105130 alerts : DedupedArtifact [ 'alerts' ] ,
106- noColor = false ,
131+ options ?: AlertStringOptions | undefined ,
107132) : string {
133+ const { colorize } = { __proto__ : null , ...options } as AlertStringOptions
134+
108135 if ( ! alerts . size ) {
109- return noColor ? `- Alerts: none!` : `- Alerts: ${ colors . green ( 'none' ) } !`
136+ return `- Alerts: ${ colorize ? colors . green ( 'none' ) : 'none' } !`
110137 }
111138
112139 const o = Array . from ( alerts . values ( ) )
140+
113141 const bad = o
114142 . filter ( alert => alert . severity !== 'low' && alert . severity !== 'middle' )
115143 . sort ( ( a , b ) => ( a . type < b . type ? - 1 : a . type > b . type ? 1 : 0 ) )
144+
116145 const mid = o
117146 . filter ( alert => alert . severity === 'middle' )
118147 . sort ( ( a , b ) => ( a . type < b . type ? - 1 : a . type > b . type ? 1 : 0 ) )
148+
119149 const low = o
120150 . filter ( alert => alert . severity === 'low' )
121151 . sort ( ( a , b ) => ( a . type < b . type ? - 1 : a . type > b . type ? 1 : 0 ) )
122152
123153 // We need to create the no-color string regardless because the actual string
124154 // contains a bunch of invisible ANSI chars which would screw up length checks.
125155 const colorless = `- Alerts (${ bad . length } /${ mid . length . toString ( ) } /${ low . length } ):`
156+ const padding = ` ${ ' ' . repeat ( Math . max ( 0 , 20 - colorless . length ) ) } `
126157
127- if ( noColor ) {
158+ if ( colorize ) {
128159 return (
129- colorless +
130- ' ' . repeat ( Math . max ( 0 , 20 - colorless . length ) ) +
131- ' ' +
160+ `- Alerts (${ colors . red ( bad . length . toString ( ) ) } /${ colors . yellow ( mid . length . toString ( ) ) } /${ low . length } ):` +
161+ padding +
132162 [
133- bad . map ( alert => `[${ alert . severity } ] ` + alert . type ) . join ( ', ' ) ,
134- mid . map ( alert => `[${ alert . severity } ] ` + alert . type ) . join ( ', ' ) ,
135- low . map ( alert => `[${ alert . severity } ] ` + alert . type ) . join ( ', ' ) ,
163+ bad
164+ . map ( a => colors . red ( `${ colors . dim ( `[${ a . severity } ] ` ) } ${ a . type } ` ) )
165+ . join ( ', ' ) ,
166+ mid
167+ . map ( a => colors . yellow ( `${ colors . dim ( `[${ a . severity } ] ` ) } ${ a . type } ` ) )
168+ . join ( ', ' ) ,
169+ low . map ( a => `${ colors . dim ( `[${ a . severity } ] ` ) } ${ a . type } ` ) . join ( ', ' ) ,
136170 ]
137171 . filter ( Boolean )
138172 . join ( ', ' )
139173 )
140174 }
141175 return (
142- `- Alerts (${ colors . red ( bad . length . toString ( ) ) } /${ colors . yellow ( mid . length . toString ( ) ) } /${ low . length } ):` +
143- ' ' . repeat ( Math . max ( 0 , 20 - colorless . length ) ) +
144- ' ' +
176+ colorless +
177+ padding +
145178 [
146- bad
147- . map ( alert =>
148- colors . red ( colors . dim ( `[${ alert . severity } ] ` ) + alert . type ) ,
149- )
150- . join ( ', ' ) ,
151- mid
152- . map ( alert =>
153- colors . yellow ( colors . dim ( `[${ alert . severity } ] ` ) + alert . type ) ,
154- )
155- . join ( ', ' ) ,
156- low
157- . map ( alert => colors . dim ( `[${ alert . severity } ] ` ) + alert . type )
158- . join ( ', ' ) ,
179+ bad . map ( a => `[${ a . severity } ] ${ a . type } ` ) . join ( ', ' ) ,
180+ mid . map ( a => `[${ a . severity } ] ${ a . type } ` ) . join ( ', ' ) ,
181+ low . map ( a => `[${ a . severity } ] ${ a . type } ` ) . join ( ', ' ) ,
159182 ]
160183 . filter ( Boolean )
161184 . join ( ', ' )
0 commit comments