16
16
17
17
const kleur = require ( 'kleur' ) ;
18
18
const bytes = require ( 'bytes' ) ;
19
- import { ItemConfig , CompressionMap , Context } from './validation/Condition' ;
19
+ import { ItemConfig , CompressionMap , Context , OrderedCompressionValues } from './validation/Condition' ;
20
20
21
21
// Disable output colors for test runs.
22
22
kleur . enabled = ! ( 'AVA_PATH' in process . env ) ;
23
23
24
24
// Aliases to colors used.
25
- const { red, grey, green, bold } = kleur ;
25
+ // @ts -ignore
26
+ const { red, grey, yellow, green, bold, dim } = kleur ;
26
27
27
28
/**
28
29
* Format output as an error message.
@@ -49,71 +50,102 @@ function prettyBytes(size: number): string {
49
50
}
50
51
51
52
/**
52
- * Given a compression type, format it to a human readable file extension.
53
+ *
54
+ * @param size
55
+ * @param maxSize
56
+ * @param maxWidth
57
+ */
58
+ function displaySize ( size : number | null , maxSize : number | null , maxWidth : number ) : [ boolean | null , string ] {
59
+ if ( size === null || maxSize === null ) {
60
+ return [ null , dim ( ) . grey ( '–' . padEnd ( maxWidth ) ) ] ;
61
+ } else if ( size < maxSize ) {
62
+ if ( 1 - size / maxSize < 0.05 ) {
63
+ return [ true , yellow ( prettyBytes ( size ) . padEnd ( maxWidth ) ) ] ;
64
+ }
65
+ return [ true , dim ( ) . green ( prettyBytes ( size ) . padEnd ( maxWidth ) ) ] ;
66
+ } else {
67
+ return [ false , red ( prettyBytes ( size ) . padEnd ( maxWidth ) ) ] ;
68
+ }
69
+ }
70
+
71
+ /**
72
+ *
73
+ * @param report
74
+ * @param paths
53
75
* @param compression
54
76
*/
55
- function compressedExtension ( compression : string ) : string {
56
- if ( compression === 'none' ) {
57
- return '' . padEnd ( 3 ) ;
77
+ function maxLengthForCompression ( report : Map < ItemConfig [ 'path' ] , CompressionMap > , paths : Array < string > , compression : string ) : number {
78
+ const reportedValueStrings : Array < number > = [ ] ;
79
+
80
+ for ( const path of paths ) {
81
+ const value = report . get ( path ) ?. get ( compression ) ;
82
+ if ( value ) {
83
+ const [ size ] = value ;
84
+ if ( size !== null ) {
85
+ reportedValueStrings . push ( prettyBytes ( size ) . length ) ;
86
+ }
87
+ }
58
88
}
59
- return '.' + compression . substring ( 0 , 2 ) ;
89
+
90
+ return Math . max . apply ( null , reportedValueStrings ) + 2 ;
91
+ }
92
+
93
+ /**
94
+ * Given a compression type, format it to a human readable file extension.
95
+ * @param compression
96
+ */
97
+ function compressedExtension ( compression : string , padEnd : number ) : string {
98
+ return compression . padEnd ( padEnd ) ;
60
99
}
61
100
62
101
/**
63
102
* Display report to the console.
64
103
* @param report
65
104
*/
66
105
export function LogReport ( { silent } : Context , report : Map < ItemConfig [ 'path' ] , CompressionMap > ) {
106
+ if ( silent ) {
107
+ return ;
108
+ }
109
+
110
+ const paths = Array . from ( report . keys ( ) ) ;
111
+ const pathMaxLength = Math . max . apply (
112
+ null ,
113
+ paths . map ( path => path . length + 2 ) ,
114
+ ) ;
115
+ const formatMaxLengths = OrderedCompressionValues . map ( compression => maxLengthForCompression ( report , paths , compression ) ) ;
116
+ const compressionHeaders = OrderedCompressionValues . map ( ( compression , index ) => compressedExtension ( compression , formatMaxLengths [ index ] ) ) ;
67
117
let success : number = 0 ;
68
118
let failure : number = 0 ;
69
119
70
- if ( ! silent && [ ...report . keys ( ) ] . length > 0 ) {
71
- console . log ( bold ( '\nFilesize Report' ) ) ;
72
- for ( const [ originalPath , values ] of report ) {
73
- const multipleOutputs = Array . from ( values . values ( ) ) . filter ( ( [ before ] ) => before !== null ) . length > 1 ;
74
- if ( multipleOutputs ) {
75
- console . log ( grey ( `\npath: ${ originalPath } ` ) ) ;
76
- for ( const [ compression , compressionResults ] of values ) {
77
- const [ size , maxSize ] = compressionResults ;
78
- const compressedPath = originalPath + grey ( compressedExtension ( compression ) ) ;
79
- if ( size === null || maxSize === null ) {
80
- continue ;
81
- } else if ( size < maxSize ) {
82
- success ++ ;
83
- console . log ( ` ✔️ ${ compressedPath } ${ prettyBytes ( size ) } ${ green ( '<' ) } ${ prettyBytes ( maxSize ) } ` ) ;
84
- } else {
85
- failure ++ ;
86
- console . log ( ` ❌ ${ compressedPath } ${ prettyBytes ( size ) } ${ red ( '>' ) } ${ prettyBytes ( maxSize ) } ` ) ;
87
- }
88
- }
89
- } else {
90
- const maximumPath =
91
- Math . max . apply (
92
- null ,
93
- Array . from ( report . keys ( ) ) . map ( item => item . length ) ,
94
- ) + 1 ;
95
- for ( const [ compression , compressionResults ] of values ) {
96
- const [ size , maxSize ] = compressionResults ;
97
- const compressedPath = originalPath + grey ( compressedExtension ( compression ) ) + new Array ( maximumPath - originalPath . length ) . join ( ' ' ) ;
98
- if ( size === null || maxSize === null ) {
99
- continue ;
100
- } else if ( size < maxSize ) {
101
- success ++ ;
102
- console . log ( ` ✔️ ${ compressedPath } ${ prettyBytes ( size ) } ${ green ( '<' ) } ${ prettyBytes ( maxSize ) } ` ) ;
103
- } else {
104
- failure ++ ;
105
- console . log ( ` ❌ ${ compressedPath } ${ prettyBytes ( size ) } ${ red ( '>' ) } ${ prettyBytes ( maxSize ) } ` ) ;
106
- }
107
- }
108
- }
120
+ console . log ( bold ( '\nFilesizes' ) ) ;
121
+ console . log ( '' . padEnd ( pathMaxLength ) + ' ' + compressionHeaders . join ( '' ) ) ;
122
+ for ( const path of paths ) {
123
+ const compressionMap = report . get ( path ) ;
124
+ if ( ! compressionMap ) {
125
+ continue ;
109
126
}
110
- if ( success > 0 || failure > 0 ) {
111
- console . log ( '\n ' + green ( success + ` ${ success === 1 ? 'check' : 'checks' } passed` ) + ( failure === 0 ? ' 🎉' : '' ) ) ;
112
- const failureColor = failure < 1 ? grey : red ;
113
- console . log ( ' ' + failureColor ( failure + ` ${ failure === 1 ? 'check' : 'checks' } failed` ) ) ;
127
+
128
+ let message = path . padEnd ( pathMaxLength ) + ' ' ;
129
+ let compressionIndex = 0 ;
130
+ for ( const compression of OrderedCompressionValues ) {
131
+ const padding = compressionHeaders [ compressionIndex ] . length ;
132
+ const [ size , maxSize ] = compressionMap . get ( compression ) as [ number | null , number | null ] ;
133
+
134
+ const [ successful , compressionMessage ] = displaySize ( size , maxSize , padding ) ;
135
+ if ( successful ) {
136
+ success ++ ;
137
+ } else if ( successful !== null ) {
138
+ failure ++ ;
139
+ }
140
+ message += compressionMessage ;
141
+ compressionIndex ++ ;
114
142
}
115
- console . log ( ) ;
116
- } else if ( ! silent ) {
117
- MakeError ( 'No report available.' ) ;
143
+ console . log ( message ) ;
144
+ }
145
+ if ( success > 0 || failure > 0 ) {
146
+ console . log ( '\n ' + green ( success + ` ${ success === 1 ? 'check' : 'checks' } passed` ) + ( failure === 0 ? ' 🎉' : '' ) ) ;
147
+ const failureColor = failure < 1 ? grey : red ;
148
+ console . log ( ' ' + failureColor ( failure + ` ${ failure === 1 ? 'check' : 'checks' } failed` ) ) ;
118
149
}
150
+ console . log ( ) ;
119
151
}
0 commit comments