From 137b6e3d09e6d383213f22acc110aef217871d42 Mon Sep 17 00:00:00 2001 From: Cori Hudson Date: Tue, 20 Sep 2022 14:19:23 -0400 Subject: [PATCH] fix(scripts): add retries, correct formatting for editor retries flaky transifex api calls up to 5 times; converts message with description to plain message for editor translations --- lib/transifex.js | 13 +++++++++++-- package.json | 2 +- scripts/tx-pull-editor.js | 15 ++++++++++++--- scripts/tx-pull-www.js | 37 ++++++++++++++++++++++--------------- 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/lib/transifex.js b/lib/transifex.js index e82f23385..b99977cf0 100644 --- a/lib/transifex.js +++ b/lib/transifex.js @@ -72,8 +72,17 @@ const downloadResource = async function (projectSlug, resourceSlug, localeCode, */ const txPull = async function (project, resource, locale, mode = 'default') { const url = await downloadResource(project, resource, locale, mode); - const buffer = await download(url); - return JSON.parse(buffer.toString()); + let buffer; + for (let i = 0; i < 5; i++) { + try { + buffer = await download(url); + return JSON.parse(buffer.toString()); + } catch (e) { + // eslint-disable-next-line no-console + console.error(`got ${e.message}, retrying after ${i + 1} failed attempt(s)`); + } + } + throw Error('failed to pull after 5 retries'); }; /** diff --git a/package.json b/package.json index 67ce9da99..f6292b7b4 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "dependencies": { "@babel/cli": "^7.1.2", "@babel/core": "^7.1.2", - "@transifex/api": "3.0.0", + "@transifex/api": "4.2.5", "babel-plugin-react-intl": "^3.0.1", "download": "^8.0.0", "transifex": "1.6.6" diff --git a/scripts/tx-pull-editor.js b/scripts/tx-pull-editor.js index fa8aba40a..77c1d014d 100644 --- a/scripts/tx-pull-editor.js +++ b/scripts/tx-pull-editor.js @@ -52,11 +52,20 @@ const getLocaleData = async function (locale) { const pullTranslations = async function () { try { const values = await batchMap(Object.keys(locales), CONCURRENCY_LIMIT, getLocaleData); - const source = values.find(elt => elt.locale === 'en').translations; values.forEach(function (translation) { - validateTranslations({locale: translation.locale, translations: translation.translations}, source); - const file = JSON.stringify(translation.translations, null, 4); + // if translation has message & description, we only want the message + let txs = {}; + for (const key of Object.keys(translation.translations)) { + const tx = translation.translations[key]; + if (tx.message) { + txs[key] = tx.message; + } else { + txs[key] = tx; + } + } + validateTranslations({locale: translation.locale, translations: txs}, source); + const file = JSON.stringify(txs, null, 4); fs.writeFileSync( `${OUTPUT_DIR}/${translation.locale}.json`, file diff --git a/scripts/tx-pull-www.js b/scripts/tx-pull-www.js index b67597d95..1f528acb9 100755 --- a/scripts/tx-pull-www.js +++ b/scripts/tx-pull-www.js @@ -46,21 +46,28 @@ const getLocaleData = async function (item) { const locale = item.locale; const resource = item.resource; let txLocale = localeMap[locale] || locale; - - const translations = await txPull(PROJECT, resource, txLocale); - - const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`; - mkdirp.sync(txOutdir); - const fileName = `${txOutdir}/${locale}.json`; - fs.writeFileSync( - fileName, - JSON.stringify(translations, null, 4) - ); - return { - resource: resource, - locale: locale, - file: fileName - }; + for (let i = 0; i < 5; i++) { + try { + const translations = await txPull(PROJECT, resource, txLocale); + + const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`; + mkdirp.sync(txOutdir); + const fileName = `${txOutdir}/${locale}.json`; + fs.writeFileSync( + fileName, + JSON.stringify(translations, null, 4) + ); + return { + resource: resource, + locale: locale, + file: fileName + }; + } catch (e) { + // eslint-disable-next-line no-console + console.error(`got ${e.message}, retrying after ${i + 1} attempts`); + } + } + throw Error('failed to pull translations after 5 retries'); }; const expandResourceFiles = (resources) => {