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

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 +45 -20
  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,24 @@ 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) {
92
+ function getPhraseAppDownloadUrl(
93
+ accessToken,
94
+ projectId,
95
+ localeId,
96
+ tags,
97
+ fallbackLocaleId,
98
+ branch,
99
+ ) {
90
100
  return `https://${accessToken}@api.phraseapp.com/api/v2/projects/${projectId}/locales/${localeId}/download?${getPhraseAppQueryParams(tags, fallbackLocaleId, branch)}`;
91
101
  }
92
102
 
93
- function downloadLocaleFile (phrase, locale) {
103
+ function downloadLocaleFile(phrase, locale) {
94
104
  const {
95
105
  access_token: accessToken,
96
106
  branch,
@@ -99,9 +109,9 @@ function downloadLocaleFile (phrase, locale) {
99
109
  } = phrase;
100
110
 
101
111
  const {
112
+ fallback_locale_id: fallbackLocaleId,
102
113
  filePrefix,
103
114
  locale_id: localeId,
104
- fallback_locale_id: fallbackLocaleId,
105
115
  } = locale;
106
116
 
107
117
  const filepath = getFilePath(path, filePrefix, localeId);
@@ -109,19 +119,32 @@ function downloadLocaleFile (phrase, locale) {
109
119
  const tags = locale.tags || [false];
110
120
 
111
121
  return new Promise((resolve, reject) => {
112
- const url = getPhraseAppDownloadUrl(accessToken, projectId, localeId, tags, fallbackLocaleId, branch);
113
-
114
- request.get(url, (error, response, body) => {
122
+ const url = getPhraseAppDownloadUrl(
123
+ accessToken,
124
+ projectId,
125
+ localeId,
126
+ tags,
127
+ fallbackLocaleId,
128
+ branch,
129
+ );
130
+
131
+ fetch(url, (error, response, body) => {
115
132
  if (!error && response.statusCode === 200) {
116
133
  fs.writeFileSync(filepath, body);
117
- console.info(`Success: locale file ${getFileName(filePrefix, localeId)} was updated`.green.bold);
134
+ console.info(
135
+ `Success: locale file ${getFileName(filePrefix, localeId)} was updated`
136
+ .green.bold,
137
+ );
118
138
  return resolve();
119
139
  }
120
140
 
121
141
  if (response.statusCode === 401) {
122
142
  console.error('Error: Please check your access_token'.red.bold);
123
143
  } else {
124
- console.error(`Error: locale file ${getFileName(filePrefix, localeId)}. Message: ${JSON.parse(body).message}`.red);
144
+ console.error(
145
+ `Error: locale file ${getFileName(filePrefix, localeId)}. Message: ${JSON.parse(body).message}`
146
+ .red,
147
+ );
125
148
  }
126
149
 
127
150
  return reject(response);
@@ -129,7 +152,7 @@ function downloadLocaleFile (phrase, locale) {
129
152
  });
130
153
  }
131
154
 
132
- async function loadLocale (phrase) {
155
+ async function loadLocale(phrase) {
133
156
  const startTime = new Date();
134
157
 
135
158
  // Usually we'd create one big array and do all requests in parallel
@@ -137,7 +160,9 @@ async function loadLocale (phrase) {
137
160
  // https://developers.phrase.com/api/#overview--rate-limiting
138
161
  for (const localeChunk of chunks(phrase.locales, 4)) {
139
162
  // Create Promise array to download the four translations for this chunk
140
- const downloads = localeChunk.map(locale => downloadLocaleFile(phrase, locale));
163
+ const downloads = localeChunk.map(locale =>
164
+ downloadLocaleFile(phrase, locale),
165
+ );
141
166
 
142
167
  // eslint-disable-next-line no-await-in-loop
143
168
  await Promise.all(downloads);
@@ -146,7 +171,7 @@ async function loadLocale (phrase) {
146
171
  console.log(`Downloaded all files in ${(new Date() - startTime) / 1000}s`);
147
172
  }
148
173
 
149
- function loadLocalesFromConfig () {
174
+ function loadLocalesFromConfig() {
150
175
  // load phraseapp config
151
176
  const phrase = loadConfig();
152
177
 
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.1",
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
  }