Skip to content

Commit

Permalink
Build bundles without glob
Browse files Browse the repository at this point in the history
  • Loading branch information
Gudahtt committed Dec 20, 2024
1 parent 24ae398 commit a4343e7
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions development/metamaskbot-build-announce.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const path = require('path');
// Fetch is part of node js in future versions, thus triggering no-shadow
// eslint-disable-next-line no-shadow
const fetch = require('node-fetch');
const glob = require('fast-glob');
const VERSION = require('../package.json').version;
const { getHighlights } = require('./highlights');

Expand Down Expand Up @@ -38,6 +37,17 @@ function getPercentageChange(from, to) {
return parseFloat(((to - from) / Math.abs(from)) * 100).toFixed(2);
}

/**
* Check whether an artifact exists,
*
* @param {string} url - The URL of the artifact to check.
* @returns True if the artifact exists, false if it doesn't
*/
async function artifactExists(url) {
const response = await fetch(url, { method: 'HEAD' });
return response.ok;
}

async function start() {
const {
PR_COMMENT_TOKEN,
Expand Down Expand Up @@ -111,30 +121,26 @@ async function start() {

// links to bundle browser builds
const bundles = {};
const fileType = '.html';
const sourceMapRoot = '/build-artifacts/source-map-explorer/';
const bundleFiles = await glob(`.${sourceMapRoot}*${fileType}`);

bundleFiles.forEach((bundleFile) => {
const fileName = bundleFile.split(sourceMapRoot)[1];
const bundleName = fileName.split(fileType)[0];
const url = `${BUILD_LINK_BASE}${sourceMapRoot}${fileName}`;
let fileRoot = bundleName;
let fileIndex = bundleName.match(/-[0-9]{1,}$/u)?.index;

if (fileIndex) {
fileRoot = bundleName.slice(0, fileIndex);
fileIndex = bundleName.slice(fileIndex + 1, bundleName.length);
}

const link = `<a href="${url}">${fileIndex || fileRoot}</a>`;
const fileRoots = [
'background',
'common',
'ui',
'content-script',
'offscreen',
];

if (fileRoot in bundles) {
for (const fileRoot of fileRoots) {
let fileIndex = 0;
let url = `${BUILD_LINK_BASE}${sourceMapRoot}${fileRoot}-${fileIndex}.html`;
while (await artifactExists(url)) {
const link = `<a href="${url}">${fileIndex}</a>`;
bundles[fileRoot].push(link);
} else {
bundles[fileRoot] = [link];

fileIndex += 1;
url = `${BUILD_LINK_BASE}${sourceMapRoot}${fileRoot}-${fileIndex}.html`;
}
});
}

const bundleMarkup = `<ul>${Object.keys(bundles)
.map((key) => `<li>${key}: ${bundles[key].join(', ')}</li>`)
Expand Down

0 comments on commit a4343e7

Please sign in to comment.