1
0
Fork 0

ci: write human readable bundle sizes and specify delta (#20092)

* ci: write human readable sizes and specify delta

* fix: getHumanReadableSize negative values support

* refactor: change formula
This commit is contained in:
Michele Esposito 2023-07-19 17:16:36 +02:00 committed by GitHub
parent 56481dd875
commit 61e88f4bdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 1 deletions

View File

@ -15,6 +15,29 @@ function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getHumanReadableSize(bytes) {
if (!bytes) {
return '0 Bytes';
}
const absBytes = Math.abs(bytes);
const kibibyteSize = 1024;
const magnitudes = ['Bytes', 'KiB', 'MiB'];
let magnitudeIndex = 0;
if (absBytes > Math.pow(kibibyteSize, 2)) {
magnitudeIndex = 2;
} else if (absBytes > kibibyteSize) {
magnitudeIndex = 1;
}
return `${parseFloat(
(bytes / Math.pow(kibibyteSize, magnitudeIndex)).toFixed(2),
)} ${magnitudes[magnitudeIndex]}`;
}
function getPercentageChange(from, to) {
return parseFloat(((to - from) / Math.abs(from)) * 100).toFixed(2);
}
async function start() {
const { GITHUB_COMMENT_TOKEN, CIRCLE_PULL_REQUEST } = process.env;
console.log('CIRCLE_PULL_REQUEST', CIRCLE_PULL_REQUEST);
@ -278,7 +301,11 @@ async function start() {
}, {});
const sizeDiffRows = Object.keys(diffs).map(
(part) => `${part}: ${diffs[part]} bytes`,
(part) =>
`${part}: ${getHumanReadableSize(diffs[part])} (${getPercentageChange(
devSizes[part],
prSizes[part],
)}%)`,
);
const sizeDiffHiddenContent = `<ul>${sizeDiffRows