@kununu/phraseapp-cli 1.5.0-beta.1 → 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 +70 -42
- 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
|
|
|
@@ -9,6 +9,17 @@ require('dotenv').config();
|
|
|
9
9
|
const request = require('request');
|
|
10
10
|
const colors = require('colors'); // eslint-disable-line no-unused-vars
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Splits array into smaller chunks
|
|
14
|
+
*
|
|
15
|
+
* E.g: chunks([1, 2, 3, 4], 2) => [[1, 2], [3, 4]]
|
|
16
|
+
*/
|
|
17
|
+
function* chunks (arr, n) {
|
|
18
|
+
for (let i = 0; i < arr.length; i += n) {
|
|
19
|
+
yield arr.slice(i, i + n);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
12
23
|
function logErrorsAndExitProcess (errors) {
|
|
13
24
|
errors.map(error => console.error(error.red.bold));
|
|
14
25
|
process.exit(1);
|
|
@@ -71,51 +82,68 @@ function getPhraseAppQueryParams (tags, fallbackLocale, branch) {
|
|
|
71
82
|
.join('&');
|
|
72
83
|
}
|
|
73
84
|
|
|
74
|
-
|
|
75
|
-
for (const locale of phrase.locales) {
|
|
76
|
-
const filepath = `${phrase.path}${locale.filePrefix ? `${locale.filePrefix}-` : ''}${locale.locale_id}.json`;
|
|
77
|
-
let file = {};
|
|
85
|
+
const getFileName = (filePrefix, localeId) => `${filePrefix ? `${filePrefix}-` : ''}${localeId}.json`;
|
|
78
86
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
87
|
+
const getFilePath = (path, filePrefix, localeId) => `${path}${getFileName(filePrefix, localeId)}`;
|
|
88
|
+
|
|
89
|
+
function getPhraseAppDownloadUrl (accessToken, projectId, localeId, tags, fallbackLocaleId, branch) {
|
|
90
|
+
return `https://${accessToken}@api.phraseapp.com/api/v2/projects/${projectId}/locales/${localeId}/download?${getPhraseAppQueryParams(tags, fallbackLocaleId, branch)}`;
|
|
91
|
+
}
|
|
82
92
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
93
|
+
function downloadLocaleFile (phrase, locale) {
|
|
94
|
+
const {
|
|
95
|
+
access_token: accessToken,
|
|
96
|
+
branch,
|
|
97
|
+
path,
|
|
98
|
+
project_id: projectId,
|
|
99
|
+
} = phrase;
|
|
100
|
+
|
|
101
|
+
const {
|
|
102
|
+
filePrefix,
|
|
103
|
+
locale_id: localeId,
|
|
104
|
+
fallback_locale_id: fallbackLocaleId,
|
|
105
|
+
} = locale;
|
|
106
|
+
|
|
107
|
+
const filepath = getFilePath(path, filePrefix, localeId);
|
|
108
|
+
|
|
109
|
+
const tags = locale.tags || [false];
|
|
110
|
+
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
const url = getPhraseAppDownloadUrl(accessToken, projectId, localeId, tags, fallbackLocaleId, branch);
|
|
113
|
+
|
|
114
|
+
request.get(url, (error, response, body) => {
|
|
115
|
+
if (!error && response.statusCode === 200) {
|
|
116
|
+
fs.writeFileSync(filepath, body);
|
|
117
|
+
console.info(`Success: locale file ${getFileName(filePrefix, localeId)} was updated`.green.bold);
|
|
118
|
+
return resolve();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (response.statusCode === 401) {
|
|
122
|
+
console.error('Error: Please check your access_token'.red.bold);
|
|
123
|
+
} else {
|
|
124
|
+
console.error(`Error: locale file ${getFileName(filePrefix, localeId)}. Message: ${JSON.parse(body).message}`.red);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return reject(response);
|
|
117
128
|
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function loadLocale (phrase) {
|
|
133
|
+
const startTime = new Date();
|
|
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)) {
|
|
139
|
+
// Create Promise array to download the four translations for this chunk
|
|
140
|
+
const downloads = localeChunk.map(locale => downloadLocaleFile(phrase, locale));
|
|
141
|
+
|
|
142
|
+
// eslint-disable-next-line no-await-in-loop
|
|
143
|
+
await Promise.all(downloads);
|
|
118
144
|
}
|
|
145
|
+
|
|
146
|
+
console.log(`Downloaded all files in ${(new Date() - startTime) / 1000}s`);
|
|
119
147
|
}
|
|
120
148
|
|
|
121
149
|
function loadLocalesFromConfig () {
|