@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.
@@ -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
- function runPhrase() {
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
- beforeEach(() => {
29
- (pushTranslationsByLocale as jest.Mock).mockClear();
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
- expect(pushTranslationsByLocale as jest.Mock).toHaveBeenCalledTimes(2);
36
- });
37
- it('should update keys', async () => {
38
- await expect(runPhrase()).resolves.toBeUndefined();
43
+ beforeEach(() => {
44
+ jest.mocked(pushTranslationsByLocale).mockClear();
45
+ jest.mocked(writeFile).mockClear();
46
+ jest.mocked(deleteUnusedKeys).mockClear();
39
47
 
40
- expect(pushTranslationsByLocale as jest.Mock).toHaveBeenCalledWith(
41
- {
42
- 'hello.mytranslations': {
43
- message: 'Hello',
44
- },
45
- 'world.mytranslations': {
46
- message: 'world',
47
- },
48
- },
49
- 'en',
50
- 'tester',
51
- );
52
- expect(pushTranslationsByLocale as jest.Mock).toHaveBeenCalledWith(
53
- {
54
- 'hello.mytranslations': {
55
- message: 'Bonjour',
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
- 'world.mytranslations': {
58
- message: 'monde',
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
- 'fr',
62
- 'tester',
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
  });
@@ -1,18 +1,26 @@
1
1
  import { TranslationsByLanguage } from './../../types/src/index';
2
2
 
3
- import { ensureBranch, pushTranslationsByLocale } from './phrase-api';
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({ branch }: PushOptions, config: UserConfig) {
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
  }