Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"dependencies": {
"@neoconfetti/react": "^1.0.0",
"chromatic": "^13.3.3",
"filesize": "^10.0.12",
"jsonfile": "^6.1.0",
"strip-ansi": "^7.1.0"
},
Expand Down
12 changes: 4 additions & 8 deletions src/buildSteps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TaskName } from 'chromatic/node';
import { filesize } from 'filesize';
import { formatBytesObject } from './utils/formatBytes';

import { KnownStep, LocalBuildProgress, StepProgressPayload } from './types';

Expand Down Expand Up @@ -52,13 +52,9 @@ export const BUILD_STEP_CONFIG: Record<
renderProgress: ({ stepProgress }) => {
const { numerator, denominator } = stepProgress.upload;
if (!denominator || !numerator) return `Uploading files...`;
const { value: total, exponent } = filesize(denominator, {
output: 'object',
round: 1,
});
const { value: progress, symbol } = filesize(numerator, {
exponent,
output: 'object',
const { value: total, exponent } = formatBytesObject(denominator, { round: 1 });
const { value: progress, symbol } = formatBytesObject(numerator, {
exponent: exponent,
round: 1,
});
return `Uploading files... ${progress}/${total} ${symbol}`;
Expand Down
26 changes: 26 additions & 0 deletions src/utils/formatBytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type FormatBytesObject = {
value: number;
symbol: string;
exponent: number;
};

export function formatBytesObject(
bytes: number,
options?: { exponent?: number; round?: number }
): FormatBytesObject {
const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const round = options?.round ?? 1;

if (!Number.isFinite(bytes) || bytes <= 0) {
return { value: 0, symbol: UNITS[0], exponent: 0 };
}

const computedExp = Math.min(UNITS.length - 1, Math.max(0, Math.floor(Math.log10(bytes) / 3)));
const exp = Math.min(UNITS.length - 1, Math.max(0, options?.exponent ?? computedExp));

const value = bytes / Math.pow(1000, exp);
const factor = Math.pow(10, round);
const rounded = Math.round(value * factor) / factor;

return { value: rounded, symbol: UNITS[exp], exponent: exp };
}
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ __metadata:
eslint-plugin-react-hooks: "npm:^5.0.0"
eslint-plugin-simple-import-sort: "npm:^12.1.1"
eslint-plugin-storybook: "npm:^9.0.0"
filesize: "npm:^10.0.12"
graphql: "npm:^16.8.1"
jsonfile: "npm:^6.1.0"
msw: "npm:^2.0.0"
Expand Down Expand Up @@ -5345,13 +5344,6 @@ __metadata:
languageName: node
linkType: hard

"filesize@npm:^10.0.12":
version: 10.1.6
resolution: "filesize@npm:10.1.6"
checksum: 10c0/9a196d64da4e947b8c0d294be09a3dfa7a634434a1fc5fb3465f1c9acc1237ea0363f245ba6e24477ea612754d942bc964d86e0e500905a72e9e0e17ae1bbdbc
languageName: node
linkType: hard

"fill-range@npm:^7.1.1":
version: 7.1.1
resolution: "fill-range@npm:7.1.1"
Expand Down