From 09eba3adb14fc46a1371d5fe681758a536e0009a Mon Sep 17 00:00:00 2001 From: hiroki Date: Sun, 10 Jan 2021 17:01:53 -0500 Subject: [PATCH] feat: display-size option (#18) --- README.md | 2 + action.yml | 2 + dist/index.js | 76 ++++++++++++++++++++++++++-------- src/comment-templates/index.js | 72 +++++++++++++++++++++++++------- src/index.js | 4 +- 5 files changed, 122 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 73ba691..590ce19 100644 --- a/README.md +++ b/README.md @@ -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 + diff --git a/action.yml b/action.yml index 0914f84..2af899a 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/dist/index.js b/dist/index.js index f5724ed..d5519eb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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)) ), ], @@ -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'], @@ -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])), ) : '—', ]), @@ -6373,7 +6413,7 @@ function generateComment({ } return defaultOutdent` - ### 📊 Package size report   ${totalDelta || 'No changes'} + ### 📊 Package size report   ${formatDelta(pkgComparisonData.diff.size) || 'No changes'} ${table} @@ -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({ @@ -11027,7 +11068,8 @@ const COMMENT_SIGNATURE = sub('🤖 This report was automatically generated by [ prNumber: pr.number, body: generateComment({ pkgComparisonData, - unchangedFiles, // templateOption + unchangedFiles, + displaySize, }), }); } diff --git a/src/comment-templates/index.js b/src/comment-templates/index.js index 92bba83..1c695cd 100644 --- a/src/comment-templates/index.js +++ b/src/comment-templates/index.js @@ -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)) ), ], @@ -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'], @@ -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])), ) : '—', ]), @@ -93,7 +133,7 @@ function generateComment({ } return outdent` - ### 📊 Package size report   ${totalDelta || 'No changes'} + ### 📊 Package size report   ${formatDelta(pkgComparisonData.diff.size) || 'No changes'} ${table} diff --git a/src/index.js b/src/index.js index e0b8064..82efd56 100644 --- a/src/index.js +++ b/src/index.js @@ -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({ @@ -68,7 +69,8 @@ const COMMENT_SIGNATURE = sub('🤖 This report was automatically generated by [ prNumber: pr.number, body: generateComment({ pkgComparisonData, - unchangedFiles, // templateOption + unchangedFiles, + displaySize, }), }); }