Skip to content

Commit 648fcbf

Browse files
committed
chore: More CI debugging
1 parent aebfdfe commit 648fcbf

File tree

4 files changed

+109
-29
lines changed

4 files changed

+109
-29
lines changed

package-lock.json

Lines changed: 41 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"@babel/preset-env": "^7.29.0",
1616
"@rschristian/size-plugin": "^0.1.0",
1717
"@types/jest": "^30.0.0",
18+
"@types/node": "^24.0.0",
1819
"babel-jest": "^30.2.0",
20+
"brotli-size": "^4.0.0",
21+
"gzip-size": "^7.0.0",
1922
"jest": "^30.2.0",
2023
"microbundle": "^0.15.1",
2124
"pretty-bytes": "^5.4.1"

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getInput, setFailed, startGroup, endGroup, debug } from '@actions/core'
22
import { context, getOctokit } from '@actions/github';
33
import { exec } from '@actions/exec';
44
import { SizePlugin } from '@rschristian/size-plugin';
5-
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash, getSortOrder } from './utils.js';
5+
import { getPackageManagerAndInstallScript, diffTable, toBool, stripHash, getSortOrder, readFromDisk } from './utils.js';
66

77
/**
88
* @typedef {ReturnType<typeof import("@actions/github").getOctokit>} Octokit
@@ -68,7 +68,15 @@ async function run(octokit, context, token) {
6868
await exec(`git reset --hard`);
6969

7070
const newSizes = await plugin.readFromDisk(cwd);
71+
const tmpNewSizes = await readFromDisk(
72+
cwd,
73+
/** @type {'gzip' | 'brotli'} */ (getInput('compression')),
74+
getInput('pattern') || '**/dist/**/*.{js,mjs,cjs}',
75+
getInput('exclude') || '{**/*.map,**/node_modules/**}',
76+
stripHash(getInput('strip-hash'))
77+
);
7178
console.log('newSizes', newSizes);
79+
console.log('tmpNewSizes', tmpNewSizes);
7280

7381
startGroup(`[base] Checkout target branch`);
7482
try {

src/utils.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import fs from 'fs';
22
import path from 'path';
33
import prettyBytes from 'pretty-bytes';
44

5+
import { gzipSize } from 'gzip-size';
6+
import { sync as brotliSize } from 'brotli-size';
7+
58
/**
69
* @param {string} cwd
710
* @returns {Promise<{ packageManager: string, installScript: string }>}
@@ -219,7 +222,7 @@ export function diffTable(files, { showTotal, collapseUnchanged, omitUnchanged,
219222
}
220223

221224
let out = '';
222-
225+
223226
if (changedRows.length !== 0) {
224227
const outChanged = markdownTable(changedRows);
225228
out = `<details open><summary>📦 <strong>View Changed</strong></summary>\n\n${outChanged}\n\n</details>`;
@@ -264,3 +267,55 @@ export function getSortOrder(sortBy) {
264267
console.warn(`Invalid 'order-by' value '${sortBy}', defaulting to 'Filename:asc'`);
265268
return 'Filename:asc';
266269
}
270+
271+
/**
272+
* @param {string} cwd
273+
* @param {'gzip' | 'brotli' | 'none'} compression
274+
* @param {string} pattern
275+
* @param {string} [exclude]
276+
* @param {function} [stripHash]
277+
* @returns {Promise<Record<string, number>>}
278+
*/
279+
export async function readFromDisk(cwd, compression, pattern, exclude, stripHash) {
280+
const files = fs.globSync(pattern, {
281+
cwd,
282+
exclude: exclude ? [exclude] : undefined,
283+
});
284+
console.log('Found files', files);
285+
286+
/** @type {Record<string, number>} */
287+
const result = {};
288+
await Promise.all(
289+
files.map(async (file) => {
290+
try {
291+
const fileContents = await fs.promises.readFile(path.join(cwd, file), 'utf-8');
292+
const size = await compressContent(compression, fileContents);
293+
result[stripHash(file)] = size;
294+
} catch (err) {
295+
console.log('Error reading file', file, err);
296+
}
297+
}),
298+
);
299+
300+
return result;
301+
};
302+
303+
/**
304+
* @param {string} input
305+
*/
306+
const noneSize = (input) => Buffer.byteLength(input);
307+
308+
const compressionMethods = {
309+
brotli: brotliSize,
310+
gzip: gzipSize,
311+
none: noneSize,
312+
};
313+
314+
/**
315+
* @param {'gzip' | 'brotli' | 'none'} method
316+
* @param {string} content
317+
* @return {Promise<number>}
318+
*/
319+
export async function compressContent(method, content) {
320+
return await compressionMethods[method](content);
321+
}

0 commit comments

Comments
 (0)