@kununu/phraseapp-cli 3.0.1 → 3.1.0-beta.2

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 +66 -31
  2. package/package.json +5 -6
package/index.js CHANGED
@@ -6,7 +6,6 @@ const fs = require('fs');
6
6
 
7
7
  require('dotenv').config();
8
8
 
9
- const request = require('request');
10
9
  const colors = require('colors'); // eslint-disable-line no-unused-vars
11
10
 
12
11
  /**
@@ -14,22 +13,22 @@ const colors = require('colors'); // eslint-disable-line no-unused-vars
14
13
  *
15
14
  * E.g: chunks([1, 2, 3, 4], 2) => [[1, 2], [3, 4]]
16
15
  */
17
- function* chunks (arr, n) {
16
+ function* chunks(arr, n) {
18
17
  for (let i = 0; i < arr.length; i += n) {
19
18
  yield arr.slice(i, i + n);
20
19
  }
21
20
  }
22
21
 
23
- function logErrorsAndExitProcess (errors) {
22
+ function logErrorsAndExitProcess(errors) {
24
23
  errors.map(error => console.error(error.red.bold));
25
24
  process.exit(1);
26
25
  }
27
26
 
28
- function loadConfig () {
27
+ function loadConfig() {
29
28
  const configPath = `${process.cwd()}/.phraseapp.json`;
30
29
 
31
30
  if (!fs.existsSync(configPath)) {
32
- logErrorsAndExitProcess(['Error: pharaseapp config file doesn\'t exist.']);
31
+ logErrorsAndExitProcess(["Error: pharaseapp config file doesn't exist."]);
33
32
  }
34
33
 
35
34
  const errors = [];
@@ -43,7 +42,9 @@ function loadConfig () {
43
42
  }
44
43
 
45
44
  if (!phrase.locales) {
46
- errors.push('Error: please provide locales in config file ( for example: locales: "pl_PL" ).');
45
+ errors.push(
46
+ 'Error: please provide locales in config file ( for example: locales: "pl_PL" ).',
47
+ );
47
48
  }
48
49
 
49
50
  if (!phrase.access_token) {
@@ -62,7 +63,7 @@ function loadConfig () {
62
63
  return logErrorsAndExitProcess(errors);
63
64
  }
64
65
 
65
- function getPhraseAppQueryParams (tags, fallbackLocale, branch) {
66
+ function getPhraseAppQueryParams(tags, fallbackLocale, branch) {
66
67
  const queryParams = {
67
68
  file_format: 'react_simple_json',
68
69
  tags: tags.join(','),
@@ -82,15 +83,23 @@ function getPhraseAppQueryParams (tags, fallbackLocale, branch) {
82
83
  .join('&');
83
84
  }
84
85
 
85
- const getFileName = (filePrefix, localeId) => `${filePrefix ? `${filePrefix}-` : ''}${localeId}.json`;
86
+ const getFileName = (filePrefix, localeId) =>
87
+ `${filePrefix ? `${filePrefix}-` : ''}${localeId}.json`;
86
88
 
87
- const getFilePath = (path, filePrefix, localeId) => `${path}${getFileName(filePrefix, localeId)}`;
89
+ const getFilePath = (path, filePrefix, localeId) =>
90
+ `${path}${getFileName(filePrefix, localeId)}`;
88
91
 
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)}`;
92
+ function getPhraseAppDownloadUrl(
93
+ projectId,
94
+ localeId,
95
+ tags,
96
+ fallbackLocaleId,
97
+ branch,
98
+ ) {
99
+ return `https://api.phraseapp.com/api/v2/projects/${projectId}/locales/${localeId}/download?${getPhraseAppQueryParams(tags, fallbackLocaleId, branch)}`;
91
100
  }
92
101
 
93
- function downloadLocaleFile (phrase, locale) {
102
+ function downloadLocaleFile(phrase, locale) {
94
103
  const {
95
104
  access_token: accessToken,
96
105
  branch,
@@ -99,9 +108,9 @@ function downloadLocaleFile (phrase, locale) {
99
108
  } = phrase;
100
109
 
101
110
  const {
111
+ fallback_locale_id: fallbackLocaleId,
102
112
  filePrefix,
103
113
  locale_id: localeId,
104
- fallback_locale_id: fallbackLocaleId,
105
114
  } = locale;
106
115
 
107
116
  const filepath = getFilePath(path, filePrefix, localeId);
@@ -109,27 +118,51 @@ function downloadLocaleFile (phrase, locale) {
109
118
  const tags = locale.tags || [false];
110
119
 
111
120
  return new Promise((resolve, reject) => {
112
- const url = getPhraseAppDownloadUrl(accessToken, projectId, localeId, tags, fallbackLocaleId, branch);
121
+ const url = getPhraseAppDownloadUrl(
122
+ projectId,
123
+ localeId,
124
+ tags,
125
+ fallbackLocaleId,
126
+ branch,
127
+ );
128
+
129
+ const headers = new Headers({
130
+ Authorization: `Basic ${btoa(accessToken)}`,
131
+ });
113
132
 
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);
133
+ fetch(url, {headers})
134
+ .then(response => {
135
+ if (!response.ok) {
136
+ throw new Error(response);
137
+ }
138
+ return response.json();
139
+ })
140
+ .then(data => {
141
+ // Handle the JSON response
142
+ fs.writeFileSync(filepath, JSON.stringify(data));
143
+ console.info(
144
+ `Success: locale file ${getFileName(filePrefix, localeId)} was updated`
145
+ .green.bold,
146
+ );
118
147
  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);
128
- });
148
+ })
149
+ .catch(error => {
150
+ if (error.status === 401) {
151
+ console.error('Error: Please check your access_token'.red.bold);
152
+ } else {
153
+ // Handle any errors
154
+ console.error(
155
+ `Error: locale file ${getFileName(filePrefix, localeId)}. Message: ${error.json()}`
156
+ .red,
157
+ );
158
+ }
159
+
160
+ return reject();
161
+ });
129
162
  });
130
163
  }
131
164
 
132
- async function loadLocale (phrase) {
165
+ async function loadLocale(phrase) {
133
166
  const startTime = new Date();
134
167
 
135
168
  // Usually we'd create one big array and do all requests in parallel
@@ -137,7 +170,9 @@ async function loadLocale (phrase) {
137
170
  // https://developers.phrase.com/api/#overview--rate-limiting
138
171
  for (const localeChunk of chunks(phrase.locales, 4)) {
139
172
  // Create Promise array to download the four translations for this chunk
140
- const downloads = localeChunk.map(locale => downloadLocaleFile(phrase, locale));
173
+ const downloads = localeChunk.map(locale =>
174
+ downloadLocaleFile(phrase, locale),
175
+ );
141
176
 
142
177
  // eslint-disable-next-line no-await-in-loop
143
178
  await Promise.all(downloads);
@@ -146,7 +181,7 @@ async function loadLocale (phrase) {
146
181
  console.log(`Downloaded all files in ${(new Date() - startTime) / 1000}s`);
147
182
  }
148
183
 
149
- function loadLocalesFromConfig () {
184
+ function loadLocalesFromConfig() {
150
185
  // load phraseapp config
151
186
  const phrase = loadConfig();
152
187
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kununu/phraseapp-cli",
3
- "version": "3.0.1",
3
+ "version": "3.1.0-beta.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "kununu",
@@ -9,12 +9,11 @@
9
9
  "lint": "eslint . --ext jsx --ext js --ext tsx --ext ts --ignore-path .eslintignore --max-warnings 10"
10
10
  },
11
11
  "dependencies": {
12
- "colors": "1.3.3",
13
- "dotenv": "8.0.0",
14
- "request": "2.88.0"
12
+ "colors": "1.4.0",
13
+ "dotenv": "16.4.5"
15
14
  },
16
15
  "devDependencies": {
17
- "@babel/eslint-plugin": "^7.23.5",
18
- "@kununu/eslint-config": "2.2.0"
16
+ "@babel/eslint-plugin": "7.23.5",
17
+ "@kununu/eslint-config": "5.0.1"
19
18
  }
20
19
  }