@@ -2,6 +2,9 @@ import fs from 'fs';
22import path from 'path' ;
33import prettyBytes from 'pretty-bytes' ;
44
5+ import { gzipSize } from 'gzip-size' ;
6+ import { sync as brotliSize } from 'brotli-size' ;
7+
58/**
69 * @param {string } cwd
710 * @returns {Promise<{ packageManager: string, installScript: string }> }
@@ -219,7 +222,7 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
219222 }
220223
221224 let out = '' ;
222-
225+
223226 if ( changedRows . length !== 0 ) {
224227 const outChanged = markdownTable ( changedRows ) ;
225228 out = `<details open><summary>📦 <strong>View Changed</strong></summary>\n\n${ outChanged } \n\n</details>` ;
@@ -264,3 +267,55 @@ export function getSortOrder(sortBy) {
264267 console . warn ( `Invalid 'order-by' value '${ sortBy } ', defaulting to 'Filename:asc'` ) ;
265268 return 'Filename:asc' ;
266269}
270+
271+ /**
272+ * @param {string } cwd
273+ * @param {'gzip' | 'brotli' | 'none' } compression
274+ * @param {string } pattern
275+ * @param {string } [exclude]
276+ * @param {function } [stripHash]
277+ * @returns {Promise<Record<string, number>> }
278+ */
279+ export async function readFromDisk ( cwd , compression , pattern , exclude , stripHash ) {
280+ const files = fs . globSync ( pattern , {
281+ cwd,
282+ exclude : exclude ? [ exclude ] : undefined ,
283+ } ) ;
284+ console . log ( 'Found files' , files ) ;
285+
286+ /** @type {Record<string, number> } */
287+ const result = { } ;
288+ await Promise . all (
289+ files . map ( async ( file ) => {
290+ try {
291+ const fileContents = await fs . promises . readFile ( path . join ( cwd , file ) , 'utf-8' ) ;
292+ const size = await compressContent ( compression , fileContents ) ;
293+ result [ stripHash ( file ) ] = size ;
294+ } catch ( err ) {
295+ console . log ( 'Error reading file' , file , err ) ;
296+ }
297+ } ) ,
298+ ) ;
299+
300+ return result ;
301+ } ;
302+
303+ /**
304+ * @param {string } input
305+ */
306+ const noneSize = ( input ) => Buffer . byteLength ( input ) ;
307+
308+ const compressionMethods = {
309+ brotli : brotliSize ,
310+ gzip : gzipSize ,
311+ none : noneSize ,
312+ } ;
313+
314+ /**
315+ * @param {'gzip' | 'brotli' | 'none' } method
316+ * @param {string } content
317+ * @return {Promise<number> }
318+ */
319+ export async function compressContent ( method , content ) {
320+ return await compressionMethods [ method ] ( content ) ;
321+ }
0 commit comments