@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.
Files changed (2) hide show
  1. package/index.js +70 -42
  2. 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
- 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 = {};
85
+ const getFileName = (filePrefix, localeId) => `${filePrefix ? `${filePrefix}-` : ''}${localeId}.json`;
78
86
 
79
- if (fs.existsSync(filepath)) {
80
- file = JSON.parse(fs.readFileSync(filepath));
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
- 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]${
100
- tag ? ` for tag ${tag} ` : ' '
101
- }was updated`.green.bold,
102
- );
103
- resolve();
104
- } else {
105
- if (response.statusCode !== 401) {
106
- console.error(
107
- `Error: locale file [${locale.locale_id}.json] error message: ${
108
- JSON.parse(body).message
109
- }`.red.bold,
110
- );
111
- } else {
112
- console.error('Error: Please check your access_token'.red.bold);
113
- }
114
- reject(response);
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 () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kununu/phraseapp-cli",
3
- "version": "1.5.0-beta.1",
3
+ "version": "2.0.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {