From 82421dc129e651d314ce4b73f2310949acf89c9e Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Mon, 12 May 2025 16:18:01 +0000 Subject: [PATCH 1/2] Edit sync_emojis.js --- scripts/sync_emojis.js | 104 ++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 33 deletions(-) diff --git a/scripts/sync_emojis.js b/scripts/sync_emojis.js index 3b5c4629..2813673f 100644 --- a/scripts/sync_emojis.js +++ b/scripts/sync_emojis.js @@ -1,5 +1,6 @@ const axios = require('axios'); const fs = require('fs'); +const path = require('path'); const baseSrcPath = 'https://gitlab.com/gitlab-org/gitlab/-/raw/master'; @@ -7,7 +8,7 @@ const jsonSrcPath = `${baseSrcPath}/fixtures/emojis`; const jsonDestPath = 'src/behaviors/emoji/json'; const jsonFilenames = ['digests.json', 'aliases.json']; -const imgSrcPath = `${baseSrcPath}/public/-/emojis/2`; +const imgSrcPath = `${baseSrcPath}/public/-/emojis/4`; const imgDestPath = 'public/images/emoji'; const createDirIfNotExists = (dirPath) => { @@ -16,36 +17,73 @@ const createDirIfNotExists = (dirPath) => { } }; -createDirIfNotExists(jsonDestPath); - -const jsonFiles = jsonFilenames.map((filename) => { - const file = `${jsonSrcPath}/${filename}`; - - return axios - .get(file) - .then((response) => { - fs.writeFile(`${jsonDestPath}/${filename}`, JSON.stringify(response.data), (err) => { - if (err) throw err; - console.log(`Saved ${filename}`); - }); - return response.data; - }) - .catch((error) => console.log(`Failed copying: ${file}: ${error}`)); -}); - -Promise.all(jsonFiles).then(([digests]) => { - createDirIfNotExists(imgDestPath); - const emojiNames = Object.keys(digests); - emojiNames.forEach((emoji) => { - const srcFile = `${imgSrcPath}/${emoji}.png`; - const destFile = `${imgDestPath}/${emoji}.png`; - - if (!fs.existsSync(destFile)) { - console.log(srcFile) - axios - .get(srcFile, { responseType: 'stream' }) - .then((response) => response.data.pipe(fs.createWriteStream(destFile))) - .catch((error) => console.log(`Failed copying: ${emoji}.png: ${error}`)); +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + +// Retry with exponential backoff +async function fetchWithRetry(url, options = {}, maxRetries = 1) { + let attempt = 0; + let delay = 1000; + + while (attempt < maxRetries) { + try { + return await axios.get(url, options); + } catch (err) { + const status = err.response?.status; + if (status === 429 || (status >= 500 && status < 600)) { + console.warn(`Retrying ${url} (attempt ${attempt + 1}) - status ${status}`); + await sleep(delay); + delay *= 2; + attempt++; + } else { + throw err; + } + } + } + throw new Error(`Failed to fetch ${url} after ${maxRetries} attempts`); +} + +(async () => { + try { + createDirIfNotExists(jsonDestPath); + + const results = await Promise.all( + jsonFilenames.map(async (filename) => { + const file = `${jsonSrcPath}/${filename}`; + const response = await fetchWithRetry(file); + const filePath = path.join(jsonDestPath, filename); + fs.writeFileSync(filePath, JSON.stringify(response.data)); + console.log(`✅ Saved ${filename}`); + return { filename, data: response.data }; + }), + ); + + const digests = results.find((r) => r.filename === 'digests.json')?.data; + if (!digests) { + throw new Error('digests.json not found or failed to load'); + } + + createDirIfNotExists(imgDestPath); + const emojiNames = Object.keys(digests); + + for (const emoji of emojiNames) { + const srcFile = `${imgSrcPath}/${emoji}.png`; + const destFile = path.join(imgDestPath, `${emoji}.png`); + + if (!fs.existsSync(destFile)) { + const response = await fetchWithRetry(srcFile, { responseType: 'stream' }); + await new Promise((resolve, reject) => { + const stream = fs.createWriteStream(destFile); + response.data.pipe(stream); + stream.on('finish', () => { + console.log(`✅ Saved ${emoji}.png`); + resolve(); + }); + stream.on('error', (err) => reject(err)); + }); + } } - }); -}) + } catch (error) { + console.error(`❌ Error encountered: ${error.message}`); + process.exit(1); + } +})(); -- GitLab From 9ec00c75fc2aba13d44ad1b3b593a76033493ea4 Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Mon, 12 May 2025 16:25:42 +0000 Subject: [PATCH 2/2] Edit sync_emojis.js --- scripts/sync_emojis.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/sync_emojis.js b/scripts/sync_emojis.js index 2813673f..2b0e07ed 100644 --- a/scripts/sync_emojis.js +++ b/scripts/sync_emojis.js @@ -84,6 +84,5 @@ async function fetchWithRetry(url, options = {}, maxRetries = 1) { } } catch (error) { console.error(`❌ Error encountered: ${error.message}`); - process.exit(1); } })(); -- GitLab