@vocab/phrase 0.0.0-phrase-pull-dev-language-202281412540

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.
@@ -0,0 +1,261 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var fs = require('fs');
6
+ var path = require('path');
7
+ var core = require('@vocab/core');
8
+ var FormData = require('form-data');
9
+ var fetch = require('node-fetch');
10
+ var chalk = require('chalk');
11
+ var debug = require('debug');
12
+
13
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
14
+
15
+ var path__default = /*#__PURE__*/_interopDefault(path);
16
+ var FormData__default = /*#__PURE__*/_interopDefault(FormData);
17
+ var fetch__default = /*#__PURE__*/_interopDefault(fetch);
18
+ var chalk__default = /*#__PURE__*/_interopDefault(chalk);
19
+ var debug__default = /*#__PURE__*/_interopDefault(debug);
20
+
21
+ const mkdir = fs.promises.mkdir;
22
+ const writeFile = fs.promises.writeFile;
23
+
24
+ const trace = debug__default['default'](`vocab:phrase`);
25
+ const log = (...params) => {
26
+ // eslint-disable-next-line no-console
27
+ console.log(chalk__default['default'].yellow('Vocab'), ...params);
28
+ };
29
+
30
+ function _callPhrase(path, options = {}) {
31
+ const phraseApiToken = process.env.PHRASE_API_TOKEN;
32
+
33
+ if (!phraseApiToken) {
34
+ throw new Error('Missing PHRASE_API_TOKEN');
35
+ }
36
+
37
+ return fetch__default['default'](path, { ...options,
38
+ headers: {
39
+ Authorization: `token ${phraseApiToken}`,
40
+ // Provide identification via User Agent as requested in https://developers.phrase.com/api/#overview--identification-via-user-agent
41
+ 'User-Agent': 'SEEK Demo Candidate App (jhope@seek.com.au)',
42
+ ...options.headers
43
+ }
44
+ }).then(async response => {
45
+ console.log(`${path}: ${response.status} - ${response.statusText}`);
46
+ console.log(`Rate Limit: ${response.headers.get('X-Rate-Limit-Remaining')} of ${response.headers.get('X-Rate-Limit-Limit')} remaining. (${response.headers.get('X-Rate-Limit-Reset')} seconds remaining})`);
47
+ console.log('\nLink:', response.headers.get('Link'), '\n'); // Print All Headers:
48
+ // console.log(Array.from(r.headers.entries()));
49
+
50
+ try {
51
+ var _response$headers$get;
52
+
53
+ const result = await response.json();
54
+ console.log(`Internal Result (Length: ${result.length})\n`);
55
+
56
+ if ((!options.method || options.method === 'GET') && (_response$headers$get = response.headers.get('Link')) !== null && _response$headers$get !== void 0 && _response$headers$get.includes('rel=next')) {
57
+ var _response$headers$get2, _response$headers$get3;
58
+
59
+ const [, nextPageUrl] = (_response$headers$get2 = (_response$headers$get3 = response.headers.get('Link')) === null || _response$headers$get3 === void 0 ? void 0 : _response$headers$get3.match(/<([^>]*)>; rel=next/)) !== null && _response$headers$get2 !== void 0 ? _response$headers$get2 : [];
60
+
61
+ if (!nextPageUrl) {
62
+ throw new Error('Cant parse next page URL');
63
+ }
64
+
65
+ console.log('Results recieved with next page: ', nextPageUrl);
66
+ const nextPageResult = await _callPhrase(nextPageUrl, options);
67
+ return [...result, ...nextPageResult];
68
+ }
69
+
70
+ return result;
71
+ } catch (e) {
72
+ console.error('Unable to parse response as JSON', e);
73
+ return response.text();
74
+ }
75
+ });
76
+ }
77
+
78
+ async function callPhrase(relativePath, options = {}) {
79
+ const projectId = process.env.PHRASE_PROJECT_ID;
80
+
81
+ if (!projectId) {
82
+ throw new Error('Missing PHRASE_PROJECT_ID');
83
+ }
84
+
85
+ return _callPhrase(`https://api.phrase.com/v2/projects/${projectId}/${relativePath}`, options).then(result => {
86
+ if (Array.isArray(result)) {
87
+ console.log('Result length:', result.length);
88
+ }
89
+
90
+ return result;
91
+ }).catch(error => {
92
+ console.error(`Error calling phrase for ${relativePath}:`, error);
93
+ throw Error;
94
+ });
95
+ }
96
+ async function pullAllTranslations(branch) {
97
+ const phraseResult = await callPhrase(`translations?branch=${branch}&per_page=100`);
98
+ const translations = {};
99
+
100
+ for (const r of phraseResult) {
101
+ if (!translations[r.locale.code]) {
102
+ translations[r.locale.code] = {};
103
+ }
104
+
105
+ translations[r.locale.code][r.key.name] = {
106
+ message: r.content
107
+ };
108
+ }
109
+
110
+ return translations;
111
+ }
112
+ async function pushTranslationsByLocale(contents, locale, branch) {
113
+ const formData = new FormData__default['default']();
114
+ const fileContents = Buffer.from(JSON.stringify(contents));
115
+ formData.append('file', fileContents, {
116
+ contentType: 'application/json',
117
+ filename: `${locale}.json`
118
+ });
119
+ formData.append('file_format', 'json');
120
+ formData.append('locale_id', locale);
121
+ formData.append('branch', branch);
122
+ formData.append('update_translations', 'true');
123
+ trace('Starting to upload:', locale);
124
+ await callPhrase(`uploads`, {
125
+ method: 'POST',
126
+ body: formData
127
+ });
128
+ log('Successfully Uploaded:', locale, '\n');
129
+ }
130
+ async function ensureBranch(branch) {
131
+ await callPhrase(`branches`, {
132
+ method: 'POST',
133
+ headers: {
134
+ 'Content-Type': 'application/json'
135
+ },
136
+ body: JSON.stringify({
137
+ name: branch
138
+ })
139
+ });
140
+ trace('Created branch:', branch);
141
+ }
142
+
143
+ async function pull({
144
+ branch = 'local-development'
145
+ }, config) {
146
+ trace(`Pulling translations from branch ${branch}`);
147
+ await ensureBranch(branch);
148
+ const alternativeLanguages = core.getAltLanguages(config);
149
+ const allPhraseTranslations = await pullAllTranslations(branch);
150
+ trace(`Pulling translations from Phrase for languages ${config.devLanguage} and ${alternativeLanguages.join(', ')}`);
151
+ const phraseLanguages = Object.keys(allPhraseTranslations);
152
+ const phraseLanguagesWithTranslations = phraseLanguages.filter(language => {
153
+ const phraseTranslationsForLanguage = allPhraseTranslations[language];
154
+ return Object.keys(phraseTranslationsForLanguage).length > 0;
155
+ });
156
+ trace(`Found Phrase translations for languages ${phraseLanguagesWithTranslations.join(', ')}`);
157
+
158
+ if (!phraseLanguagesWithTranslations.includes(config.devLanguage)) {
159
+ throw new Error(`Phrase did not return any translations for dev language "${config.devLanguage}".\nEnsure you have configured your Phrase project for your dev language, and have pushed your translations.`);
160
+ }
161
+
162
+ const allVocabTranslations = await core.loadAllTranslations({
163
+ fallbacks: 'none',
164
+ includeNodeModules: false
165
+ }, config);
166
+
167
+ for (const loadedTranslation of allVocabTranslations) {
168
+ const devTranslations = loadedTranslation.languages[config.devLanguage];
169
+
170
+ if (!devTranslations) {
171
+ throw new Error('No dev language translations loaded');
172
+ }
173
+
174
+ const defaultValues = { ...devTranslations
175
+ };
176
+ const localKeys = Object.keys(defaultValues);
177
+
178
+ for (const key of localKeys) {
179
+ defaultValues[key] = { ...defaultValues[key],
180
+ ...allPhraseTranslations[config.devLanguage][core.getUniqueKey(key, loadedTranslation.namespace)]
181
+ };
182
+ }
183
+
184
+ await writeFile(loadedTranslation.filePath, `${JSON.stringify(defaultValues, null, 2)}\n`);
185
+
186
+ for (const alternativeLanguage of alternativeLanguages) {
187
+ if (alternativeLanguage in allPhraseTranslations) {
188
+ const altTranslations = { ...loadedTranslation.languages[alternativeLanguage]
189
+ };
190
+ const phraseAltTranslations = allPhraseTranslations[alternativeLanguage];
191
+
192
+ for (const key of localKeys) {
193
+ var _phraseAltTranslation;
194
+
195
+ const phraseKey = core.getUniqueKey(key, loadedTranslation.namespace);
196
+ const phraseTranslationMessage = (_phraseAltTranslation = phraseAltTranslations[phraseKey]) === null || _phraseAltTranslation === void 0 ? void 0 : _phraseAltTranslation.message;
197
+
198
+ if (!phraseTranslationMessage) {
199
+ trace(`Missing translation. No translation for key ${key} in phrase as ${phraseKey} in language ${alternativeLanguage}.`);
200
+ continue;
201
+ }
202
+
203
+ altTranslations[key] = { ...altTranslations[key],
204
+ message: phraseTranslationMessage
205
+ };
206
+ }
207
+
208
+ const altTranslationFilePath = core.getAltLanguageFilePath(loadedTranslation.filePath, alternativeLanguage);
209
+ await mkdir(path__default['default'].dirname(altTranslationFilePath), {
210
+ recursive: true
211
+ });
212
+ await writeFile(altTranslationFilePath, `${JSON.stringify(altTranslations, null, 2)}\n`);
213
+ }
214
+ }
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Uploading to the Phrase API for each language. Adding a unique namespace to each key using file path they key came from
220
+ */
221
+ async function push({
222
+ branch
223
+ }, config) {
224
+ const allLanguageTranslations = await core.loadAllTranslations({
225
+ fallbacks: 'none',
226
+ includeNodeModules: false
227
+ }, config);
228
+ trace(`Pushing translations to branch ${branch}`);
229
+ const allLanguages = config.languages.map(v => v.name);
230
+ await ensureBranch(branch);
231
+ trace(`Pushing translations to phrase for languages ${allLanguages.join(', ')}`);
232
+ const phraseTranslations = {};
233
+
234
+ for (const loadedTranslation of allLanguageTranslations) {
235
+ for (const language of allLanguages) {
236
+ const localTranslations = loadedTranslation.languages[language];
237
+
238
+ if (!localTranslations) {
239
+ continue;
240
+ }
241
+
242
+ if (!phraseTranslations[language]) {
243
+ phraseTranslations[language] = {};
244
+ }
245
+
246
+ for (const localKey of Object.keys(localTranslations)) {
247
+ const phraseKey = core.getUniqueKey(localKey, loadedTranslation.namespace);
248
+ phraseTranslations[language][phraseKey] = localTranslations[localKey];
249
+ }
250
+ }
251
+ }
252
+
253
+ for (const language of allLanguages) {
254
+ if (phraseTranslations[language]) {
255
+ await pushTranslationsByLocale(phraseTranslations[language], language, branch);
256
+ }
257
+ }
258
+ }
259
+
260
+ exports.pull = pull;
261
+ exports.push = push;
@@ -0,0 +1,248 @@
1
+ import { promises } from 'fs';
2
+ import path from 'path';
3
+ import { getAltLanguages, loadAllTranslations, getUniqueKey, getAltLanguageFilePath } from '@vocab/core';
4
+ import FormData from 'form-data';
5
+ import fetch from 'node-fetch';
6
+ import chalk from 'chalk';
7
+ import debug from 'debug';
8
+
9
+ const mkdir = promises.mkdir;
10
+ const writeFile = promises.writeFile;
11
+
12
+ const trace = debug(`vocab:phrase`);
13
+ const log = (...params) => {
14
+ // eslint-disable-next-line no-console
15
+ console.log(chalk.yellow('Vocab'), ...params);
16
+ };
17
+
18
+ function _callPhrase(path, options = {}) {
19
+ const phraseApiToken = process.env.PHRASE_API_TOKEN;
20
+
21
+ if (!phraseApiToken) {
22
+ throw new Error('Missing PHRASE_API_TOKEN');
23
+ }
24
+
25
+ return fetch(path, { ...options,
26
+ headers: {
27
+ Authorization: `token ${phraseApiToken}`,
28
+ // Provide identification via User Agent as requested in https://developers.phrase.com/api/#overview--identification-via-user-agent
29
+ 'User-Agent': 'SEEK Demo Candidate App (jhope@seek.com.au)',
30
+ ...options.headers
31
+ }
32
+ }).then(async response => {
33
+ console.log(`${path}: ${response.status} - ${response.statusText}`);
34
+ console.log(`Rate Limit: ${response.headers.get('X-Rate-Limit-Remaining')} of ${response.headers.get('X-Rate-Limit-Limit')} remaining. (${response.headers.get('X-Rate-Limit-Reset')} seconds remaining})`);
35
+ console.log('\nLink:', response.headers.get('Link'), '\n'); // Print All Headers:
36
+ // console.log(Array.from(r.headers.entries()));
37
+
38
+ try {
39
+ var _response$headers$get;
40
+
41
+ const result = await response.json();
42
+ console.log(`Internal Result (Length: ${result.length})\n`);
43
+
44
+ if ((!options.method || options.method === 'GET') && (_response$headers$get = response.headers.get('Link')) !== null && _response$headers$get !== void 0 && _response$headers$get.includes('rel=next')) {
45
+ var _response$headers$get2, _response$headers$get3;
46
+
47
+ const [, nextPageUrl] = (_response$headers$get2 = (_response$headers$get3 = response.headers.get('Link')) === null || _response$headers$get3 === void 0 ? void 0 : _response$headers$get3.match(/<([^>]*)>; rel=next/)) !== null && _response$headers$get2 !== void 0 ? _response$headers$get2 : [];
48
+
49
+ if (!nextPageUrl) {
50
+ throw new Error('Cant parse next page URL');
51
+ }
52
+
53
+ console.log('Results recieved with next page: ', nextPageUrl);
54
+ const nextPageResult = await _callPhrase(nextPageUrl, options);
55
+ return [...result, ...nextPageResult];
56
+ }
57
+
58
+ return result;
59
+ } catch (e) {
60
+ console.error('Unable to parse response as JSON', e);
61
+ return response.text();
62
+ }
63
+ });
64
+ }
65
+
66
+ async function callPhrase(relativePath, options = {}) {
67
+ const projectId = process.env.PHRASE_PROJECT_ID;
68
+
69
+ if (!projectId) {
70
+ throw new Error('Missing PHRASE_PROJECT_ID');
71
+ }
72
+
73
+ return _callPhrase(`https://api.phrase.com/v2/projects/${projectId}/${relativePath}`, options).then(result => {
74
+ if (Array.isArray(result)) {
75
+ console.log('Result length:', result.length);
76
+ }
77
+
78
+ return result;
79
+ }).catch(error => {
80
+ console.error(`Error calling phrase for ${relativePath}:`, error);
81
+ throw Error;
82
+ });
83
+ }
84
+ async function pullAllTranslations(branch) {
85
+ const phraseResult = await callPhrase(`translations?branch=${branch}&per_page=100`);
86
+ const translations = {};
87
+
88
+ for (const r of phraseResult) {
89
+ if (!translations[r.locale.code]) {
90
+ translations[r.locale.code] = {};
91
+ }
92
+
93
+ translations[r.locale.code][r.key.name] = {
94
+ message: r.content
95
+ };
96
+ }
97
+
98
+ return translations;
99
+ }
100
+ async function pushTranslationsByLocale(contents, locale, branch) {
101
+ const formData = new FormData();
102
+ const fileContents = Buffer.from(JSON.stringify(contents));
103
+ formData.append('file', fileContents, {
104
+ contentType: 'application/json',
105
+ filename: `${locale}.json`
106
+ });
107
+ formData.append('file_format', 'json');
108
+ formData.append('locale_id', locale);
109
+ formData.append('branch', branch);
110
+ formData.append('update_translations', 'true');
111
+ trace('Starting to upload:', locale);
112
+ await callPhrase(`uploads`, {
113
+ method: 'POST',
114
+ body: formData
115
+ });
116
+ log('Successfully Uploaded:', locale, '\n');
117
+ }
118
+ async function ensureBranch(branch) {
119
+ await callPhrase(`branches`, {
120
+ method: 'POST',
121
+ headers: {
122
+ 'Content-Type': 'application/json'
123
+ },
124
+ body: JSON.stringify({
125
+ name: branch
126
+ })
127
+ });
128
+ trace('Created branch:', branch);
129
+ }
130
+
131
+ async function pull({
132
+ branch = 'local-development'
133
+ }, config) {
134
+ trace(`Pulling translations from branch ${branch}`);
135
+ await ensureBranch(branch);
136
+ const alternativeLanguages = getAltLanguages(config);
137
+ const allPhraseTranslations = await pullAllTranslations(branch);
138
+ trace(`Pulling translations from Phrase for languages ${config.devLanguage} and ${alternativeLanguages.join(', ')}`);
139
+ const phraseLanguages = Object.keys(allPhraseTranslations);
140
+ const phraseLanguagesWithTranslations = phraseLanguages.filter(language => {
141
+ const phraseTranslationsForLanguage = allPhraseTranslations[language];
142
+ return Object.keys(phraseTranslationsForLanguage).length > 0;
143
+ });
144
+ trace(`Found Phrase translations for languages ${phraseLanguagesWithTranslations.join(', ')}`);
145
+
146
+ if (!phraseLanguagesWithTranslations.includes(config.devLanguage)) {
147
+ throw new Error(`Phrase did not return any translations for dev language "${config.devLanguage}".\nEnsure you have configured your Phrase project for your dev language, and have pushed your translations.`);
148
+ }
149
+
150
+ const allVocabTranslations = await loadAllTranslations({
151
+ fallbacks: 'none',
152
+ includeNodeModules: false
153
+ }, config);
154
+
155
+ for (const loadedTranslation of allVocabTranslations) {
156
+ const devTranslations = loadedTranslation.languages[config.devLanguage];
157
+
158
+ if (!devTranslations) {
159
+ throw new Error('No dev language translations loaded');
160
+ }
161
+
162
+ const defaultValues = { ...devTranslations
163
+ };
164
+ const localKeys = Object.keys(defaultValues);
165
+
166
+ for (const key of localKeys) {
167
+ defaultValues[key] = { ...defaultValues[key],
168
+ ...allPhraseTranslations[config.devLanguage][getUniqueKey(key, loadedTranslation.namespace)]
169
+ };
170
+ }
171
+
172
+ await writeFile(loadedTranslation.filePath, `${JSON.stringify(defaultValues, null, 2)}\n`);
173
+
174
+ for (const alternativeLanguage of alternativeLanguages) {
175
+ if (alternativeLanguage in allPhraseTranslations) {
176
+ const altTranslations = { ...loadedTranslation.languages[alternativeLanguage]
177
+ };
178
+ const phraseAltTranslations = allPhraseTranslations[alternativeLanguage];
179
+
180
+ for (const key of localKeys) {
181
+ var _phraseAltTranslation;
182
+
183
+ const phraseKey = getUniqueKey(key, loadedTranslation.namespace);
184
+ const phraseTranslationMessage = (_phraseAltTranslation = phraseAltTranslations[phraseKey]) === null || _phraseAltTranslation === void 0 ? void 0 : _phraseAltTranslation.message;
185
+
186
+ if (!phraseTranslationMessage) {
187
+ trace(`Missing translation. No translation for key ${key} in phrase as ${phraseKey} in language ${alternativeLanguage}.`);
188
+ continue;
189
+ }
190
+
191
+ altTranslations[key] = { ...altTranslations[key],
192
+ message: phraseTranslationMessage
193
+ };
194
+ }
195
+
196
+ const altTranslationFilePath = getAltLanguageFilePath(loadedTranslation.filePath, alternativeLanguage);
197
+ await mkdir(path.dirname(altTranslationFilePath), {
198
+ recursive: true
199
+ });
200
+ await writeFile(altTranslationFilePath, `${JSON.stringify(altTranslations, null, 2)}\n`);
201
+ }
202
+ }
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Uploading to the Phrase API for each language. Adding a unique namespace to each key using file path they key came from
208
+ */
209
+ async function push({
210
+ branch
211
+ }, config) {
212
+ const allLanguageTranslations = await loadAllTranslations({
213
+ fallbacks: 'none',
214
+ includeNodeModules: false
215
+ }, config);
216
+ trace(`Pushing translations to branch ${branch}`);
217
+ const allLanguages = config.languages.map(v => v.name);
218
+ await ensureBranch(branch);
219
+ trace(`Pushing translations to phrase for languages ${allLanguages.join(', ')}`);
220
+ const phraseTranslations = {};
221
+
222
+ for (const loadedTranslation of allLanguageTranslations) {
223
+ for (const language of allLanguages) {
224
+ const localTranslations = loadedTranslation.languages[language];
225
+
226
+ if (!localTranslations) {
227
+ continue;
228
+ }
229
+
230
+ if (!phraseTranslations[language]) {
231
+ phraseTranslations[language] = {};
232
+ }
233
+
234
+ for (const localKey of Object.keys(localTranslations)) {
235
+ const phraseKey = getUniqueKey(localKey, loadedTranslation.namespace);
236
+ phraseTranslations[language][phraseKey] = localTranslations[localKey];
237
+ }
238
+ }
239
+ }
240
+
241
+ for (const language of allLanguages) {
242
+ if (phraseTranslations[language]) {
243
+ await pushTranslationsByLocale(phraseTranslations[language], language, branch);
244
+ }
245
+ }
246
+ }
247
+
248
+ export { pull, push };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@vocab/phrase",
3
+ "version": "0.0.0-phrase-pull-dev-language-202281412540",
4
+ "main": "dist/vocab-phrase.cjs.js",
5
+ "module": "dist/vocab-phrase.esm.js",
6
+ "author": "SEEK",
7
+ "license": "MIT",
8
+ "dependencies": {
9
+ "@vocab/core": "^1.0.0",
10
+ "@vocab/types": "^1.0.0",
11
+ "chalk": "^4.1.0",
12
+ "debug": "^4.3.1",
13
+ "form-data": "^3.0.0",
14
+ "node-fetch": "^2.6.1"
15
+ },
16
+ "devDependencies": {
17
+ "@types/node-fetch": "^2.5.7"
18
+ }
19
+ }
package/src/file.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { promises as fs } from 'fs';
2
+
3
+ export const mkdir = fs.mkdir;
4
+ export const writeFile = fs.writeFile;
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { pull } from './pull-translations';
2
+ export { push } from './push-translations';
package/src/logger.ts ADDED
@@ -0,0 +1,9 @@
1
+ import chalk from 'chalk';
2
+ import debug from 'debug';
3
+
4
+ export const trace = debug(`vocab:phrase`);
5
+
6
+ export const log = (...params: unknown[]) => {
7
+ // eslint-disable-next-line no-console
8
+ console.log(chalk.yellow('Vocab'), ...params);
9
+ };
@@ -0,0 +1,147 @@
1
+ import FormData from 'form-data';
2
+ import { TranslationsByKey } from './../../types/src/index';
3
+ /* eslint-disable no-console */
4
+ import type { TranslationsByLanguage } from '@vocab/types';
5
+ import fetch from 'node-fetch';
6
+ import { log, trace } from './logger';
7
+
8
+ function _callPhrase(path: string, options: Parameters<typeof fetch>[1] = {}) {
9
+ const phraseApiToken = process.env.PHRASE_API_TOKEN;
10
+
11
+ if (!phraseApiToken) {
12
+ throw new Error('Missing PHRASE_API_TOKEN');
13
+ }
14
+
15
+ return fetch(path, {
16
+ ...options,
17
+ headers: {
18
+ Authorization: `token ${phraseApiToken}`,
19
+ // Provide identification via User Agent as requested in https://developers.phrase.com/api/#overview--identification-via-user-agent
20
+ 'User-Agent': 'SEEK Demo Candidate App (jhope@seek.com.au)',
21
+ ...options.headers,
22
+ },
23
+ }).then(async (response) => {
24
+ console.log(`${path}: ${response.status} - ${response.statusText}`);
25
+ console.log(
26
+ `Rate Limit: ${response.headers.get(
27
+ 'X-Rate-Limit-Remaining',
28
+ )} of ${response.headers.get(
29
+ 'X-Rate-Limit-Limit',
30
+ )} remaining. (${response.headers.get(
31
+ 'X-Rate-Limit-Reset',
32
+ )} seconds remaining})`,
33
+ );
34
+ console.log('\nLink:', response.headers.get('Link'), '\n');
35
+ // Print All Headers:
36
+ // console.log(Array.from(r.headers.entries()));
37
+
38
+ try {
39
+ const result = await response.json();
40
+
41
+ console.log(`Internal Result (Length: ${result.length})\n`);
42
+
43
+ if (
44
+ (!options.method || options.method === 'GET') &&
45
+ response.headers.get('Link')?.includes('rel=next')
46
+ ) {
47
+ const [, nextPageUrl] =
48
+ response.headers.get('Link')?.match(/<([^>]*)>; rel=next/) ?? [];
49
+
50
+ if (!nextPageUrl) {
51
+ throw new Error('Cant parse next page URL');
52
+ }
53
+
54
+ console.log('Results recieved with next page: ', nextPageUrl);
55
+
56
+ const nextPageResult = (await _callPhrase(nextPageUrl, options)) as any;
57
+
58
+ return [...result, ...nextPageResult];
59
+ }
60
+
61
+ return result;
62
+ } catch (e) {
63
+ console.error('Unable to parse response as JSON', e);
64
+ return response.text();
65
+ }
66
+ });
67
+ }
68
+
69
+ export async function callPhrase(
70
+ relativePath: string,
71
+ options: Parameters<typeof fetch>[1] = {},
72
+ ) {
73
+ const projectId = process.env.PHRASE_PROJECT_ID;
74
+
75
+ if (!projectId) {
76
+ throw new Error('Missing PHRASE_PROJECT_ID');
77
+ }
78
+ return _callPhrase(
79
+ `https://api.phrase.com/v2/projects/${projectId}/${relativePath}`,
80
+ options,
81
+ )
82
+ .then((result) => {
83
+ if (Array.isArray(result)) {
84
+ console.log('Result length:', result.length);
85
+ }
86
+ return result;
87
+ })
88
+ .catch((error) => {
89
+ console.error(`Error calling phrase for ${relativePath}:`, error);
90
+ throw Error;
91
+ });
92
+ }
93
+
94
+ export async function pullAllTranslations(
95
+ branch: string,
96
+ ): Promise<TranslationsByLanguage> {
97
+ const phraseResult: Array<{
98
+ key: { name: string };
99
+ locale: { code: string };
100
+ content: string;
101
+ }> = await callPhrase(`translations?branch=${branch}&per_page=100`);
102
+ const translations: TranslationsByLanguage = {};
103
+ for (const r of phraseResult) {
104
+ if (!translations[r.locale.code]) {
105
+ translations[r.locale.code] = {};
106
+ }
107
+ translations[r.locale.code][r.key.name] = { message: r.content };
108
+ }
109
+ return translations;
110
+ }
111
+
112
+ export async function pushTranslationsByLocale(
113
+ contents: TranslationsByKey,
114
+ locale: string,
115
+ branch: string,
116
+ ) {
117
+ const formData = new FormData();
118
+ const fileContents = Buffer.from(JSON.stringify(contents));
119
+ formData.append('file', fileContents, {
120
+ contentType: 'application/json',
121
+ filename: `${locale}.json`,
122
+ });
123
+
124
+ formData.append('file_format', 'json');
125
+ formData.append('locale_id', locale);
126
+ formData.append('branch', branch);
127
+ formData.append('update_translations', 'true');
128
+
129
+ trace('Starting to upload:', locale);
130
+
131
+ await callPhrase(`uploads`, {
132
+ method: 'POST',
133
+ body: formData,
134
+ });
135
+ log('Successfully Uploaded:', locale, '\n');
136
+ }
137
+
138
+ export async function ensureBranch(branch: string) {
139
+ await callPhrase(`branches`, {
140
+ method: 'POST',
141
+ headers: {
142
+ 'Content-Type': 'application/json',
143
+ },
144
+ body: JSON.stringify({ name: branch }),
145
+ });
146
+ trace('Created branch:', branch);
147
+ }