Skip to content

Commit

Permalink
fix(scripts): add retries, correct formatting for editor
Browse files Browse the repository at this point in the history
retries flaky transifex api calls up to 5 times; converts message with description to plain message for editor translations
  • Loading branch information
Cori Hudson committed Sep 20, 2022
1 parent dd2ed99 commit 137b6e3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
13 changes: 11 additions & 2 deletions lib/transifex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
};

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 12 additions & 3 deletions scripts/tx-pull-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 22 additions & 15 deletions scripts/tx-pull-www.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 137b6e3

Please sign in to comment.