@@ -15,6 +15,7 @@ const formatBytes = bytes => {
1515 if ( ! bytes ) {
1616 return '0 B' ;
1717 }
18+
1819 const i = Math . floor ( Math . log ( Math . abs ( bytes ) ) / Math . log ( 1024 ) ) ;
1920 return `${ ( bytes / Math . pow ( 1024 , i ) ) . toFixed ( 2 ) } ${ UNITS [ i ] } ` ;
2021} ;
@@ -45,37 +46,17 @@ const getDirectoryStats = async dir => {
4546 return new Map ( entries ) ;
4647} ;
4748
48- /**
49- * Gets the extension of a file
50- * @param {string } file - Filename
51- * @returns {string } File extension (without dot)
52- */
53- const getExtension = file => file . split ( '.' ) . pop ( ) ;
54-
55- /**
56- * Gets the base name of a file (without extension)
57- * @param {string } file - Filename
58- * @returns {string } Base filename
59- */
60- const getBaseName = file => file . slice ( 0 , file . lastIndexOf ( '.' ) || file . length ) ;
61-
6249/**
6350 * Generates a table row for a file
6451 * @param {string } file - Filename
65- * @param {number } [ baseSize] - Base size in bytes
66- * @param {number } [ headSize] - Head size in bytes
52+ * @param {number } baseSize - Base size in bytes
53+ * @param {number } headSize - Head size in bytes
6754 * @returns {string } Markdown table row
6855 */
6956const generateRow = ( file , baseSize , headSize ) => {
70- const baseCol = baseSize != null ? formatBytes ( baseSize ) : '-' ;
71- const headCol = headSize != null ? formatBytes ( headSize ) : '-' ;
72-
73- let diffCol = 'Added' ;
74- if ( baseSize != null && headSize != null ) {
75- diffCol = formatDiff ( baseSize , headSize ) ;
76- } else if ( baseSize != null ) {
77- diffCol = 'Removed' ;
78- }
57+ const baseCol = formatBytes ( baseSize ) ;
58+ const headCol = formatBytes ( headSize ) ;
59+ const diffCol = formatDiff ( baseSize , headSize ) ;
7960
8061 return `| \`${ file } \` | ${ baseCol } | ${ headCol } | ${ diffCol } |` ;
8162} ;
@@ -104,40 +85,48 @@ const generateTable = (files, baseStats, headStats) => {
10485const details = ( summary , content ) =>
10586 `<details>\n<summary>${ summary } </summary>\n\n${ content } \n\n</details>` ;
10687
107- async function main ( ) {
108- const [ baseStats , headStats ] = await Promise . all (
109- [ BASE , HEAD ] . map ( getDirectoryStats )
110- ) ;
88+ const [ baseStats , headStats ] = await Promise . all (
89+ [ BASE , HEAD ] . map ( getDirectoryStats )
90+ ) ;
11191
112- const allFiles = Array . from (
113- new Set ( [ ...baseStats . keys ( ) , ...headStats . keys ( ) ] )
114- ) ;
92+ const allFiles = Array . from (
93+ new Set ( [ ...baseStats . keys ( ) , ...headStats . keys ( ) ] )
94+ ) ;
11595
116- if ( ! allFiles . length ) {
117- return ;
118- }
96+ // Filter to only changed files (exist in both and have different sizes)
97+ const changedFiles = allFiles . filter (
98+ f =>
99+ baseStats . has ( f ) &&
100+ headStats . has ( f ) &&
101+ baseStats . get ( f ) !== headStats . get ( f )
102+ ) ;
119103
104+ if ( changedFiles . length ) {
120105 // Separate HTML/JS pairs from other files
121106 const pairs = [ ] ;
122107 const other = [ ] ;
123108 const processed = new Set ( ) ;
124109
125- for ( const file of allFiles ) {
110+ for ( const file of changedFiles ) {
126111 if ( processed . has ( file ) ) {
127112 continue ;
128113 }
129114
130- const basename = getBaseName ( file ) ;
131- const hasHtml = allFiles . some (
132- f => getBaseName ( f ) === basename && getExtension ( f ) === 'html'
115+ const basename = path . basename ( file , path . extname ( file ) ) ;
116+ const hasHtml = changedFiles . some (
117+ f =>
118+ path . basename ( f , path . extname ( f ) ) === basename &&
119+ path . extname ( f ) === '.html'
133120 ) ;
134- const hasJs = allFiles . some (
135- f => getBaseName ( f ) === basename && getExtension ( f ) === 'js'
121+ const hasJs = changedFiles . some (
122+ f =>
123+ path . basename ( f , path . extname ( f ) ) === basename &&
124+ path . extname ( f ) === '.js'
136125 ) ;
137126
138127 if ( hasHtml && hasJs ) {
139- allFiles
140- . filter ( f => getBaseName ( f ) === basename )
128+ changedFiles
129+ . filter ( f => path . basename ( f , path . extname ( f ) ) === basename )
141130 . forEach ( f => {
142131 pairs . push ( f ) ;
143132 processed . add ( f ) ;
@@ -151,29 +140,15 @@ async function main() {
151140 pairs . sort ( ) ;
152141 other . sort ( ) ;
153142
154- // Generate report sections
155- const sections = [ ] ;
143+ console . log ( '## Web Generator\n' ) ;
144+ console . log ( generateTable ( other , baseStats , headStats ) ) ;
156145
157146 if ( pairs . length ) {
158- sections . push (
147+ console . log (
159148 details (
160- `HTML/JS Pairs (${ pairs . length } )` ,
149+ `Pages (${ pairs . length / 2 } )` ,
161150 generateTable ( pairs , baseStats , headStats )
162151 )
163152 ) ;
164153 }
165-
166- if ( other . length ) {
167- sections . push (
168- details (
169- `Other Files (${ other . length } )` ,
170- generateTable ( other , baseStats , headStats )
171- )
172- ) ;
173- }
174-
175- console . log ( '## Web Generator' ) ;
176- console . log ( sections . join ( '\n\n' ) ) ;
177154}
178-
179- main ( ) ;
0 commit comments