@vocab/phrase 1.0.0 → 1.1.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.
- package/CHANGELOG.md +29 -0
- package/README.md +154 -4
- package/dist/declarations/src/phrase-api.d.ts +5 -2
- package/dist/declarations/src/pull-translations.d.ts +1 -0
- package/dist/declarations/src/push-translations.d.ts +2 -1
- package/dist/vocab-phrase.cjs.dev.js +67 -29
- package/dist/vocab-phrase.cjs.prod.js +67 -29
- package/dist/vocab-phrase.esm.js +67 -29
- package/package.json +1 -1
- package/src/phrase-api.ts +48 -15
- package/src/pull-translations.test.ts +179 -45
- package/src/pull-translations.ts +47 -32
- package/src/push-translations.test.ts +138 -35
- package/src/push-translations.ts +15 -3
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { push } from './push-translations';
|
|
3
|
-
import { pushTranslationsByLocale } from './phrase-api';
|
|
3
|
+
import { pushTranslationsByLocale, deleteUnusedKeys } from './phrase-api';
|
|
4
4
|
import { writeFile } from './file';
|
|
5
5
|
|
|
6
6
|
jest.mock('./file', () => ({
|
|
@@ -11,55 +11,158 @@ jest.mock('./file', () => ({
|
|
|
11
11
|
jest.mock('./phrase-api', () => ({
|
|
12
12
|
ensureBranch: jest.fn(() => Promise.resolve()),
|
|
13
13
|
pushTranslationsByLocale: jest.fn(() => Promise.resolve({ en: {}, fr: {} })),
|
|
14
|
+
deleteUnusedKeys: jest.fn(() => Promise.resolve()),
|
|
14
15
|
}));
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
const uploadId = '1234';
|
|
18
|
+
|
|
19
|
+
function runPhrase(config: { deleteUnusedKeys: boolean }) {
|
|
17
20
|
return push(
|
|
18
|
-
{ branch: 'tester' },
|
|
21
|
+
{ branch: 'tester', deleteUnusedKeys: config.deleteUnusedKeys },
|
|
19
22
|
{
|
|
20
23
|
devLanguage: 'en',
|
|
21
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
|
+
],
|
|
22
34
|
projectRoot: path.resolve(__dirname, '..', '..', '..', 'fixtures/phrase'),
|
|
23
35
|
},
|
|
24
36
|
);
|
|
25
37
|
}
|
|
26
38
|
|
|
27
39
|
describe('push', () => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
(writeFile as jest.Mock).mockClear();
|
|
31
|
-
});
|
|
32
|
-
it('should resolve', async () => {
|
|
33
|
-
await expect(runPhrase()).resolves.toBeUndefined();
|
|
40
|
+
describe('when deleteUnusedKeys is false', () => {
|
|
41
|
+
const config = { deleteUnusedKeys: false };
|
|
34
42
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
jest.mocked(pushTranslationsByLocale).mockClear();
|
|
45
|
+
jest.mocked(writeFile).mockClear();
|
|
46
|
+
jest.mocked(deleteUnusedKeys).mockClear();
|
|
39
47
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
+
},
|
|
56
70
|
},
|
|
57
|
-
'
|
|
58
|
-
|
|
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
|
+
},
|
|
59
83
|
},
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
);
|
|
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
|
+
});
|
|
64
167
|
});
|
|
65
168
|
});
|
package/src/push-translations.ts
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
import { TranslationsByLanguage } from './../../types/src/index';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
ensureBranch,
|
|
5
|
+
pushTranslationsByLocale,
|
|
6
|
+
deleteUnusedKeys as phraseDeleteUnusedKeys,
|
|
7
|
+
} from './phrase-api';
|
|
4
8
|
import { trace } from './logger';
|
|
5
9
|
import { loadAllTranslations, getUniqueKey } from '@vocab/core';
|
|
6
10
|
import { UserConfig } from '@vocab/types';
|
|
7
11
|
|
|
8
12
|
interface PushOptions {
|
|
9
13
|
branch: string;
|
|
14
|
+
deleteUnusedKeys?: boolean;
|
|
10
15
|
}
|
|
11
16
|
|
|
12
17
|
/**
|
|
13
18
|
* Uploading to the Phrase API for each language. Adding a unique namespace to each key using file path they key came from
|
|
14
19
|
*/
|
|
15
|
-
export async function push(
|
|
20
|
+
export async function push(
|
|
21
|
+
{ branch, deleteUnusedKeys }: PushOptions,
|
|
22
|
+
config: UserConfig,
|
|
23
|
+
) {
|
|
16
24
|
const allLanguageTranslations = await loadAllTranslations(
|
|
17
25
|
{ fallbacks: 'none', includeNodeModules: false },
|
|
18
26
|
config,
|
|
@@ -45,11 +53,15 @@ export async function push({ branch }: PushOptions, config: UserConfig) {
|
|
|
45
53
|
|
|
46
54
|
for (const language of allLanguages) {
|
|
47
55
|
if (phraseTranslations[language]) {
|
|
48
|
-
await pushTranslationsByLocale(
|
|
56
|
+
const { uploadId } = await pushTranslationsByLocale(
|
|
49
57
|
phraseTranslations[language],
|
|
50
58
|
language,
|
|
51
59
|
branch,
|
|
52
60
|
);
|
|
61
|
+
|
|
62
|
+
if (deleteUnusedKeys) {
|
|
63
|
+
await phraseDeleteUnusedKeys(uploadId, language, branch);
|
|
64
|
+
}
|
|
53
65
|
}
|
|
54
66
|
}
|
|
55
67
|
}
|