|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +const transifex = require('./transifex'); |
| 4 | +const util = require('util'); |
| 5 | +const shell = require('shelljs'); |
| 6 | +const fetch = require('node-fetch'); |
| 7 | +const download = require('download'); |
| 8 | + |
| 9 | +const getLanguages = async (organization, project) => { |
| 10 | + const url = transifex.url( |
| 11 | + util.format('projects/o:%s:p:%s/languages', organization, project) |
| 12 | + ); |
| 13 | + const json = await fetch(url, { headers: transifex.authHeader() }) |
| 14 | + .catch(err => { |
| 15 | + shell.echo(err); |
| 16 | + shell.exit(1); |
| 17 | + }) |
| 18 | + .then(res => res.json()); |
| 19 | + let languages = []; |
| 20 | + json['data'].forEach(e => { |
| 21 | + const languageCode = e['attributes']['code']; |
| 22 | + // Skip english since it's the one we generate |
| 23 | + if (languageCode === 'en') { |
| 24 | + return; |
| 25 | + } |
| 26 | + languages.push(languageCode); |
| 27 | + }); |
| 28 | + return languages; |
| 29 | +}; |
| 30 | + |
| 31 | +const requestTranslationDownload = async (relationships) => { |
| 32 | + let url = transifex.url('resource_translations_async_downloads'); |
| 33 | + const data = { |
| 34 | + data: { |
| 35 | + relationships, |
| 36 | + type: 'resource_translations_async_downloads' |
| 37 | + } |
| 38 | + }; |
| 39 | + const headers = transifex.authHeader(); |
| 40 | + headers['Content-Type'] = 'application/vnd.api+json'; |
| 41 | + const json = await fetch(url, { |
| 42 | + method: 'POST', |
| 43 | + headers, |
| 44 | + body: JSON.stringify(data) |
| 45 | + }) |
| 46 | + .catch(err => { |
| 47 | + shell.echo(err); |
| 48 | + shell.exit(1); |
| 49 | + }) |
| 50 | + .then(res => res.json()); |
| 51 | + |
| 52 | + return json['data']['id']; |
| 53 | +}; |
| 54 | + |
| 55 | +const getTranslationDownloadStatus = async (language, downloadRequestId) => { |
| 56 | + // The download request status must be asked from time to time, if it's |
| 57 | + // still pending we try again using exponentional backoff starting from 2.5 seconds. |
| 58 | + let backoffMs = 2500; |
| 59 | + while (true) { |
| 60 | + const url = transifex.url( |
| 61 | + util.format('resource_translations_async_downloads/%s', downloadRequestId) |
| 62 | + ); |
| 63 | + const options = { |
| 64 | + headers: transifex.authHeader(), |
| 65 | + redirect: 'manual' |
| 66 | + }; |
| 67 | + const res = await fetch(url, options).catch(err => { |
| 68 | + shell.echo(err); |
| 69 | + shell.exit(1); |
| 70 | + }); |
| 71 | + |
| 72 | + if (res.status === 303) { |
| 73 | + // When the file to download is ready we get redirected |
| 74 | + return { |
| 75 | + language, |
| 76 | + downloadUrl: res.headers.get('location') |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + const json = await res.json(); |
| 81 | + const downloadStatus = json['data']['attributes']['status']; |
| 82 | + if (downloadStatus == 'pending' || downloadStatus == 'processing') { |
| 83 | + await new Promise(r => setTimeout(r, backoffMs)); |
| 84 | + backoffMs = backoffMs * 2; |
| 85 | + // Retry the download request status again |
| 86 | + continue; |
| 87 | + } else if (downloadStatus == 'failed') { |
| 88 | + const errors = []; |
| 89 | + json['data']['attributes']['errors'].forEach(err => { |
| 90 | + errors.push(util.format('%s: %s', err.code, err.details)); |
| 91 | + }); |
| 92 | + throw util.format('Download request failed: %s', errors.join(', ')); |
| 93 | + } |
| 94 | + throw 'Download request failed in an unforeseen way'; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +(async () => { |
| 99 | + const { organization, project, resource } = await transifex.credentials(); |
| 100 | + const translationsDirectory = process.argv[2]; |
| 101 | + if (!translationsDirectory) { |
| 102 | + shell.echo('Traslations directory not specified'); |
| 103 | + shell.exit(1); |
| 104 | + } |
| 105 | + |
| 106 | + const languages = await getLanguages(organization, project); |
| 107 | + shell.echo('translations found:', languages.join(', ')); |
| 108 | + |
| 109 | + let downloadIds = []; |
| 110 | + for (const language of languages) { |
| 111 | + downloadIds.push({ |
| 112 | + language, |
| 113 | + id: await requestTranslationDownload({ |
| 114 | + language: { |
| 115 | + data: { |
| 116 | + id: util.format('l:%s', language), |
| 117 | + type: 'languages' |
| 118 | + } |
| 119 | + }, |
| 120 | + resource: { |
| 121 | + data: { |
| 122 | + id: util.format('o:%s:p:%s:r:%s', organization, project, resource), |
| 123 | + type: 'resources' |
| 124 | + } |
| 125 | + } |
| 126 | + }) |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + const res = await Promise.all( |
| 131 | + downloadIds.map(d => getTranslationDownloadStatus(d['language'], d['id'])) |
| 132 | + ).catch(err => { |
| 133 | + shell.echo(err); |
| 134 | + shell.exit(1); |
| 135 | + }); |
| 136 | + |
| 137 | + await Promise.all( |
| 138 | + res.map(r => { |
| 139 | + return download(r['downloadUrl'], translationsDirectory, { |
| 140 | + filename: r['language'] + '.json' |
| 141 | + }); |
| 142 | + }) |
| 143 | + ).catch(err => { |
| 144 | + shell.echo(err); |
| 145 | + shell.exit(1); |
| 146 | + }); |
| 147 | + |
| 148 | + shell.echo('Translation files downloaded.'); |
| 149 | +})(); |
0 commit comments