File tree Expand file tree Collapse file tree
packages/wxt/src/core/utils Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { describe , it , expect } from 'vitest' ;
2+ import { getBytesDisplay } from '../fs' ;
3+
4+ describe ( 'FS Utils' , ( ) => {
5+ describe ( 'getBytesDisplay' , ( ) => {
6+ it . each ( [
7+ [ 123 , '123 B' ] ,
8+ [ 999 , '999 B' ] ,
9+ [ 1000 , '1.00 kB' ] ,
10+ [ 3419 , '3.42 kB' ] ,
11+ [ 999994 , '999.99 kB' ] ,
12+ [ 999995 , '1.00 MB' ] ,
13+ [ 123456789 , '123.46 MB' ] ,
14+ ] ) ( 'Converts %d bytes to "%s"' , ( bytes , expected ) => {
15+ expect ( getBytesDisplay ( bytes ) ) . toBe ( expected ) ;
16+ } ) ;
17+ } ) ;
18+ } ) ;
Original file line number Diff line number Diff line change @@ -44,3 +44,9 @@ export async function getPublicFiles(): Promise<string[]> {
4444 } ) ;
4545 return files . map ( unnormalizePath ) ;
4646}
47+
48+ export function getBytesDisplay ( bytes : number ) : string {
49+ if ( bytes < 1000 ) return `${ bytes } B` ;
50+ if ( bytes < 999995 ) return `${ ( bytes / 1e3 ) . toFixed ( 2 ) } kB` ;
51+ return `${ ( bytes / 1e6 ) . toFixed ( 2 ) } MB` ;
52+ }
Original file line number Diff line number Diff line change 11import path from 'node:path' ;
22import { lstat } from 'node:fs/promises' ;
3- import { filesize } from 'filesize ' ;
3+ import { getBytesDisplay } from '../../utils/fs ' ;
44import { printTable } from './printTable' ;
55import { styleText } from 'node:util' ;
66import { TextStyle } from '../../../utils/text-style' ;
@@ -27,7 +27,7 @@ export async function printFileList(
2727 try {
2828 const stats = await lstat ( file ) ;
2929 totalSize += stats . size ;
30- size = String ( filesize ( stats . size ) ) ;
30+ size = getBytesDisplay ( stats . size ) ;
3131 } catch ( ex ) {
3232 wxt . logger . warn ( `Could not get stats of '${ file } ' error: ${ ex } ` ) ;
3333 }
@@ -40,7 +40,7 @@ export async function printFileList(
4040 ) ;
4141
4242 fileRows . push ( [
43- `${ styleText ( 'cyan' , 'Σ Total size:' ) } ${ String ( filesize ( totalSize ) ) } ` ,
43+ `${ styleText ( 'cyan' , 'Σ Total size:' ) } ${ getBytesDisplay ( totalSize ) } ` ,
4444 ] ) ;
4545
4646 printTable ( log , header , fileRows ) ;
You can’t perform that action at this time.
0 commit comments