Skip to content

Commit

Permalink
Added Dangerfile and provide bundle size info (#2608)
Browse files Browse the repository at this point in the history
  • Loading branch information
mAAdhaTTah authored Nov 9, 2020
1 parent 2d3a126 commit 9df20c5
Show file tree
Hide file tree
Showing 5 changed files with 1,039 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/danger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Danger

on:
pull_request:
branches: [ master ]

jobs:
run:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 0
- run: npm ci
- name: Danger
run: npx danger ci
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tests/
*.html
bower.json
composer.json
dangerfile.js
gulpfile.js
.editorconfig
.gitattributes
Expand Down
114 changes: 114 additions & 0 deletions dangerfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const { markdown } = require('danger');
const fs = require('fs').promises;
const gzipSize = require('gzip-size');
const git = require('simple-git/promise')(__dirname).silent(true);

// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
const formatBytes = (bytes, decimals = 2) => {
if (bytes === 0) return '0 Bytes';

const k = 1000;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(k));

return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

const maybePlus = (from, to) => from < to ? "+" : "";

const absDiff = (from, to) => {
if (from === to) {
return formatBytes(0);
}

return `${maybePlus(from, to)}${formatBytes(to - from)}`;
}

const percDiff = (from, to) => {
if (from === to) {
return '0%';
}

return `${maybePlus(from, to)}${
(100 * (to - from) / (from || to)).toFixed(1)
}%`;
}

const getSummary = (rows, totalMasterFileSize, totalFileSize) => {
const numFiles = rows.length;
const maybeS = rows.length > 0 ? 's' : '';
const byteDiff = absDiff(totalMasterFileSize, totalFileSize);
const percentDiff = percDiff(totalMasterFileSize, totalFileSize);

return `A total of ${numFiles} file${maybeS} have changed, with a combined diff of ${byteDiff} (${percentDiff}).`;
}

const getChangedMinifiedFiles = async () => {
const result = await git.diff(['--name-only', '--no-renames', 'master...']);

return result
? result.split(/\r?\n/g).filter(file => file.endsWith('.min.js'))
: [];
};

const run = async () => {
// Check if master exists & check it out if not.
const result = await git.branch(['--list', 'master']);
if (result.all.length === 0) {
await git.branch(['master', 'origin/master']);
}

const minified = await getChangedMinifiedFiles();

if (minified.length === 0) {
markdown(`## No JS Changes`);
return;
}

const rows = [];
let totalFileSize = 0;
let totalMasterFileSize = 0;

for (const file of minified) {
const [fileContents, fileMasterContents] = await Promise.all([
fs.readFile(file, 'utf-8').catch(() => ''),
git.show([`master:${file}`]).catch(() => ''),
]);

const [fileSize, fileMasterSize] = await Promise.all([
gzipSize(fileContents),
gzipSize(fileMasterContents),
]);

totalFileSize += fileSize;
totalMasterFileSize +=fileMasterSize

rows.push([
file,
formatBytes(fileMasterSize),
formatBytes(fileSize),
absDiff(fileMasterSize, fileSize),
percDiff(fileMasterSize, fileSize),
]);
}

markdown(`## JS File Size Changes (gzipped)
${getSummary(rows, totalMasterFileSize, totalFileSize)}
<details>
| file | master | pull | size diff | % diff |
| --- | --- | --- | --- | --- |
${rows.map(row => `| ${row.join(' | ')} |`).join('\n')}
</details>
`);
}

run().catch(err => {
console.error(err);
process.exit(1);
});
Loading

0 comments on commit 9df20c5

Please sign in to comment.