Skip to content

Commit

Permalink
Use transifex .download() to download
Browse files Browse the repository at this point in the history
Undo tr change
  • Loading branch information
seotts authored and Cori Hudson committed Sep 20, 2022
1 parent 842704f commit dd2ed99
Show file tree
Hide file tree
Showing 6 changed files with 5,230 additions and 3,833 deletions.
17 changes: 17 additions & 0 deletions lib/batch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Maps each value of an array into an async function, and returns an array of the results
* @param {array} arr - array of values
* @param {number} batchSize - number of calls to `func` to do at one time
* @param {function} func - async function to apply to all items in `arr`. Function should take one argument.
* @return {array} - results of `func` applied to each item in `arr`
*/
exports.batchMap = async (arr, batchSize, func) => {
const results = [];
for (let i = 0; i < arr.length; i += batchSize) {
const result = await Promise.all( // eslint-disable-line
arr.slice(i, i + batchSize).map(func)
);
results.push(...result);
}
return results;
};
98 changes: 14 additions & 84 deletions lib/transifex.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* TODO: add functions for pushing to Transifex
*/

import {transifexApi} from '@transifex/api';
import download from 'download';
const transifexApi = require('@transifex/api').transifexApi;
const download = require('download');

const ORG_NAME = 'llk';
const SOURCE_LOCALE = 'en';
Expand All @@ -23,74 +23,15 @@ try {
throw err;
}

const timeout = async function (ms) {
return new Promise(r => setTimeout(r, ms)); // eslint-disable-line no-undef
};

/**
* Wait until a download is ready, and then get the location of the file to download.
* In order to do this, we need to check the download status until it 303s. Once it 303s,
* we can get the download location from the header.
* @param {*} downloadEventId - id of the transifex download event
* @param {*} isSourceLocale - whether the locale is the same as the source locale
* @returns {string} - url of file that is ready to download
*/
const getDownloadFileLocation = async function (downloadEventId, isSourceLocale) {
const statusEndpoint = isSourceLocale ?
transifexApi.ResourceStringAsyncDownload :
transifexApi.ResourceTranslationAsyncDownload;

let waitMs = 100;

// eslint-disable-next-line no-constant-condition
while (true) {
try {
const downloadStatus = await statusEndpoint.get(downloadEventId);

if (downloadStatus.attributes.status === 'failed') {
if (downloadStatus.attributes.errors.length() > 0) {
throw new Error(downloadStatus.attributes.errors);
}
throw new Error('Error downloading file');
}

/** If there is no error from getting the status, the download is still pending.
Wait before trying again. **/
await timeout(waitMs);

// exponentially increase wait time
waitMs = waitMs * 2;
} catch (err) {
/** status 303 means the download event is complete and the
resource can be downloaded at the location uri. **/
if (err.response.status === 303) {
return err.response.headers.location;
}
throw err;
}
}
};

/**
* Given a download event id, waits for the event to complete and then downloads the file
* @param {string} downloadEventId - id of the transifex download event
* @param {boolean} isSourceLocale - whether the locale is the same as the source locale
* @returns {object} - buffer of downloaded file
*/
const downloadFile = async function (downloadEventId, isSourceLocale) {
const location = await getDownloadFileLocation(downloadEventId, isSourceLocale);
return await download(location);
};

/**
* Creates a download event for a specific project, resource, and locale.
* @param {string} projectSlug - project slug (for example, "scratch-editor")
* @param {string} resourceSlug - resource slug (for example, "blocks")
* @param {string} localeCode - language code (for example, "ko")
* @param {string} mode - translation status of strings to include (defaults to "reviewed")
* @param {string} mode - translation status of strings to include
* @returns {string} - id of the created download event
*/
const createDownloadEvent = async function (projectSlug, resourceSlug, localeCode, mode) {
const downloadResource = async function (projectSlug, resourceSlug, localeCode, mode = 'default') {
const resource = {
data: {
id: `o:${ORG_NAME}:p:${projectSlug}:r:${resourceSlug}`,
Expand All @@ -100,12 +41,9 @@ const createDownloadEvent = async function (projectSlug, resourceSlug, localeCod

// if locale is English, create a download event of the source file
if (localeCode === SOURCE_LOCALE) {
const sourceDownloadEvent = await transifexApi.ResourceStringAsyncDownload.create({
attributes: {file_type: 'default'},
relationships: {resource}
return await transifexApi.ResourceStringsAsyncDownload.download({
resource
});

return sourceDownloadEvent.id;
}

const language = {
Expand All @@ -116,12 +54,11 @@ const createDownloadEvent = async function (projectSlug, resourceSlug, localeCod
};

// if locale is not English, create a download event of the translation file
const downloadEvent = await transifexApi.ResourceTranslationAsyncDownload.create({
attributes: {mode: mode},
relationships: {resource, language}
return await transifexApi.ResourceTranslationsAsyncDownload.download({
mode,
resource,
language
});

return downloadEvent.id;
};

/**
Expand All @@ -134,16 +71,9 @@ const createDownloadEvent = async function (projectSlug, resourceSlug, localeCod
* strings, if the local is the source language)
*/
const txPull = async function (project, resource, locale, mode = 'default') {
try {
const downloadId = await createDownloadEvent(project, resource, locale, mode);
const buffer = await downloadFile(downloadId, locale === SOURCE_LOCALE);
return JSON.parse(buffer.toString());
} catch (err) {
if (err.statusCode === 409) {
throw new Error({statusCode: 409, message: 'translation does not exist for ' + locale});
}
throw err;
}
const url = await downloadResource(project, resource, locale, mode);
const buffer = await download(url);
return JSON.parse(buffer.toString());
};

/**
Expand All @@ -166,4 +96,4 @@ const txResources = async function (project) {
return slugs;
};

export {txPull, txResources};
module.exports = {txPull, txResources};
Loading

0 comments on commit dd2ed99

Please sign in to comment.