|
27 | 27 | import shutil |
28 | 28 | import subprocess |
29 | 29 | import sys |
| 30 | +from contextlib import contextmanager |
30 | 31 | from pathlib import Path |
31 | 32 |
|
32 | 33 | from rich.console import Console |
|
45 | 46 | LANGUAGES = ["en", "zh"] |
46 | 47 |
|
47 | 48 |
|
| 49 | +@contextmanager |
| 50 | +def preserve_non_json_localization_files(root: Path): |
| 51 | + """Keep files such as TypeScript exports intact while Locize CLI runs.""" |
| 52 | + |
| 53 | + preserved_files: dict[Path, bytes] = {} |
| 54 | + |
| 55 | + if root.exists(): |
| 56 | + preserved_files = { |
| 57 | + path: path.read_bytes() |
| 58 | + for path in root.rglob("*") |
| 59 | + if path.is_file() and path.suffix.lower() != ".json" |
| 60 | + } |
| 61 | + |
| 62 | + if preserved_files: |
| 63 | + console.print( |
| 64 | + f"[dim]Preserving {len(preserved_files)} non-JSON localization file(s) during Locize command.[/]" |
| 65 | + ) |
| 66 | + |
| 67 | + try: |
| 68 | + yield |
| 69 | + finally: |
| 70 | + for path, content in preserved_files.items(): |
| 71 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 72 | + path.write_bytes(content) |
| 73 | + |
| 74 | + if preserved_files: |
| 75 | + console.print( |
| 76 | + "[dim]Restored non-JSON localization file(s) after Locize command.[/]" |
| 77 | + ) |
| 78 | + |
| 79 | + |
48 | 80 | def check_environment(): |
49 | 81 | """Verify required environment variables are set.""" |
50 | 82 | api_key = os.getenv("LOCIZE_API_KEY") |
@@ -281,7 +313,8 @@ def push_local(api_key, project_id, dry_run=False): |
281 | 313 | if dry_run: |
282 | 314 | console.print("[dim]Executing with --dry=true to preview changes...[/]\n") |
283 | 315 |
|
284 | | - result = subprocess.run(cmd, cwd=REPO_ROOT) |
| 316 | + with preserve_non_json_localization_files(APP_LOCALIZATION): |
| 317 | + result = subprocess.run(cmd, cwd=REPO_ROOT) |
285 | 318 |
|
286 | 319 | if result.returncode != 0: |
287 | 320 | console.print("[bold red]✗ Error:[/] Failed to push translations to Locize") |
@@ -334,7 +367,8 @@ def download_remote(api_key, project_id, dry_run=False): |
334 | 367 | if dry_run: |
335 | 368 | console.print("[dim]Executing with --dry=true to preview changes...[/]\n") |
336 | 369 |
|
337 | | - result = subprocess.run(cmd, cwd=REPO_ROOT) |
| 370 | + with preserve_non_json_localization_files(APP_LOCALIZATION): |
| 371 | + result = subprocess.run(cmd, cwd=REPO_ROOT) |
338 | 372 |
|
339 | 373 | if result.stdout: |
340 | 374 | console.print("\n[bold cyan]📄 stdout:[/]") |
|
0 commit comments