Skip to content

Commit

Permalink
feat: display-size option (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Jan 10, 2021
1 parent a085d68 commit 09eba3a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 34 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,5 @@ jobs:
- `sort-by` Which property to sort by: delta (size difference), headSize, baseSize, path
- `sort-order` Sort order: desc, asc
- `hide-files` Glob pattern to hide files with
- `display-size` Which size to show: uncompressed, gzip, brotli

2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ inputs:
description: 'Sort order: desc, asc'
hide-files:
description: 'Glob pattern to hide files with'
display-size:
description: 'What size to display. Comma delimited list for multiple: uncompressed (default), gzip, brotli:'
runs:
using: 'node12'
main: 'dist/index.js'
76 changes: 59 additions & 17 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6297,39 +6297,76 @@ const directionSymbol = (value) => {
return '';
};

const formatSize = ({ delta, percent }) => (delta ? (percent + directionSymbol(delta)) : '');
const formatDelta = ({ delta, percent }) => (delta ? (percent + directionSymbol(delta)) : '');

const supportedSizes = {
uncompressed: {
label: 'Size',
property: 'size',
},
gzip: {
label: 'Gzip',
property: 'sizeGzip',
},
brotli: {
label: 'Brotli',
property: 'sizeBrotli',
},
};

const listSizes = (displaySizes, callback) => displaySizes
.map(({ property }) => callback(property))
.join(' / ');

function generateComment({
unchangedFiles,
pkgComparisonData,
displaySize,
}) {
const { changed, unchanged, hidden } = pkgComparisonData.files;
const totalDelta = formatSize(pkgComparisonData.diff.size);
const displaySizes = displaySize
.split(',')
.map(s => s.trim())
.filter(s => supportedSizes.hasOwnProperty(s)) // eslint-disable-line no-prototype-builtins
.map(s => supportedSizes[s]);

let sizeHeadingLabel = '';
if (displaySizes.length > 1 || displaySizes[0].property !== 'size') {
sizeHeadingLabel = ` (${displaySizes.map(s => s.label).join(' / ')})`;
}

console.log(JSON.stringify(changed, null, 4));

const table = markdownTable_1([
['File', 'Before', 'After'],
['File', `Before${sizeHeadingLabel}`, `After${sizeHeadingLabel}`],
...[
...changed,
...(unchangedFiles === 'show' ? unchanged : []),
].map(file => [
file.link,
file.base && file.base.size ? c$1(dist(file.base.size)) : '—',
file.base && file.base.size
? listSizes(displaySizes, p => c$1(dist(file.base[p])))
: '—',
file.head && file.head.size
? (
(file.base && file.base.size ? sup(formatSize(file.diff.size)) : '') + c$1(dist(file.head.size))
? listSizes(
displaySizes,
p => (file.base && file.base[p] ? sup(formatDelta(file.diff[p])) : '') + c$1(dist(file.head[p])),
)
: '—',
]),
[
`${strong('Total')} ${(unchangedFiles === 'show' ? '' : sub('_(Includes all files)_'))}`,
c$1(dist(pkgComparisonData.base.size)),
sup(totalDelta) + c$1(dist(pkgComparisonData.head.size)),
listSizes(displaySizes, p => c$1(dist(pkgComparisonData.base[p]))),
listSizes(displaySizes, p => (
sup(formatDelta(pkgComparisonData.diff[p]))
+ c$1(dist(pkgComparisonData.head[p]))
)),
],
[
strong('Tarball size'),
c$1(dist(pkgComparisonData.base.tarballSize)),
(
sup(formatSize(pkgComparisonData.diff.tarballSize))
sup(formatDelta(pkgComparisonData.diff.tarballSize))
+ c$1(dist(pkgComparisonData.head.tarballSize))
),
],
Expand All @@ -6340,10 +6377,10 @@ function generateComment({
let unchangedTable = '';
if (unchangedFiles === 'collapse' && unchanged.length > 0) {
unchangedTable = markdownTable_1([
['File', 'Size'],
['File', `Size${sizeHeadingLabel}`],
...unchanged.map(file => [
file.link,
c$1(dist(file.base.size)),
listSizes(displaySizes, p => c$1(dist(file.base[p]))),
]),
], {
align: ['', 'r'],
Expand All @@ -6355,13 +6392,16 @@ function generateComment({
let hiddenTable = '';
if (hidden.length > 0) {
hiddenTable = markdownTable_1([
['File', 'Before', 'After'],
['File', `Before${sizeHeadingLabel}`, `After${sizeHeadingLabel}`],
...hidden.map(file => [
file.link,
file.base && file.base.size ? c$1(dist(file.base.size)) : '—',
file.base && file.base.size
? listSizes(displaySizes, p => c$1(dist(file.base[p])))
: '—',
file.head && file.head.size
? (
(file.base && file.base.size ? sup(formatSize(file.diff.size)) : '') + c$1(dist(file.head.size))
? listSizes(
displaySizes,
p => (file.base && file.base[p] ? sup(formatDelta(file.diff[p])) : '') + c$1(dist(file.head[p])),
)
: '—',
]),
Expand All @@ -6373,7 +6413,7 @@ function generateComment({
}

return defaultOutdent`
### 📊 Package size report&nbsp;&nbsp;&nbsp;<kbd>${totalDelta || 'No changes'}</kbd>
### 📊 Package size report&nbsp;&nbsp;&nbsp;<kbd>${formatDelta(pkgComparisonData.diff.size) || 'No changes'}</kbd>
${table}
Expand Down Expand Up @@ -10982,6 +11022,7 @@ const COMMENT_SIGNATURE = sub('🤖 This report was automatically generated by [
const hideFiles = core.getInput('hide-files');
const sortBy = core.getInput('sort-by') || 'delta';
const sortOrder = core.getInput('sort-order') || 'desc';
const displaySize = core.getInput('display-size') || 'uncompressed';

core.startGroup('Build HEAD');
const headPkgData = await buildRef({
Expand Down Expand Up @@ -11027,7 +11068,8 @@ const COMMENT_SIGNATURE = sub('🤖 This report was automatically generated by [
prNumber: pr.number,
body: generateComment({
pkgComparisonData,
unchangedFiles, // templateOption
unchangedFiles,
displaySize,
}),
});
}
Expand Down
72 changes: 56 additions & 16 deletions src/comment-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,76 @@ const directionSymbol = (value) => {
return '';
};

const formatSize = ({ delta, percent }) => (delta ? (percent + directionSymbol(delta)) : '');
const formatDelta = ({ delta, percent }) => (delta ? (percent + directionSymbol(delta)) : '');

const supportedSizes = {
uncompressed: {
label: 'Size',
property: 'size',
},
gzip: {
label: 'Gzip',
property: 'sizeGzip',
},
brotli: {
label: 'Brotli',
property: 'sizeBrotli',
},
};

const listSizes = (displaySizes, callback) => displaySizes
.map(({ property }) => callback(property))
.join(' / ');

function generateComment({
unchangedFiles,
pkgComparisonData,
displaySize,
}) {
const { changed, unchanged, hidden } = pkgComparisonData.files;
const totalDelta = formatSize(pkgComparisonData.diff.size);
const displaySizes = displaySize
.split(',')
.map(s => s.trim())
.filter(s => supportedSizes.hasOwnProperty(s)) // eslint-disable-line no-prototype-builtins
.map(s => supportedSizes[s]);

let sizeHeadingLabel = '';
if (displaySizes.length > 1 || displaySizes[0].property !== 'size') {
sizeHeadingLabel = ` (${displaySizes.map(s => s.label).join(' / ')})`;
}

console.log(JSON.stringify(changed, null, 4));

const table = markdownTable([
['File', 'Before', 'After'],
['File', `Before${sizeHeadingLabel}`, `After${sizeHeadingLabel}`],
...[
...changed,
...(unchangedFiles === 'show' ? unchanged : []),
].map(file => [
file.link,
file.base && file.base.size ? c(byteSize(file.base.size)) : '—',
file.base && file.base.size
? listSizes(displaySizes, p => c(byteSize(file.base[p])))
: '—',
file.head && file.head.size
? (
(file.base && file.base.size ? sup(formatSize(file.diff.size)) : '') + c(byteSize(file.head.size))
? listSizes(
displaySizes,
p => (file.base && file.base[p] ? sup(formatDelta(file.diff[p])) : '') + c(byteSize(file.head[p])),
)
: '—',
]),
[
`${strong('Total')} ${(unchangedFiles === 'show' ? '' : sub('_(Includes all files)_'))}`,
c(byteSize(pkgComparisonData.base.size)),
sup(totalDelta) + c(byteSize(pkgComparisonData.head.size)),
listSizes(displaySizes, p => c(byteSize(pkgComparisonData.base[p]))),
listSizes(displaySizes, p => (
sup(formatDelta(pkgComparisonData.diff[p]))
+ c(byteSize(pkgComparisonData.head[p]))
)),
],
[
strong('Tarball size'),
c(byteSize(pkgComparisonData.base.tarballSize)),
(
sup(formatSize(pkgComparisonData.diff.tarballSize))
sup(formatDelta(pkgComparisonData.diff.tarballSize))
+ c(byteSize(pkgComparisonData.head.tarballSize))
),
],
Expand All @@ -60,10 +97,10 @@ function generateComment({
let unchangedTable = '';
if (unchangedFiles === 'collapse' && unchanged.length > 0) {
unchangedTable = markdownTable([
['File', 'Size'],
['File', `Size${sizeHeadingLabel}`],
...unchanged.map(file => [
file.link,
c(byteSize(file.base.size)),
listSizes(displaySizes, p => c(byteSize(file.base[p]))),
]),
], {
align: ['', 'r'],
Expand All @@ -75,13 +112,16 @@ function generateComment({
let hiddenTable = '';
if (hidden.length > 0) {
hiddenTable = markdownTable([
['File', 'Before', 'After'],
['File', `Before${sizeHeadingLabel}`, `After${sizeHeadingLabel}`],
...hidden.map(file => [
file.link,
file.base && file.base.size ? c(byteSize(file.base.size)) : '—',
file.base && file.base.size
? listSizes(displaySizes, p => c(byteSize(file.base[p])))
: '—',
file.head && file.head.size
? (
(file.base && file.base.size ? sup(formatSize(file.diff.size)) : '') + c(byteSize(file.head.size))
? listSizes(
displaySizes,
p => (file.base && file.base[p] ? sup(formatDelta(file.diff[p])) : '') + c(byteSize(file.head[p])),
)
: '—',
]),
Expand All @@ -93,7 +133,7 @@ function generateComment({
}

return outdent`
### 📊 Package size report&nbsp;&nbsp;&nbsp;<kbd>${totalDelta || 'No changes'}</kbd>
### 📊 Package size report&nbsp;&nbsp;&nbsp;<kbd>${formatDelta(pkgComparisonData.diff.size) || 'No changes'}</kbd>
${table}
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const COMMENT_SIGNATURE = sub('🤖 This report was automatically generated by [
const hideFiles = getInput('hide-files');
const sortBy = getInput('sort-by') || 'delta';
const sortOrder = getInput('sort-order') || 'desc';
const displaySize = getInput('display-size') || 'uncompressed';

log.startGroup('Build HEAD');
const headPkgData = await buildRef({
Expand Down Expand Up @@ -68,7 +69,8 @@ const COMMENT_SIGNATURE = sub('🤖 This report was automatically generated by [
prNumber: pr.number,
body: generateComment({
pkgComparisonData,
unchangedFiles, // templateOption
unchangedFiles,
displaySize,
}),
});
}
Expand Down

0 comments on commit 09eba3a

Please sign in to comment.