@codechecks/build-size-watcher
Version:
Keep your build size in check and detect when it gets too big
57 lines • 2.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const bytes = require("bytes");
const lodash_1 = require("lodash");
function getReportFromDiff(diff, originalFiles, options) {
const originalFilesByPath = lodash_1.groupBy(originalFiles, "path");
const shortDescription = `Change: ${renderSize(diff.totalSizeChange, diff.totalSizeChangeFraction)} Total: ${bytes(diff.totalSize)}`;
const reportKeys = lodash_1.sortBy(Object.keys(diff.files), k => {
const value = diff.files[k];
const typeVal = value.type === "changed" ? 1 : value.type === "new" ? 2 : 3;
return typeVal * 10 + k;
});
const longDescription = `
| Status | Files | Now | Diff | Max |
|:------:|:-----:|:---:|:----:|:---:|
${reportKeys
.map(fk => {
const f = diff.files[fk];
const maxSize = originalFilesByPath[fk] && originalFilesByPath[fk][0].maxSize;
// prettier-ignore
return `| ${renderStatus(f.type, maxSize ? f.overallSize > maxSize : false)} | ${fk} | ${bytes(f.overallSize)} | ${renderSize(f.sizeChange, f.sizeChangeFraction)} | ${maxSize ? bytes(maxSize) : " — "} |`;
})
.join("\n")}
`;
const shouldFail = originalFiles
.filter(file => file.maxSize)
.reduce((result, file) => result || diff.files[file.path] ? diff.files[file.path].overallSize > file.maxSize : false, false);
return {
name: options.name,
shortDescription,
longDescription,
status: shouldFail ? "failure" : "success",
};
}
exports.getReportFromDiff = getReportFromDiff;
function renderStatus(type, reachedMaxSize) {
if (reachedMaxSize) {
return "🛑 Max size reached";
}
return type;
}
function renderSign(value) {
if (value > 0) {
return "+";
}
else {
// we dont' render signs for negative (it's part of a number xD) or 0
return "";
}
}
function renderFraction(value) {
return (value * 100).toFixed(2) + "%";
}
function renderSize(size, fraction) {
return `${renderSign(size) + bytes(size)} (${renderSign(fraction) + renderFraction(fraction)})`;
}
//# sourceMappingURL=getReportFromDiff.js.map