@vocab/phrase 1.1.0 → 1.2.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.
package/src/phrase-api.ts DELETED
@@ -1,181 +0,0 @@
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
- trace('\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
- trace(`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("Can't parse next page URL");
52
- }
53
-
54
- console.log('Results received 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<T = any>(
70
- relativePath: string,
71
- options: Parameters<typeof fetch>[1] = {},
72
- ): Promise<T> {
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 = await callPhrase<
98
- Array<{
99
- key: { name: string };
100
- locale: { code: string };
101
- content: string;
102
- }>
103
- >(`translations?branch=${branch}&per_page=100`);
104
-
105
- const translations: TranslationsByLanguage = {};
106
-
107
- for (const r of phraseResult) {
108
- if (!translations[r.locale.code]) {
109
- translations[r.locale.code] = {};
110
- }
111
- translations[r.locale.code][r.key.name] = { message: r.content };
112
- }
113
-
114
- return translations;
115
- }
116
-
117
- export async function pushTranslationsByLocale(
118
- contents: TranslationsByKey,
119
- locale: string,
120
- branch: string,
121
- ) {
122
- const formData = new FormData();
123
- const fileContents = Buffer.from(JSON.stringify(contents));
124
- formData.append('file', fileContents, {
125
- contentType: 'application/json',
126
- filename: `${locale}.json`,
127
- });
128
-
129
- formData.append('file_format', 'json');
130
- formData.append('locale_id', locale);
131
- formData.append('branch', branch);
132
- formData.append('update_translations', 'true');
133
-
134
- log('Starting to upload:', locale, '\n');
135
-
136
- const { id } = await callPhrase<{ id: string }>(`uploads`, {
137
- method: 'POST',
138
- body: formData,
139
- });
140
- log('Upload ID:', id, '\n');
141
- log('Successfully Uploaded:', locale, '\n');
142
-
143
- return { uploadId: id };
144
- }
145
-
146
- export async function deleteUnusedKeys(
147
- uploadId: string,
148
- locale: string,
149
- branch: string,
150
- ) {
151
- const query = `unmentioned_in_upload:${uploadId}`;
152
- const { records_affected } = await callPhrase<{ records_affected: number }>(
153
- 'keys',
154
- {
155
- method: 'DELETE',
156
- headers: {
157
- 'Content-Type': 'application/json',
158
- },
159
- body: JSON.stringify({ branch, locale_id: locale, q: query }),
160
- },
161
- );
162
-
163
- log(
164
- 'Successfully deleted',
165
- records_affected,
166
- 'unused keys from branch',
167
- branch,
168
- );
169
- }
170
-
171
- export async function ensureBranch(branch: string) {
172
- await callPhrase(`branches`, {
173
- method: 'POST',
174
- headers: {
175
- 'Content-Type': 'application/json',
176
- },
177
- body: JSON.stringify({ name: branch }),
178
- });
179
-
180
- log('Created branch:', branch);
181
- }
@@ -1,214 +0,0 @@
1
- import path from 'path';
2
- import { pull } from './pull-translations';
3
- import { pullAllTranslations } from './phrase-api';
4
- import { writeFile } from './file';
5
- import { GeneratedLanguageTarget, LanguageTarget } from '@vocab/types';
6
-
7
- jest.mock('./file', () => ({
8
- writeFile: jest.fn(() => Promise.resolve),
9
- mkdir: jest.fn(() => Promise.resolve),
10
- }));
11
-
12
- jest.mock('./phrase-api', () => ({
13
- ensureBranch: jest.fn(() => Promise.resolve()),
14
- pullAllTranslations: jest.fn(() => Promise.resolve({ en: {}, fr: {} })),
15
- }));
16
-
17
- const devLanguage = 'en';
18
-
19
- function runPhrase(options: {
20
- languages: LanguageTarget[];
21
- generatedLanguages: GeneratedLanguageTarget[];
22
- }) {
23
- return pull(
24
- { branch: 'tester' },
25
- {
26
- ...options,
27
- devLanguage,
28
- projectRoot: path.resolve(__dirname, '..', '..', '..', 'fixtures/phrase'),
29
- },
30
- );
31
- }
32
-
33
- describe('pull translations', () => {
34
- describe('when pulling translations for languages that already have translations', () => {
35
- beforeEach(() => {
36
- jest.mocked(pullAllTranslations).mockClear();
37
- jest.mocked(writeFile).mockClear();
38
- jest.mocked(pullAllTranslations).mockImplementation(() =>
39
- Promise.resolve({
40
- en: {
41
- 'hello.mytranslations': {
42
- message: 'Hi there',
43
- },
44
- },
45
- fr: {
46
- 'hello.mytranslations': {
47
- message: 'merci',
48
- },
49
- },
50
- }),
51
- );
52
- });
53
-
54
- const options = {
55
- languages: [{ name: 'en' }, { name: 'fr' }],
56
- generatedLanguages: [
57
- {
58
- name: 'generatedLanguage',
59
- extends: 'en',
60
- generator: {
61
- transformMessage: (message: string) => `[${message}]`,
62
- },
63
- },
64
- ],
65
- };
66
-
67
- it('should resolve', async () => {
68
- await expect(runPhrase(options)).resolves.toBeUndefined();
69
-
70
- expect(jest.mocked(writeFile)).toHaveBeenCalledTimes(2);
71
- });
72
-
73
- it('should update keys', async () => {
74
- await expect(runPhrase(options)).resolves.toBeUndefined();
75
-
76
- expect(
77
- jest
78
- .mocked(writeFile)
79
- .mock.calls.map(([_filePath, contents]) =>
80
- JSON.parse(contents as string),
81
- ),
82
- ).toMatchInlineSnapshot(`
83
- [
84
- {
85
- "hello": {
86
- "message": "Hi there",
87
- },
88
- "world": {
89
- "message": "world",
90
- },
91
- },
92
- {
93
- "hello": {
94
- "message": "merci",
95
- },
96
- "world": {
97
- "message": "monde",
98
- },
99
- },
100
- ]
101
- `);
102
- });
103
- });
104
-
105
- describe('when pulling translations and some languages do not have any translations', () => {
106
- beforeEach(() => {
107
- jest.mocked(pullAllTranslations).mockClear();
108
- jest.mocked(writeFile).mockClear();
109
- jest.mocked(pullAllTranslations).mockImplementation(() =>
110
- Promise.resolve({
111
- en: {
112
- 'hello.mytranslations': {
113
- message: 'Hi there',
114
- },
115
- },
116
- fr: {
117
- 'hello.mytranslations': {
118
- message: 'merci',
119
- },
120
- },
121
- }),
122
- );
123
- });
124
-
125
- const options = {
126
- languages: [{ name: 'en' }, { name: 'fr' }, { name: 'ja' }],
127
- generatedLanguages: [
128
- {
129
- name: 'generatedLanguage',
130
- extends: 'en',
131
- generator: {
132
- transformMessage: (message: string) => `[${message}]`,
133
- },
134
- },
135
- ],
136
- };
137
-
138
- it('should resolve', async () => {
139
- await expect(runPhrase(options)).resolves.toBeUndefined();
140
-
141
- expect(jest.mocked(writeFile)).toHaveBeenCalledTimes(2);
142
- });
143
-
144
- it('should update keys', async () => {
145
- await expect(runPhrase(options)).resolves.toBeUndefined();
146
-
147
- expect(
148
- jest
149
- .mocked(writeFile)
150
- .mock.calls.map(([_filePath, contents]) =>
151
- JSON.parse(contents as string),
152
- ),
153
- ).toMatchInlineSnapshot(`
154
- [
155
- {
156
- "hello": {
157
- "message": "Hi there",
158
- },
159
- "world": {
160
- "message": "world",
161
- },
162
- },
163
- {
164
- "hello": {
165
- "message": "merci",
166
- },
167
- "world": {
168
- "message": "monde",
169
- },
170
- },
171
- ]
172
- `);
173
- });
174
- });
175
-
176
- describe('when pulling translations and the project has not configured translations for the dev language', () => {
177
- beforeEach(() => {
178
- jest.mocked(pullAllTranslations).mockClear();
179
- jest.mocked(writeFile).mockClear();
180
- jest.mocked(pullAllTranslations).mockImplementation(() =>
181
- Promise.resolve({
182
- fr: {
183
- 'hello.mytranslations': {
184
- message: 'merci',
185
- },
186
- },
187
- }),
188
- );
189
- });
190
-
191
- const options = {
192
- languages: [{ name: 'en' }, { name: 'fr' }],
193
- generatedLanguages: [
194
- {
195
- name: 'generatedLanguage',
196
- extends: 'en',
197
- generator: {
198
- transformMessage: (message: string) => `[${message}]`,
199
- },
200
- },
201
- ],
202
- };
203
-
204
- it('should throw an error', async () => {
205
- await expect(runPhrase(options)).rejects.toThrow(
206
- new Error(
207
- `Phrase did not return any translations for the configured development language "en".\nPlease ensure this language is present in your Phrase project's configuration.`,
208
- ),
209
- );
210
-
211
- expect(jest.mocked(writeFile)).toHaveBeenCalledTimes(0);
212
- });
213
- });
214
- });
@@ -1,114 +0,0 @@
1
- import { writeFile, mkdir } from './file';
2
- import path from 'path';
3
-
4
- import {
5
- loadAllTranslations,
6
- getAltLanguageFilePath,
7
- getAltLanguages,
8
- getUniqueKey,
9
- } from '@vocab/core';
10
- import type { UserConfig } from '@vocab/types';
11
-
12
- import { pullAllTranslations, ensureBranch } from './phrase-api';
13
- import { trace } from './logger';
14
-
15
- interface PullOptions {
16
- branch?: string;
17
- deleteUnusedKeys?: boolean;
18
- }
19
-
20
- export async function pull(
21
- { branch = 'local-development' }: PullOptions,
22
- config: UserConfig,
23
- ) {
24
- trace(`Pulling translations from branch ${branch}`);
25
- await ensureBranch(branch);
26
- const alternativeLanguages = getAltLanguages(config);
27
- const allPhraseTranslations = await pullAllTranslations(branch);
28
- trace(
29
- `Pulling translations from Phrase for languages ${
30
- config.devLanguage
31
- } and ${alternativeLanguages.join(', ')}`,
32
- );
33
-
34
- const phraseLanguages = Object.keys(allPhraseTranslations);
35
- trace(
36
- `Found Phrase translations for languages ${phraseLanguages.join(', ')}`,
37
- );
38
-
39
- if (!phraseLanguages.includes(config.devLanguage)) {
40
- throw new Error(
41
- `Phrase did not return any translations for the configured development language "${config.devLanguage}".\nPlease ensure this language is present in your Phrase project's configuration.`,
42
- );
43
- }
44
-
45
- const allVocabTranslations = await loadAllTranslations(
46
- { fallbacks: 'none', includeNodeModules: false },
47
- config,
48
- );
49
-
50
- for (const loadedTranslation of allVocabTranslations) {
51
- const devTranslations = loadedTranslation.languages[config.devLanguage];
52
-
53
- if (!devTranslations) {
54
- throw new Error('No dev language translations loaded');
55
- }
56
-
57
- const defaultValues = { ...devTranslations };
58
- const localKeys = Object.keys(defaultValues);
59
-
60
- for (const key of localKeys) {
61
- defaultValues[key] = {
62
- ...defaultValues[key],
63
- ...allPhraseTranslations[config.devLanguage][
64
- getUniqueKey(key, loadedTranslation.namespace)
65
- ],
66
- };
67
- }
68
- await writeFile(
69
- loadedTranslation.filePath,
70
- `${JSON.stringify(defaultValues, null, 2)}\n`,
71
- );
72
-
73
- for (const alternativeLanguage of alternativeLanguages) {
74
- if (alternativeLanguage in allPhraseTranslations) {
75
- const altTranslations = {
76
- ...loadedTranslation.languages[alternativeLanguage],
77
- };
78
- const phraseAltTranslations =
79
- allPhraseTranslations[alternativeLanguage];
80
-
81
- for (const key of localKeys) {
82
- const phraseKey = getUniqueKey(key, loadedTranslation.namespace);
83
- const phraseTranslationMessage =
84
- phraseAltTranslations[phraseKey]?.message;
85
-
86
- if (!phraseTranslationMessage) {
87
- trace(
88
- `Missing translation. No translation for key ${key} in phrase as ${phraseKey} in language ${alternativeLanguage}.`,
89
- );
90
- continue;
91
- }
92
-
93
- altTranslations[key] = {
94
- ...altTranslations[key],
95
- message: phraseTranslationMessage,
96
- };
97
- }
98
-
99
- const altTranslationFilePath = getAltLanguageFilePath(
100
- loadedTranslation.filePath,
101
- alternativeLanguage,
102
- );
103
-
104
- await mkdir(path.dirname(altTranslationFilePath), {
105
- recursive: true,
106
- });
107
- await writeFile(
108
- altTranslationFilePath,
109
- `${JSON.stringify(altTranslations, null, 2)}\n`,
110
- );
111
- }
112
- }
113
- }
114
- }
@@ -1,168 +0,0 @@
1
- import path from 'path';
2
- import { push } from './push-translations';
3
- import { pushTranslationsByLocale, deleteUnusedKeys } from './phrase-api';
4
- import { writeFile } from './file';
5
-
6
- jest.mock('./file', () => ({
7
- writeFile: jest.fn(() => Promise.resolve),
8
- mkdir: jest.fn(() => Promise.resolve),
9
- }));
10
-
11
- jest.mock('./phrase-api', () => ({
12
- ensureBranch: jest.fn(() => Promise.resolve()),
13
- pushTranslationsByLocale: jest.fn(() => Promise.resolve({ en: {}, fr: {} })),
14
- deleteUnusedKeys: jest.fn(() => Promise.resolve()),
15
- }));
16
-
17
- const uploadId = '1234';
18
-
19
- function runPhrase(config: { deleteUnusedKeys: boolean }) {
20
- return push(
21
- { branch: 'tester', deleteUnusedKeys: config.deleteUnusedKeys },
22
- {
23
- devLanguage: 'en',
24
- languages: [{ name: 'en' }, { name: 'fr' }],
25
- generatedLanguages: [
26
- {
27
- name: 'generatedLanguage',
28
- extends: 'en',
29
- generator: {
30
- transformMessage: (message: string) => `[${message}]`,
31
- },
32
- },
33
- ],
34
- projectRoot: path.resolve(__dirname, '..', '..', '..', 'fixtures/phrase'),
35
- },
36
- );
37
- }
38
-
39
- describe('push', () => {
40
- describe('when deleteUnusedKeys is false', () => {
41
- const config = { deleteUnusedKeys: false };
42
-
43
- beforeEach(() => {
44
- jest.mocked(pushTranslationsByLocale).mockClear();
45
- jest.mocked(writeFile).mockClear();
46
- jest.mocked(deleteUnusedKeys).mockClear();
47
-
48
- jest
49
- .mocked(pushTranslationsByLocale)
50
- .mockImplementation(() => Promise.resolve({ uploadId }));
51
- });
52
-
53
- it('should resolve', async () => {
54
- await expect(runPhrase(config)).resolves.toBeUndefined();
55
-
56
- expect(jest.mocked(pushTranslationsByLocale)).toHaveBeenCalledTimes(2);
57
- });
58
-
59
- it('should update keys', async () => {
60
- await expect(runPhrase(config)).resolves.toBeUndefined();
61
-
62
- expect(jest.mocked(pushTranslationsByLocale)).toHaveBeenCalledWith(
63
- {
64
- 'hello.mytranslations': {
65
- message: 'Hello',
66
- },
67
- 'world.mytranslations': {
68
- message: 'world',
69
- },
70
- },
71
- 'en',
72
- 'tester',
73
- );
74
-
75
- expect(jest.mocked(pushTranslationsByLocale)).toHaveBeenCalledWith(
76
- {
77
- 'hello.mytranslations': {
78
- message: 'Bonjour',
79
- },
80
- 'world.mytranslations': {
81
- message: 'monde',
82
- },
83
- },
84
- 'fr',
85
- 'tester',
86
- );
87
- });
88
-
89
- it('should not delete unused keys', () => {
90
- expect(deleteUnusedKeys).not.toHaveBeenCalled();
91
- });
92
- });
93
-
94
- describe('when deleteUnusedKeys is true', () => {
95
- const config = { deleteUnusedKeys: true };
96
-
97
- beforeEach(() => {
98
- jest.mocked(pushTranslationsByLocale).mockClear();
99
- jest.mocked(writeFile).mockClear();
100
- jest.mocked(deleteUnusedKeys).mockClear();
101
- });
102
-
103
- describe('and the upload succeeds', () => {
104
- beforeEach(() => {
105
- jest
106
- .mocked(pushTranslationsByLocale)
107
- .mockImplementation(() => Promise.resolve({ uploadId }));
108
- });
109
-
110
- it('should resolve', async () => {
111
- await expect(runPhrase(config)).resolves.toBeUndefined();
112
-
113
- expect(jest.mocked(pushTranslationsByLocale)).toHaveBeenCalledTimes(2);
114
- });
115
-
116
- it('should update keys', async () => {
117
- await expect(runPhrase(config)).resolves.toBeUndefined();
118
-
119
- expect(jest.mocked(pushTranslationsByLocale)).toHaveBeenCalledWith(
120
- {
121
- 'hello.mytranslations': {
122
- message: 'Hello',
123
- },
124
- 'world.mytranslations': {
125
- message: 'world',
126
- },
127
- },
128
- 'en',
129
- 'tester',
130
- );
131
-
132
- expect(jest.mocked(pushTranslationsByLocale)).toHaveBeenCalledWith(
133
- {
134
- 'hello.mytranslations': {
135
- message: 'Bonjour',
136
- },
137
- 'world.mytranslations': {
138
- message: 'monde',
139
- },
140
- },
141
- 'fr',
142
- 'tester',
143
- );
144
- });
145
-
146
- it('should delete unused keys', async () => {
147
- await expect(runPhrase(config)).resolves.toBeUndefined();
148
-
149
- expect(deleteUnusedKeys).toHaveBeenCalledWith(uploadId, 'en', 'tester');
150
- expect(deleteUnusedKeys).toHaveBeenCalledWith(uploadId, 'fr', 'tester');
151
- });
152
- });
153
-
154
- describe('and the upload fails', () => {
155
- beforeEach(() => {
156
- jest
157
- .mocked(pushTranslationsByLocale)
158
- .mockImplementation(() => Promise.reject('Upload failed'));
159
- });
160
-
161
- it('should not delete unused keys', async () => {
162
- await expect(runPhrase(config)).rejects.toBe('Upload failed');
163
-
164
- expect(deleteUnusedKeys).not.toHaveBeenCalled();
165
- });
166
- });
167
- });
168
- });