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 txPull = async function (project, resource, locale, mode = 'default') {
const url = await downloadResource(project, resource, locale, mode); const url = await downloadResource(project, resource, locale, mode);
const buffer = await download(url); let buffer;
return JSON.parse(buffer.toString()); 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": { "dependencies": {
"@babel/cli": "^7.1.2", "@babel/cli": "^7.1.2",
"@babel/core": "^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", "babel-plugin-react-intl": "^3.0.1",
"download": "^8.0.0", "download": "^8.0.0",
"transifex": "1.6.6" "transifex": "1.6.6"

View file

@ -52,11 +52,20 @@ const getLocaleData = async function (locale) {
const pullTranslations = async function () { const pullTranslations = async function () {
try { try {
const values = await batchMap(Object.keys(locales), CONCURRENCY_LIMIT, getLocaleData); const values = await batchMap(Object.keys(locales), CONCURRENCY_LIMIT, getLocaleData);
const source = values.find(elt => elt.locale === 'en').translations; const source = values.find(elt => elt.locale === 'en').translations;
values.forEach(function (translation) { values.forEach(function (translation) {
validateTranslations({locale: translation.locale, translations: translation.translations}, source); // if translation has message & description, we only want the message
const file = JSON.stringify(translation.translations, null, 4); 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( fs.writeFileSync(
`${OUTPUT_DIR}/${translation.locale}.json`, `${OUTPUT_DIR}/${translation.locale}.json`,
file file

View file

@ -46,21 +46,28 @@ const getLocaleData = async function (item) {
const locale = item.locale; const locale = item.locale;
const resource = item.resource; const resource = item.resource;
let txLocale = localeMap[locale] || locale; let txLocale = localeMap[locale] || locale;
for (let i = 0; i < 5; i++) {
const translations = await txPull(PROJECT, resource, txLocale); try {
const translations = await txPull(PROJECT, resource, txLocale);
const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`;
mkdirp.sync(txOutdir); const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`;
const fileName = `${txOutdir}/${locale}.json`; mkdirp.sync(txOutdir);
fs.writeFileSync( const fileName = `${txOutdir}/${locale}.json`;
fileName, fs.writeFileSync(
JSON.stringify(translations, null, 4) fileName,
); JSON.stringify(translations, null, 4)
return { );
resource: resource, return {
locale: locale, resource: resource,
file: fileName 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) => { const expandResourceFiles = (resources) => {