Skip to content

Commit d95b34e

Browse files
authored
chore(deps): Replace filesize with DIY function (#2589)
1 parent 55b79e8 commit d95b34e

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
});

packages/wxt/src/core/utils/fs.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

packages/wxt/src/core/utils/log/printFileList.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22
import { lstat } from 'node:fs/promises';
3-
import { filesize } from 'filesize';
3+
import { getBytesDisplay } from '../../utils/fs';
44
import { printTable } from './printTable';
55
import { styleText } from 'node:util';
66
import { 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);

0 commit comments

Comments
 (0)