Skip to content

Commit

Permalink
feat: hide-files (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Dec 7, 2020
1 parent 20830ca commit 8713f6d
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pkg-size-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ jobs:
uses: ./
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
hide-files: '*.js.map'
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: 'Which property to sort by: delta (size difference), headSize, baseSize, path'
sort-order:
description: 'Sort order: desc, asc'
hide-files:
description: 'Glob pattern to hide files with'
runs:
using: 'node12'
main: 'dist/index.js'
176 changes: 172 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"lodash-es": "^4.17.15",
"markdown-table": "^2.0.0",
"outdent": "^0.7.1",
"xo": "^0.35.0"
"xo": "^0.35.0",
"glob-to-regexp": "^0.4.1"
}
}
33 changes: 29 additions & 4 deletions src/generate-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import byteSize from 'byte-size';
import {partition, round} from 'lodash-es';
import markdownTable from 'markdown-table';
import outdent from 'outdent';
import globToRegExp from 'glob-to-regexp';
import {c, link, sub, sup} from './markdown-utils';

const percent = fraction => {
Expand Down Expand Up @@ -76,6 +77,7 @@ function processFiles(fileMap, type, sizeData) {
function generateComment({
commentSignature,
unchangedFiles,
hideFiles,
sortBy,
sortOrder,
baseSizeData,
Expand All @@ -84,15 +86,21 @@ function generateComment({
const fileMap = {};
const baseTotalSize = processFiles(fileMap, 'baseSize', baseSizeData);
const headTotalSize = processFiles(fileMap, 'headSize', headSizeData);
const totalDelta = delta(baseTotalSize, headTotalSize);

const files = Object.values(fileMap);
let files = Object.values(fileMap);
files.sort((a, b) => (b[sortBy] - a[sortBy]) || (a.path.localeCompare(b.path)));
if (sortOrder === 'asc') {
files.reverse();
}

let hidden = [];
if (hideFiles) {
const hideFilesPtrn = globToRegExp(hideFiles);
[hidden, files] = partition(files, fileData => hideFilesPtrn.test(fileData.path));
}

const [unchanged, changed] = partition(files, fileData => (fileData.baseSize === fileData.headSize));
const totalDelta = delta(baseTotalSize, headTotalSize);

const table = markdownTable([
['File', 'Before', 'After'],
Expand All @@ -107,7 +115,7 @@ function generateComment({
) : '—',
]),
[
'**Total** ' + (unchangedFiles === 'show' ? '' : sub('_(Includes unchanged files)_')),
'**Total** ' + (unchangedFiles === 'show' ? '' : sub('_(Includes all files)_')),
c(byteSize(baseTotalSize)),
sup(totalDelta) + c(byteSize(headTotalSize)),
],
Expand All @@ -116,7 +124,7 @@ function generateComment({
});

let unchangedTable = '';
if (unchangedFiles === 'collapse') {
if (unchangedFiles === 'collapse' && unchanged.length > 0) {
unchangedTable = markdownTable([
['File', 'Size'],
...unchanged.map(data => [
Expand All @@ -130,6 +138,21 @@ function generateComment({
unchangedTable = `<details><summary>Unchanged files</summary>\n\n${unchangedTable}\n</details>`;
}

let hiddenTable = '';
if (hidden.length > 0) {
hiddenTable = markdownTable([
['File', 'Size'],
...hidden.map(data => [
data.link,
c(byteSize(data.baseSize)),
]),
], {
align: ['', 'r'],
});

hiddenTable = `<details><summary>Hidden files</summary>\n\n${hiddenTable}\n</details>`;
}

return outdent`
### 📊 Package size report&nbsp;&nbsp;&nbsp;<kbd>${totalDelta || 'No changes'}</kbd>
Expand All @@ -139,6 +162,8 @@ function generateComment({
${unchangedTable}
${hiddenTable}
${commentSignature}
`;
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ async function buildRef({
const buildCommand = core.getInput('build-command');
const commentReport = core.getInput('comment-report');
const unchangedFiles = core.getInput('unchanged-files') || 'collapse';
const hideFiles = core.getInput('hide-files');
const sortBy = core.getInput('sort-by') || 'delta';
const sortOrder = core.getInput('sort-order') || 'desc';

Expand Down Expand Up @@ -179,6 +180,7 @@ async function buildRef({
body: generateComment({
commentSignature: COMMENT_SIGNATURE,
unchangedFiles,
hideFiles,
sortBy,
sortOrder,
baseSizeData,
Expand Down
Loading

0 comments on commit 8713f6d

Please sign in to comment.