Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dangerfile and provide bundle size info #2608

Merged
merged 1 commit into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
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)
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
}%`;
}

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']);
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
}

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