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
This commit is contained in:
Cori Hudson 2022-09-20 14:19:23 -04:00
parent dd2ed99ece
commit 137b6e3d09
4 changed files with 46 additions and 21 deletions

View file

@ -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);
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');
};
/**

View file

@ -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"

View file

@ -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

View file

@ -46,7 +46,8 @@ const getLocaleData = async function (item) {
const locale = item.locale;
const resource = item.resource;
let txLocale = localeMap[locale] || locale;
for (let i = 0; i < 5; i++) {
try {
const translations = await txPull(PROJECT, resource, txLocale);
const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`;
@ -61,6 +62,12 @@ const getLocaleData = async function (item) {
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) => {