@kununu/phraseapp-cli 1.5.0-beta.2 → 1.5.0-beta.3
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 +71 -40
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -9,6 +9,15 @@ 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
|
|
14
|
+
*/
|
|
15
|
+
function* chunks(arr, n) {
|
|
16
|
+
for (let i = 0; i < arr.length; i += n) {
|
|
17
|
+
yield arr.slice(i, i + n);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
12
21
|
function logErrorsAndExitProcess (errors) {
|
|
13
22
|
errors.map(error => console.error(error.red.bold));
|
|
14
23
|
process.exit(1);
|
|
@@ -54,7 +63,7 @@ function loadConfig () {
|
|
|
54
63
|
function getPhraseAppQueryParams (tags, fallbackLocale, branch) {
|
|
55
64
|
const queryParams = {
|
|
56
65
|
file_format: 'react_simple_json',
|
|
57
|
-
tags: tags.join(',')
|
|
66
|
+
tags: tags.join(',')
|
|
58
67
|
};
|
|
59
68
|
|
|
60
69
|
if (fallbackLocale) {
|
|
@@ -71,49 +80,71 @@ function getPhraseAppQueryParams (tags, fallbackLocale, branch) {
|
|
|
71
80
|
.join('&');
|
|
72
81
|
}
|
|
73
82
|
|
|
74
|
-
|
|
75
|
-
for (const locale of phrase.locales) {
|
|
76
|
-
const filepath = `${phrase.path}${locale.filePrefix ? `${locale.filePrefix}-` : ''}${locale.locale_id}.json`;
|
|
77
|
-
let file = {};
|
|
83
|
+
const getFileName = (filePrefix, localeId) => `${filePrefix ? `${filePrefix}-` : ''}${localeId}.json`;
|
|
78
84
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
const getFilePath = (path, filePrefix, localeId) => {
|
|
86
|
+
return `${path}${getFileName(filePrefix, localeId)}`;
|
|
87
|
+
}
|
|
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
|
+
}
|
|
92
|
+
|
|
93
|
+
function logDownloadError (err) {
|
|
94
|
+
console.error(err.red.bold);
|
|
95
|
+
}
|
|
82
96
|
|
|
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
|
-
}
|
|
97
|
+
function downloadLocaleFile (phrase, locale) {
|
|
98
|
+
const {
|
|
99
|
+
access_token: accessToken,
|
|
100
|
+
branch,
|
|
101
|
+
path,
|
|
102
|
+
project_id: projectId,
|
|
103
|
+
} = phrase;
|
|
104
|
+
|
|
105
|
+
const {
|
|
106
|
+
filePrefix,
|
|
107
|
+
locale_id: localeId,
|
|
108
|
+
fallback_locale_id: fallbackLocaleId,
|
|
109
|
+
} = locale;
|
|
110
|
+
|
|
111
|
+
const filepath = getFilePath(path, filePrefix, localeId);
|
|
112
|
+
|
|
113
|
+
const tags = locale.tags || [false];
|
|
114
|
+
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
const url = getPhraseAppDownloadUrl(accessToken, projectId, localeId, tags, fallbackLocaleId, branch);
|
|
117
|
+
|
|
118
|
+
request.get(url, (error, response, body) => {
|
|
119
|
+
if (!error && response.statusCode === 200) {
|
|
120
|
+
// const content = JSON.stringify(JSON.parse(body), null, '\t');
|
|
121
|
+
fs.writeFileSync(filepath, body);
|
|
122
|
+
console.info(`Success: locale file ${getFileName(filePrefix, localeId)} was updated`.green.bold);
|
|
123
|
+
return resolve();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (response.statusCode === 401) {
|
|
127
|
+
console.error('Error: Please check your access_token'.red.bold);
|
|
128
|
+
} else {
|
|
129
|
+
console.error(`Error: locale file ${getFileName(filePrefix, localeId)}. Message: ${JSON.parse(body).message}`.red)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
reject(response);
|
|
115
133
|
});
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function loadLocale (phrase) {
|
|
138
|
+
const startTime = new Date();
|
|
139
|
+
// unfortunately phraseapp only allows us 4 concurrent request. So we split our locales into groups of 4
|
|
140
|
+
const localeChunks = [...chunks(phrase.locales, 4)];
|
|
141
|
+
for (localeChunk of localeChunks) {
|
|
142
|
+
// Create Promise array to download the four translations for this chunk
|
|
143
|
+
const downloads = localeChunk.map(locale => downloadLocaleFile(phrase, locale));
|
|
144
|
+
await Promise.all(downloads);
|
|
116
145
|
}
|
|
146
|
+
|
|
147
|
+
console.log(`Downloaded all files in ${(new Date() - startTime) / 1000}s`);
|
|
117
148
|
}
|
|
118
149
|
|
|
119
150
|
function loadLocalesFromConfig () {
|