@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.
Files changed (2) hide show
  1. package/index.js +71 -40
  2. 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
- async function loadLocale (phrase) {
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
- if (fs.existsSync(filepath)) {
80
- file = JSON.parse(fs.readFileSync(filepath));
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
- const tags = locale.tags || [false];
84
-
85
- await new Promise((resolve, reject) => {
86
- const url = `https://${
87
- phrase.access_token
88
- }@api.phraseapp.com/api/v2/projects/${phrase.project_id}/locales/${
89
- locale.locale_id
90
- }/download?${getPhraseAppQueryParams(tags, locale.fallback_locale_id, phrase.branch)}`;
91
-
92
- request.get(url, (error, response, body) => {
93
- if (!error && response.statusCode === 200) {
94
- fs.writeFileSync(
95
- filepath,
96
- JSON.stringify(Object.assign(file, JSON.parse(body)), null, '\t'),
97
- );
98
- console.info(
99
- `Success: locale file [${locale.locale_id}.json] was updated`.green.bold,
100
- );
101
- resolve();
102
- } else {
103
- if (response.statusCode !== 401) {
104
- console.error(
105
- `Error: locale file [${locale.locale_id}.json] error message: ${
106
- JSON.parse(body).message
107
- }`.red.bold,
108
- );
109
- } else {
110
- console.error('Error: Please check your access_token'.red.bold);
111
- }
112
- reject(response);
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 () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kununu/phraseapp-cli",
3
- "version": "1.5.0-beta.2",
3
+ "version": "1.5.0-beta.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {