auto-translate-json-library 2.0.2 → 2.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.
Files changed (43) hide show
  1. package/README.MD +305 -3
  2. package/build/package.json +31 -16
  3. package/build/src/bin/cli.d.ts +1 -1
  4. package/build/src/bin/cli.js +253 -58
  5. package/build/src/config.d.ts +12 -1
  6. package/build/src/files.js +2 -2
  7. package/build/src/format-detector.js +1 -1
  8. package/build/src/huggingface-local.d.ts +11 -0
  9. package/build/src/huggingface-local.js +71 -0
  10. package/build/src/huggingface.d.ts +12 -0
  11. package/build/src/huggingface.js +76 -0
  12. package/build/src/index.js +16 -0
  13. package/build/src/lib.js +10 -26
  14. package/build/src/openai.js +1 -0
  15. package/build/src/provider-factory.d.ts +3 -0
  16. package/build/src/provider-factory.js +38 -0
  17. package/package.json +31 -16
  18. package/build/tests/arb-handler.test.d.ts +0 -1
  19. package/build/tests/arb-handler.test.js +0 -694
  20. package/build/tests/cli-integration.test.d.ts +0 -1
  21. package/build/tests/cli-integration.test.js +0 -352
  22. package/build/tests/csv-handler.test.d.ts +0 -1
  23. package/build/tests/csv-handler.test.js +0 -507
  24. package/build/tests/error-handling.test.d.ts +0 -1
  25. package/build/tests/error-handling.test.js +0 -456
  26. package/build/tests/files-integration.test.d.ts +0 -1
  27. package/build/tests/files-integration.test.js +0 -421
  28. package/build/tests/json-handler.test.d.ts +0 -1
  29. package/build/tests/json-handler.test.js +0 -526
  30. package/build/tests/po-handler.test.d.ts +0 -1
  31. package/build/tests/po-handler.test.js +0 -768
  32. package/build/tests/properties-handler.test.d.ts +0 -1
  33. package/build/tests/properties-handler.test.js +0 -316
  34. package/build/tests/setup.d.ts +0 -1
  35. package/build/tests/setup.js +0 -5
  36. package/build/tests/xliff-handler.test.d.ts +0 -1
  37. package/build/tests/xliff-handler.test.js +0 -459
  38. package/build/tests/xmb-xtb-handler.test.d.ts +0 -1
  39. package/build/tests/xmb-xtb-handler.test.js +0 -758
  40. package/build/tests/xml-handler.test.d.ts +0 -1
  41. package/build/tests/xml-handler.test.js +0 -570
  42. package/build/tests/yaml-handler.test.d.ts +0 -1
  43. package/build/tests/yaml-handler.test.js +0 -156
@@ -1 +0,0 @@
1
- export {};
@@ -1,352 +0,0 @@
1
- import { spawn } from 'node:child_process';
2
- import { fileURLToPath } from 'url';
3
- import { dirname } from 'path';
4
- const __filename = fileURLToPath(import.meta.url);
5
- const __dirname = dirname(__filename);
6
- import * as fs from 'node:fs';
7
- import * as path from 'node:path';
8
- describe('CLI Integration Tests', () => {
9
- const testDir = path.join(__dirname, 'test-cli-integration');
10
- const cliPath = path.join(__dirname, '..', 'build', 'src', 'bin', 'cli.js');
11
- beforeAll(() => {
12
- // Create test directory
13
- if (!fs.existsSync(testDir)) {
14
- fs.mkdirSync(testDir, { recursive: true });
15
- }
16
- });
17
- afterAll(() => {
18
- // Clean up test directory
19
- if (fs.existsSync(testDir)) {
20
- fs.rmSync(testDir, { recursive: true, force: true });
21
- }
22
- });
23
- // Helper function to run CLI commands
24
- const runCLI = (args, options = {}) => {
25
- return new Promise((resolve) => {
26
- const child = spawn('node', [cliPath, ...args], {
27
- cwd: testDir,
28
- env: { ...process.env, NODE_ENV: 'test' },
29
- ...options
30
- });
31
- let stdout = '';
32
- let stderr = '';
33
- child.stdout?.on('data', (data) => {
34
- stdout += data.toString();
35
- });
36
- child.stderr?.on('data', (data) => {
37
- stderr += data.toString();
38
- });
39
- child.on('close', (code) => {
40
- resolve({
41
- stdout,
42
- stderr,
43
- exitCode: code || 0
44
- });
45
- });
46
- });
47
- };
48
- describe('Help Documentation and Format Listing', () => {
49
- it('should display help when no arguments provided', async () => {
50
- const result = await runCLI([]);
51
- expect(result.exitCode).toBe(0);
52
- expect(result.stdout).toContain('Auto translate json/xml cli');
53
- expect(result.stdout).toContain('Usage: atj [options] <inputPath>');
54
- expect(result.stdout).toContain('Options:');
55
- expect(result.stdout).toContain('--help, -h');
56
- expect(result.stdout).toContain('--list-formats');
57
- expect(result.stdout).toContain('--format, -f');
58
- expect(result.stdout).toContain('--engine, -e');
59
- expect(result.stdout).toContain('--sourceLocale, -s');
60
- });
61
- it('should display help with --help flag', async () => {
62
- const result = await runCLI(['--help']);
63
- expect(result.exitCode).toBe(0);
64
- expect(result.stdout).toContain('Auto translate json/xml cli');
65
- expect(result.stdout).toContain('Usage: atj [options] <inputPath>');
66
- expect(result.stdout).toContain('Supported formats:');
67
- expect(result.stdout).toContain('JSON-based:');
68
- expect(result.stdout).toContain('XML-based:');
69
- expect(result.stdout).toContain('Text-based:');
70
- expect(result.stdout).toContain('Tabular:');
71
- expect(result.stdout).toContain('Format examples:');
72
- expect(result.stdout).toContain('Usage examples:');
73
- });
74
- it('should display help with -h flag', async () => {
75
- const result = await runCLI(['-h']);
76
- expect(result.exitCode).toBe(0);
77
- expect(result.stdout).toContain('Auto translate json/xml cli');
78
- expect(result.stdout).toContain('Usage: atj [options] <inputPath>');
79
- });
80
- it('should list supported formats with --list-formats', async () => {
81
- const result = await runCLI(['--list-formats']);
82
- expect(result.exitCode).toBe(0);
83
- expect(result.stdout).toContain('Auto translate json/xml cli');
84
- expect(result.stdout).toContain('Supported file formats:');
85
- // Check for expected format entries
86
- const expectedFormats = [
87
- 'json',
88
- 'arb',
89
- 'xml',
90
- 'android-xml',
91
- 'ios-xml',
92
- 'xliff',
93
- 'xmb',
94
- 'xtb',
95
- 'po',
96
- 'pot',
97
- 'yaml',
98
- 'properties',
99
- 'csv',
100
- 'tsv'
101
- ];
102
- expectedFormats.forEach(format => {
103
- expect(result.stdout).toContain(format);
104
- });
105
- // Check for format descriptions
106
- expect(result.stdout).toContain('JSON translation files');
107
- expect(result.stdout).toContain('Flutter Application Resource Bundle');
108
- expect(result.stdout).toContain('Android strings.xml format');
109
- expect(result.stdout).toContain('XLIFF 1.2 and 2.x translation files');
110
- expect(result.stdout).toContain('GNU gettext PO files');
111
- expect(result.stdout).toContain('YAML translation files');
112
- expect(result.stdout).toContain('Java Properties files');
113
- expect(result.stdout).toContain('Comma-separated values files');
114
- // Check for usage examples
115
- expect(result.stdout).toContain('Usage examples:');
116
- expect(result.stdout).toContain('Best practices:');
117
- });
118
- });
119
- describe('Format Parameter Handling', () => {
120
- beforeEach(() => {
121
- // Create test files for different formats
122
- const testFiles = {
123
- 'test.json': '{"hello": "Hello", "world": "World"}',
124
- 'test.xml': '<?xml version="1.0" encoding="UTF-8"?>\n<resources>\n <string name="hello">Hello</string>\n <string name="world">World</string>\n</resources>',
125
- 'test.arb': '{\n "@@locale": "en",\n "hello": "Hello",\n "@hello": {\n "description": "Greeting"\n }\n}',
126
- 'test.xlf': '<?xml version="1.0" encoding="UTF-8"?>\n<xliff version="1.2">\n <file source-language="en" target-language="es">\n <body>\n <trans-unit id="1">\n <source>Hello</source>\n <target>Hola</target>\n </trans-unit>\n </body>\n </file>\n</xliff>',
127
- 'test.po': 'msgid ""\nmsgstr ""\n\nmsgid "hello"\nmsgstr "Hello"',
128
- 'test.yaml': 'hello: Hello\nworld: World',
129
- 'test.properties': 'hello=Hello\nworld=World',
130
- 'test.csv': 'key,en\nhello,Hello\nworld,World'
131
- };
132
- Object.entries(testFiles).forEach(([filename, content]) => {
133
- fs.writeFileSync(path.join(testDir, filename), content);
134
- });
135
- });
136
- it('should validate format parameter against supported formats', async () => {
137
- const result = await runCLI(['--format', 'unsupported', 'test.json']);
138
- expect(result.exitCode).toBe(1);
139
- expect(result.stderr).toContain('Unsupported format: unsupported');
140
- expect(result.stderr).toContain('Use --list-formats to see all supported formats');
141
- });
142
- it('should accept valid format parameters', async () => {
143
- const validFormats = ['json', 'xml', 'yaml', 'properties', 'csv'];
144
- for (const format of validFormats) {
145
- const result = await runCLI(['--format', format, `test.${format === 'xml' ? 'xml' : format}`]);
146
- // Should not exit with format validation error
147
- if (result.exitCode === 1) {
148
- // If it exits with 1, it should be due to missing translation credentials, not format validation
149
- expect(result.stderr).not.toContain('Unsupported format');
150
- expect(result.stderr).toContain('not found in environment variable');
151
- }
152
- }
153
- });
154
- it('should handle format parameter with short flag -f', async () => {
155
- const result = await runCLI(['-f', 'json', 'test.json']);
156
- // Should not exit with format validation error
157
- if (result.exitCode === 1) {
158
- expect(result.stderr).not.toContain('Unsupported format');
159
- expect(result.stderr).toContain('not found in environment variable');
160
- }
161
- });
162
- it('should override auto-detection when format is specified', async () => {
163
- // Use a JSON file but specify XML format - should attempt to parse as XML
164
- const result = await runCLI(['--format', 'xml', 'test.json']);
165
- // Should not exit with format validation error
166
- if (result.exitCode === 1) {
167
- expect(result.stderr).not.toContain('Unsupported format');
168
- // May fail due to parsing or missing credentials, but format should be accepted
169
- }
170
- });
171
- });
172
- describe('CLI Consistency Across Formats', () => {
173
- beforeEach(() => {
174
- // Create test files for different formats
175
- const testFiles = {
176
- 'consistency-test.json': '{"hello": "Hello"}',
177
- 'consistency-test.xml': '<?xml version="1.0"?>\n<resources>\n <string name="hello">Hello</string>\n</resources>',
178
- 'consistency-test.yaml': 'hello: Hello',
179
- 'consistency-test.properties': 'hello=Hello'
180
- };
181
- Object.entries(testFiles).forEach(([filename, content]) => {
182
- fs.writeFileSync(path.join(testDir, filename), content);
183
- });
184
- });
185
- it('should validate input path requirement consistently', async () => {
186
- const result = await runCLI(['--format', 'json']);
187
- expect(result.exitCode).toBe(1);
188
- expect(result.stderr).toContain('Input path is required');
189
- expect(result.stderr).toContain('Usage: atj [options] <inputPath>');
190
- });
191
- it('should validate file existence consistently across formats', async () => {
192
- const formats = ['json', 'xml', 'yaml', 'properties'];
193
- for (const format of formats) {
194
- const result = await runCLI(['--format', format, 'nonexistent-file.txt']);
195
- expect(result.exitCode).toBe(1);
196
- expect(result.stderr).toContain('Input path does not exist');
197
- }
198
- });
199
- it('should validate mode parameter consistently', async () => {
200
- const result = await runCLI(['--mode', 'invalid', 'consistency-test.json']);
201
- expect(result.exitCode).toBe(1);
202
- expect(result.stderr).toContain('Invalid mode: invalid');
203
- expect(result.stderr).toContain("Mode must be either 'file' or 'folder'");
204
- });
205
- it('should validate engine parameter consistently', async () => {
206
- const result = await runCLI(['--engine', 'invalid', 'consistency-test.json']);
207
- expect(result.exitCode).toBe(1);
208
- expect(result.stderr).toContain('Invalid engine: invalid');
209
- expect(result.stderr).toContain('Supported engines:');
210
- expect(result.stderr).toContain('aws, azure, google, deepLPro, deepLFree, openai');
211
- });
212
- it('should handle missing translation credentials consistently', async () => {
213
- const formats = ['json', 'xml', 'yaml', 'properties'];
214
- for (const format of formats) {
215
- const result = await runCLI(['--format', format, '--engine', 'aws', `consistency-test.${format}`]);
216
- expect(result.exitCode).toBe(1);
217
- expect(result.stderr).toContain('AWS credentials not found in environment variables');
218
- expect(result.stderr).toContain('ATJ_AWS_ACCESS_KEY_ID, ATJ_AWS_SECRET_ACCESS_KEY, ATJ_AWS_REGION');
219
- }
220
- });
221
- it('should accept all common CLI options across formats', async () => {
222
- const formats = ['json', 'xml', 'yaml', 'properties'];
223
- const commonOptions = [
224
- ['--mode', 'file'],
225
- ['--sourceLocale', 'en'],
226
- ['--keepTranslations'],
227
- ['--no-keepExtraTranslations']
228
- ];
229
- for (const format of formats) {
230
- for (const option of commonOptions) {
231
- const args = ['--format', format, ...option, `consistency-test.${format}`];
232
- const result = await runCLI(args);
233
- // Should not fail due to invalid options (may fail due to missing credentials)
234
- if (result.exitCode === 1) {
235
- expect(result.stderr).not.toContain('Unknown option');
236
- expect(result.stderr).not.toContain('Invalid option');
237
- }
238
- }
239
- }
240
- }, 60000); // 60 second timeout for CLI integration test
241
- it('should provide consistent progress reporting format', async () => {
242
- const formats = ['json', 'xml'];
243
- for (const format of formats) {
244
- const result = await runCLI(['--format', format, `consistency-test.${format}`]);
245
- // All formats should show the same initial output pattern
246
- expect(result.stdout).toContain('Auto translate json cli');
247
- // May not reach "Translating" due to missing credentials, but should show consistent startup
248
- if (result.stdout.includes('Translating')) {
249
- // Should show consistent path formatting
250
- const outputLines = result.stdout.split('\n');
251
- const translatingLine = outputLines.find(line => line.includes('Translating'));
252
- expect(translatingLine).toBeDefined();
253
- expect(translatingLine).toContain(testDir);
254
- }
255
- }
256
- });
257
- it('should handle folder mode consistently across formats', async () => {
258
- // Create a folder structure with different format files
259
- const folderPath = path.join(testDir, 'multi-format');
260
- if (!fs.existsSync(folderPath)) {
261
- fs.mkdirSync(folderPath);
262
- }
263
- const folderFiles = {
264
- 'app-en.json': '{"hello": "Hello"}',
265
- 'strings-en.xml': '<?xml version="1.0"?>\n<resources>\n <string name="hello">Hello</string>\n</resources>',
266
- 'messages-en.yaml': 'hello: Hello',
267
- 'labels-en.properties': 'hello=Hello'
268
- };
269
- Object.entries(folderFiles).forEach(([filename, content]) => {
270
- fs.writeFileSync(path.join(folderPath, filename), content);
271
- });
272
- const result = await runCLI(['--mode', 'folder', folderPath]);
273
- // Should attempt to process the folder (may fail due to missing credentials)
274
- expect(result.stdout).toContain('Auto translate json cli');
275
- // May not reach "Translating" due to missing credentials, but should show consistent startup
276
- if (result.stdout.includes('Translating')) {
277
- expect(result.stdout).toContain('Translating');
278
- }
279
- if (result.exitCode === 1) {
280
- // Should fail consistently due to missing credentials, not format issues
281
- expect(result.stderr).toContain('not found in environment variable');
282
- }
283
- });
284
- });
285
- describe('Error Handling and User Guidance', () => {
286
- it('should provide helpful error messages for common mistakes', async () => {
287
- // Test missing input path
288
- const noInputResult = await runCLI(['--format', 'json']);
289
- expect(noInputResult.stderr).toContain('Input path is required');
290
- expect(noInputResult.stderr).toContain('Usage: atj [options] <inputPath>');
291
- // Test invalid format
292
- const invalidFormatResult = await runCLI(['--format', 'invalid', 'test.json']);
293
- expect(invalidFormatResult.stderr).toContain('Unsupported format: invalid');
294
- expect(invalidFormatResult.stderr).toContain('Use --list-formats to see all supported formats');
295
- // Test invalid mode
296
- const invalidModeResult = await runCLI(['--mode', 'invalid', 'test.json']);
297
- expect(invalidModeResult.stderr).toContain('Invalid mode: invalid');
298
- expect(invalidModeResult.stderr).toContain("Mode must be either 'file' or 'folder'");
299
- // Test invalid engine
300
- const invalidEngineResult = await runCLI(['--engine', 'invalid', 'test.json']);
301
- expect(invalidEngineResult.stderr).toContain('Invalid engine: invalid');
302
- expect(invalidEngineResult.stderr).toContain('Supported engines:');
303
- });
304
- it('should provide format-specific guidance in error messages', async () => {
305
- const result = await runCLI(['--format', 'unsupported', 'test.json']);
306
- expect(result.exitCode).toBe(1);
307
- expect(result.stderr).toContain('Unsupported format: unsupported');
308
- expect(result.stderr).toContain('Use --list-formats to see all supported formats');
309
- });
310
- it('should handle non-existent files with clear error messages', async () => {
311
- const result = await runCLI(['nonexistent-file.json']);
312
- expect(result.exitCode).toBe(1);
313
- expect(result.stderr).toContain('Input path does not exist: nonexistent-file.json');
314
- });
315
- it('should provide consistent error message formatting', async () => {
316
- const errorScenarios = [
317
- { args: ['--format', 'invalid', 'test.json'], expectedError: 'Unsupported format' },
318
- { args: ['--mode', 'invalid', 'test.json'], expectedError: 'Invalid mode' },
319
- { args: ['--engine', 'invalid', 'test.json'], expectedError: 'Invalid engine' },
320
- { args: ['nonexistent.json'], expectedError: 'Input path does not exist' }
321
- ];
322
- for (const scenario of errorScenarios) {
323
- const result = await runCLI(scenario.args);
324
- expect(result.exitCode).toBe(1);
325
- expect(result.stderr).toContain('❌'); // Error emoji for consistency
326
- expect(result.stderr).toContain(scenario.expectedError);
327
- // Should provide helpful guidance
328
- if (scenario.expectedError.includes('format')) {
329
- expect(result.stderr).toContain('💡'); // Tip emoji
330
- }
331
- }
332
- });
333
- });
334
- describe('Version and Information Display', () => {
335
- it('should display version information in help output', async () => {
336
- const result = await runCLI(['--help']);
337
- expect(result.exitCode).toBe(0);
338
- expect(result.stdout).toMatch(/Auto translate json\/xml cli v\d+\.\d+\.\d+/);
339
- });
340
- it('should display version information in list-formats output', async () => {
341
- const result = await runCLI(['--list-formats']);
342
- expect(result.exitCode).toBe(0);
343
- expect(result.stdout).toMatch(/Auto translate json\/xml cli v\d+\.\d+\.\d+/);
344
- });
345
- it('should display version information when starting translation', async () => {
346
- // Create a test file
347
- fs.writeFileSync(path.join(testDir, 'version-test.json'), '{"test": "value"}');
348
- const result = await runCLI(['version-test.json']);
349
- expect(result.stdout).toMatch(/Auto translate json cli v\d+\.\d+\.\d+/);
350
- });
351
- });
352
- });
@@ -1 +0,0 @@
1
- export {};