Skip to content

Commit

Permalink
Merge branch 'master' into change-column
Browse files Browse the repository at this point in the history
# Conflicts:
#	tests/__snapshots__/utils.spec.js.snap
  • Loading branch information
fisker committed Dec 21, 2020
2 parents 9577be7 + 7d87f60 commit c3268cd
Show file tree
Hide file tree
Showing 10 changed files with 2,534 additions and 2,909 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: CI

on: pull_request_target

jobs:
build_test:
name: Build & Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- run: npm ci
- run: npm run build
- run: npm test
- name: Report Compressed Size
uses: ./
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
pattern: index.js
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Preact

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ inputs:
exclude:
description: 'minimatch pattern of files NOT to track'
default: '{**/*.map,**/node_modules/**}'
cwd:
description: 'A custom working directory to execute the action in relative to repo root (defaults to .)'
runs:
using: 'node12'
main: 'index.js'
16 changes: 1 addition & 15 deletions index.js

Large diffs are not rendered by default.

5,251 changes: 2,414 additions & 2,837 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "compressed-size-action",
"version": "2.0.1",
"version": "2.1.0",
"main": "index.js",
"scripts": {
"build": "microbundle -f cjs --define 'navigator={}' --compress --no-sourcemap --target node src/index.js",
"test": "jest"
},
"license": "MIT",
"devDependencies": {
"@actions/core": "^1.2.2",
"@actions/exec": "^1.0.3",
"@actions/github": "^2.0.1",
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@types/jest": "^26.0.0",
"babel-jest": "^25.5.1",
"jest": "^25.5.2",
"microbundle": "^0.12.0-next.8",
"pretty-bytes": "^5.3.0",
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4",
"@actions/github": "^4.0.0",
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@types/jest": "^26.0.19",
"babel-jest": "^26.6.3",
"jest": "^26.6.3",
"microbundle": "^0.12.4",
"pretty-bytes": "^5.4.1",
"size-plugin-core": "0.0.9"
},
"jest": {
Expand All @@ -35,4 +35,4 @@
]
]
}
}
}
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from 'path';
import { getInput, setFailed, startGroup, endGroup, debug } from '@actions/core';
import { GitHub, context } from '@actions/github';
import { context, getOctokit } from '@actions/github';
import { exec } from '@actions/exec';
import SizePlugin from 'size-plugin-core';
import { fileExists, diffTable, toBool, stripHash } from './utils.js';


