@kununu/phraseapp-cli 1.5.0-beta.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +18 -19
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
2
|
|
|
3
|
-
/* eslint-disable */
|
|
3
|
+
/* eslint-disable no-console */
|
|
4
4
|
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
|
|
@@ -10,9 +10,11 @@ const request = require('request');
|
|
|
10
10
|
const colors = require('colors'); // eslint-disable-line no-unused-vars
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Splits array into
|
|
13
|
+
* Splits array into smaller chunks
|
|
14
|
+
*
|
|
15
|
+
* E.g: chunks([1, 2, 3, 4], 2) => [[1, 2], [3, 4]]
|
|
14
16
|
*/
|
|
15
|
-
function* chunks(arr, n) {
|
|
17
|
+
function* chunks (arr, n) {
|
|
16
18
|
for (let i = 0; i < arr.length; i += n) {
|
|
17
19
|
yield arr.slice(i, i + n);
|
|
18
20
|
}
|
|
@@ -63,7 +65,7 @@ function loadConfig () {
|
|
|
63
65
|
function getPhraseAppQueryParams (tags, fallbackLocale, branch) {
|
|
64
66
|
const queryParams = {
|
|
65
67
|
file_format: 'react_simple_json',
|
|
66
|
-
tags: tags.join(',')
|
|
68
|
+
tags: tags.join(','),
|
|
67
69
|
};
|
|
68
70
|
|
|
69
71
|
if (fallbackLocale) {
|
|
@@ -82,18 +84,12 @@ function getPhraseAppQueryParams (tags, fallbackLocale, branch) {
|
|
|
82
84
|
|
|
83
85
|
const getFileName = (filePrefix, localeId) => `${filePrefix ? `${filePrefix}-` : ''}${localeId}.json`;
|
|
84
86
|
|
|
85
|
-
const getFilePath = (path, filePrefix, localeId) => {
|
|
86
|
-
return `${path}${getFileName(filePrefix, localeId)}`;
|
|
87
|
-
}
|
|
87
|
+
const getFilePath = (path, filePrefix, localeId) => `${path}${getFileName(filePrefix, localeId)}`;
|
|
88
88
|
|
|
89
|
-
function getPhraseAppDownloadUrl(accessToken, projectId, localeId, tags, fallbackLocaleId, branch) {
|
|
89
|
+
function getPhraseAppDownloadUrl (accessToken, projectId, localeId, tags, fallbackLocaleId, branch) {
|
|
90
90
|
return `https://${accessToken}@api.phraseapp.com/api/v2/projects/${projectId}/locales/${localeId}/download?${getPhraseAppQueryParams(tags, fallbackLocaleId, branch)}`;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
function logDownloadError (err) {
|
|
94
|
-
console.error(err.red.bold);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
93
|
function downloadLocaleFile (phrase, locale) {
|
|
98
94
|
const {
|
|
99
95
|
access_token: accessToken,
|
|
@@ -117,7 +113,6 @@ function downloadLocaleFile (phrase, locale) {
|
|
|
117
113
|
|
|
118
114
|
request.get(url, (error, response, body) => {
|
|
119
115
|
if (!error && response.statusCode === 200) {
|
|
120
|
-
// const content = JSON.stringify(JSON.parse(body), null, '\t');
|
|
121
116
|
fs.writeFileSync(filepath, body);
|
|
122
117
|
console.info(`Success: locale file ${getFileName(filePrefix, localeId)} was updated`.green.bold);
|
|
123
118
|
return resolve();
|
|
@@ -126,21 +121,25 @@ function downloadLocaleFile (phrase, locale) {
|
|
|
126
121
|
if (response.statusCode === 401) {
|
|
127
122
|
console.error('Error: Please check your access_token'.red.bold);
|
|
128
123
|
} else {
|
|
129
|
-
console.error(`Error: locale file ${getFileName(filePrefix, localeId)}. Message: ${JSON.parse(body).message}`.red)
|
|
124
|
+
console.error(`Error: locale file ${getFileName(filePrefix, localeId)}. Message: ${JSON.parse(body).message}`.red);
|
|
130
125
|
}
|
|
131
126
|
|
|
132
|
-
reject(response);
|
|
127
|
+
return reject(response);
|
|
133
128
|
});
|
|
134
|
-
})
|
|
129
|
+
});
|
|
135
130
|
}
|
|
136
131
|
|
|
137
132
|
async function loadLocale (phrase) {
|
|
138
133
|
const startTime = new Date();
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
134
|
+
|
|
135
|
+
// Usually we'd create one big array and do all requests in parallel
|
|
136
|
+
// Unfortunately phraseapp only allows us 4 concurrent request. So we split our locales into groups of 4
|
|
137
|
+
// https://developers.phrase.com/api/#overview--rate-limiting
|
|
138
|
+
for (const localeChunk of chunks(phrase.locales, 4)) {
|
|
142
139
|
// Create Promise array to download the four translations for this chunk
|
|
143
140
|
const downloads = localeChunk.map(locale => downloadLocaleFile(phrase, locale));
|
|
141
|
+
|
|
142
|
+
// eslint-disable-next-line no-await-in-loop
|
|
144
143
|
await Promise.all(downloads);
|
|
145
144
|
}
|
|
146
145
|
|