diff --git a/package.json b/package.json index b176d1e6..7f91e1c6 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/buildSteps.ts b/src/buildSteps.ts index 1a3dcf83..f2da81d9 100644 --- a/src/buildSteps.ts +++ b/src/buildSteps.ts @@ -1,5 +1,5 @@ import { TaskName } from 'chromatic/node'; -import { filesize } from 'filesize'; +import { formatBytesObject } from './utils/formatBytes'; import { KnownStep, LocalBuildProgress, StepProgressPayload } from './types'; @@ -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}`; diff --git a/src/utils/formatBytes.ts b/src/utils/formatBytes.ts new file mode 100644 index 00000000..6f57451a --- /dev/null +++ b/src/utils/formatBytes.ts @@ -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 }; +} diff --git a/yarn.lock b/yarn.lock index 8717fe6c..4cb26281 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"