async function run(octokit, context, token) {
const { owner, repo, number: pull_number } = context.issue;

Expand All @@ -18,6 +17,8 @@ async function run(octokit, context, token) {
throw Error('Could not retrieve PR information. Only "pull_request" triggered workflows are currently supported.');
}

if (getInput('cwd')) process.chdir(getInput('cwd'));

const plugin = new SizePlugin({
compression: getInput('compression'),
pattern: getInput('pattern') || '**/dist/**/*.js',
Expand Down Expand Up @@ -51,6 +52,9 @@ async function run(octokit, context, token) {
console.log(`Building using ${npm} run ${buildScript}`);
await exec(`${npm} run ${buildScript}`);
endGroup();

// In case the build step alters a JSON-file, ....
await exec(`git reset --hard`);

const newSizes = await plugin.readFromDisk(cwd);

Expand Down Expand Up @@ -94,6 +98,9 @@ async function run(octokit, context, token) {
await exec(`${npm} run ${buildScript}`);
endGroup();

// In case the build step alters a JSON-file, ....
await exec(`git reset --hard`);

const oldSizes = await plugin.readFromDisk(cwd);

const diff = await plugin.getDiff(oldSizes, newSizes);
Expand Down Expand Up @@ -231,7 +238,7 @@ async function createCheck(octokit, context) {
(async () => {
try {
const token = getInput('repo-token', { required: true });
const octokit = new GitHub(token);
const octokit = getOctokit(token);
await run(octokit, context, token);
} catch (e) {
setFailed(e.message);
Expand Down
54 changes: 32 additions & 22 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,40 @@ export function stripHash(regex) {

/**
* @param {number} delta
* @param {number} difference
* @param {number} originalSize
*/
export function getDeltaText(delta, difference) {
export function getDeltaText(delta, originalSize) {
let deltaText = (delta > 0 ? '+' : '') + prettyBytes(delta);
if (delta && Math.abs(delta) > 1) {
deltaText += ` (${Math.abs(difference)}%)`;
if (Math.abs(delta) === 0) {
// only print size
} else if (originalSize === 0) {
deltaText += ` (new file)`;
} else if (originalSize === -delta) {
deltaText += ` (removed)`;
} else {
const percentage = Math.round((delta / originalSize) * 100);
deltaText += ` (${percentage > 0 ? '+' : ''}${percentage}%)`;
}
return deltaText;
}

/**
* @param {number} difference
* @param {number} delta
* @param {number} originalSize
*/
export function iconForDifference(difference) {
let icon = '';
if (difference >= 50) icon = '🆘';
else if (difference >= 20) icon = '🚨';
else if (difference >= 10) icon = '⚠️';
else if (difference >= 5) icon = '🔍';
else if (difference <= -50) icon = '🏆';
else if (difference <= -20) icon = '🎉';
else if (difference <= -10) icon = '👏';
else if (difference <= -5) icon = '✅';
return icon;
export function iconForDifference(delta, originalSize) {
if (originalSize === 0) return '🆕';

const percentage = Math.round((delta / originalSize) * 100);
if (percentage >= 50) return '🆘';
else if (percentage >= 20) return '🚨';
else if (percentage >= 10) return '⚠️';
else if (percentage >= 5) return '🔍';
else if (percentage <= -50) return '🏆';
else if (percentage <= -20) return '🎉';
else if (percentage <= -10) return '👏';
else if (percentage <= -5) return '✅';
return '';
}

/**
Expand Down Expand Up @@ -135,16 +145,16 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
totalSize += size;
totalDelta += delta;

const difference = ((delta / size) * 100) | 0;
const originalSize = size - delta;
const isUnchanged = Math.abs(delta) < minimumChangeThreshold;

if (isUnchanged && omitUnchanged) continue;

const columns = [
`\`${filename}\``,
prettyBytes(size),
getDeltaText(delta, difference),
iconForDifference(difference)
getDeltaText(delta, originalSize),
iconForDifference(delta, originalSize)
];
if (isUnchanged && collapseUnchanged) {
unChangedRows.push(columns);
Expand All @@ -161,9 +171,9 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
}

if (showTotal) {
const totalDifference = ((totalDelta / totalSize) * 100) | 0;
let totalDeltaText = getDeltaText(totalDelta, totalDifference);
let totalIcon = iconForDifference(totalDifference);
const totalOriginalSize = totalSize - totalDelta;
let totalDeltaText = getDeltaText(totalDelta, totalOriginalSize);
let totalIcon = iconForDifference(totalDelta, totalOriginalSize);
out = `**Total Size:** ${prettyBytes(totalSize)}\n\n${out}`;
out = `**Size Change:** ${totalDeltaText} ${totalIcon}\n\n${out}`;
}
Expand Down
32 changes: 16 additions & 16 deletions tests/__snapshots__/utils.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
exports[`diffTable 1`] = `
"**Size Change:** +9 B (0%)
**Total Size:** 4.8 kB
**Total Size:** 14.8 kB
| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (50%) | 🆘 |
| \`two.js\` | -5 kB | -2.5 kB (50%) | 🆘 |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
| \`four.js\` | 4.5 kB | +9 B (0%) | |
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
Expand All @@ -25,8 +25,8 @@ exports[`diffTable 1`] = `
exports[`diffTable 2`] = `
"| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (50%) | 🆘 |
| \`two.js\` | -5 kB | -2.5 kB (50%) | 🆘 |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
| \`four.js\` | 4.5 kB | +9 B (0%) | |
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
Expand All @@ -43,37 +43,37 @@ exports[`diffTable 2`] = `
exports[`diffTable 3`] = `
"**Size Change:** +9 B (0%)
**Total Size:** 4.8 kB
**Total Size:** 14.8 kB
| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (50%) | 🆘 |
| \`two.js\` | -5 kB | -2.5 kB (50%) | 🆘 |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
| \`three.js\` | 300 B | 0 B | |
| \`four.js\` | 4.5 kB | +9 B (0%) | |"
`;

exports[`diffTable 4`] = `
"**Size Change:** +9 B (0%)
**Total Size:** 4.8 kB
**Total Size:** 14.8 kB
| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (50%) | 🆘 |
| \`two.js\` | -5 kB | -2.5 kB (50%) | 🆘 |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
| \`four.js\` | 4.5 kB | +9 B (0%) | |"
`;

exports[`diffTable 5`] = `
"**Size Change:** +9 B (0%)
**Total Size:** 4.8 kB
**Total Size:** 14.8 kB
| Filename | Size | Change | |
| :--- | :---: | :---: | :---: |
| \`one.js\` | 5 kB | +2.5 kB (50%) | 🆘 |
| \`two.js\` | -5 kB | -2.5 kB (50%) | 🆘 |
| \`one.js\` | 5 kB | +2.5 kB (+100%) | 🆘 |
| \`two.js\` | 5 kB | -2.5 kB (-33%) | 🎉 |
<details><summary>ℹ️ <strong>View Unchanged</strong></summary>
Expand All @@ -90,7 +90,7 @@ exports[`diffTable 5`] = `
exports[`diffTable 6`] = `
"**Size Change:** 0 B
**Total Size:** 4.8 kB
**Total Size:** 14.8 kB
Expand All @@ -99,7 +99,7 @@ exports[`diffTable 6`] = `
| Filename | Size |
| :--- | :---: |
| \`one.js\` | 5 kB |
| \`two.js\` | -5 kB |
| \`two.js\` | 5 kB |
| \`three.js\` | 300 B |
| \`four.js\` | 4.5 kB |
Expand Down
11 changes: 7 additions & 4 deletions tests/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ test('toBool', () => {
});

test('getDeltaText', () => {
expect(getDeltaText(5000, 25.5)).toBe('+5 kB (25.5%)');
expect(getDeltaText(-5000, -25.5)).toBe('-5 kB (25.5%)');
expect(getDeltaText(5000, 20000)).toBe('+5 kB (+25%)');
expect(getDeltaText(-5000, 20000)).toBe('-5 kB (-25%)');
expect(getDeltaText(210, 0)).toBe('+210 B (new file)');
expect(getDeltaText(0, 0)).toBe('0 B');
});

test('iconForDifference', () => {
expect(iconForDifference(0)).toBe('');
expect(iconForDifference(0, 5000)).toBe('');
expect(iconForDifference(5500, 5000)).toBe('🆘');
expect(iconForDifference(-550, 5000)).toBe('👏');
});

test('diffTable', () => {
Expand All @@ -29,7 +32,7 @@ test('diffTable', () => {
},
{
filename: 'two.js',
size: -5000,
size: 5000,
delta: -2500
},
{
Expand Down

0 comments on commit c3268cd

Please sign in to comment.