import chalk from 'chalk'; import path from 'node:path'; import { logger } from '../../console/logger.js'; import { BatchedFiles, downloadFileBatch, DownloadFileBatchResult, } from '../../api/downloadFileBatch.js'; import type { ApiClient } from '../../utils/api.js'; import { Settings } from '../../types/index.js'; import { recordWarning } from '../../state/translateWarnings.js'; import { FileStatusTracker } from './PollJobsStep.js'; import { TEMPLATE_FILE_NAME } from '../../utils/constants.js'; import type { InlineLibrary } from '../../types/libraries.js'; export type DownloadTranslationsInput = { fileTracker: FileStatusTracker; resolveOutputPath: (sourcePath: string, locale: string) => string | null; forceDownload?: boolean; }; export class DownloadTranslationsStep { private spinner: ReturnType | null = null; constructor( private api: ApiClient, private settings: Settings, private inlineLibrary?: InlineLibrary ) {} async run({ fileTracker, resolveOutputPath, forceDownload, }: DownloadTranslationsInput): Promise { this.spinner = logger.createProgressBar(fileTracker.completed.size); this.spinner.start('Downloading files...'); return this.downloadFiles(fileTracker, resolveOutputPath, forceDownload); } private async downloadFiles( fileTracker: FileStatusTracker, resolveOutputPath: (sourcePath: string, locale: string) => string | null, forceDownload?: boolean ): Promise { try { // Only download files that are marked as completed const currentQueryData = Array.from(fileTracker.completed.values()); // If no files to download, we're done if (currentQueryData.length === 0) { this.spinner?.stop(chalk.green('No files to download')); return true; } // Check for translations const responseData = await this.api.queryFileData({ translatedFiles: currentQueryData.map((item) => ({ fileId: item.fileId, versionId: item.versionId, branchId: item.branchId, locale: item.locale, })), }); const translatedFiles = responseData.translatedFiles || []; // Filter for ready translations const readyTranslations = translatedFiles.filter( (file) => file.completedAt !== null ); let missingCount = 0; if (readyTranslations.length < currentQueryData.length) { const readyKeys = new Set( readyTranslations.map( (t) => `${t.branchId}:${t.fileId}:${t.versionId}:${t.locale}` ) ); const missing = currentQueryData.filter( (item) => !readyKeys.has( `${item.branchId}:${item.fileId}:${item.versionId}:${item.locale}` ) ); missingCount = missing.length; logger.warn( `Failed to download ${missing.length} file(s):\n${missing.map((f) => `- ${f.fileName} (${f.locale})`).join('\n')}` ); for (const f of missing) { recordWarning( 'failed_download', f.fileName, `Failed to download for locale ${f.locale}` ); } } // Review gating: skip completed translations for normal files whose // effective requiresReview policy is true but that are not approved // yet. Skips are intentional, not failures — a review-gated file is // simply absent locally and falls back to source behavior. // GTJSON is exempt: it is always requested, and the platform filters // unapproved components out of the served content itself. const requiresReviewPaths = this.settings.files?.requiresReviewPaths ?? new Set(); const reviewGatedKeys = new Set(); if (requiresReviewPaths.size > 0) { for (const translation of readyTranslations) { if (translation.approvedAt !== null) continue; const fileKey = `${translation.branchId}:${translation.fileId}:${translation.versionId}:${translation.locale}`; const fileProperties = fileTracker.completed.get(fileKey); if (!fileProperties) continue; if (fileProperties.fileName === TEMPLATE_FILE_NAME) continue; const absolutePath = path.resolve( process.cwd(), fileProperties.fileName ); if (!requiresReviewPaths.has(absolutePath)) continue; reviewGatedKeys.add(fileKey); fileTracker.completed.delete(fileKey); fileTracker.skipped.set(fileKey, fileProperties); recordWarning( 'pending_review', fileProperties.fileName, `Translation for locale ${translation.locale} requires review and is not approved yet` ); } if (reviewGatedKeys.size > 0) { logger.info( `Skipped ${reviewGatedKeys.size} file(s) awaiting review approval. They will download once approved.` ); } } // Prepare batch download data const batchFiles: BatchedFiles = readyTranslations .map((translation) => { const fileKey = `${translation.branchId}:${translation.fileId}:${translation.versionId}:${translation.locale}`; const fileProperties = fileTracker.completed.get(fileKey); if (!fileProperties) { return null; } const outputPath = resolveOutputPath( fileProperties.fileName, translation.locale ); // Skip downloading GTJSON files that are not in the files configuration if (outputPath === null) { fileTracker.completed.delete(fileKey); fileTracker.skipped.set(fileKey, fileProperties); return null; } return { branchId: translation.branchId, fileId: translation.fileId, versionId: translation.versionId, locale: translation.locale, inputPath: fileProperties.fileName, outputPath, }; }) .filter((file) => file !== null) as BatchedFiles; if (batchFiles.length > 0) { const batchResult = await this.downloadFilesWithRetry( fileTracker, batchFiles, forceDownload ); this.spinner?.stop( chalk.green( `Downloaded ${batchResult.successful.length} files${batchResult.skipped.length > 0 ? `, skipped ${batchResult.skipped.length} files` : ''}` ) ); if (batchResult.failed.length > 0) { logger.warn( `Failed to download ${batchResult.failed.length} files: ${batchResult.failed.map((f) => f.inputPath).join('\n')}` ); for (const f of batchResult.failed) { recordWarning( 'failed_download', f.inputPath, `Failed to download for locale ${f.locale}` ); } } } else if (missingCount > 0) { this.spinner?.stop( chalk.yellow('No files downloaded - see warnings above') ); } else { this.spinner?.stop(chalk.green('No files to download')); } return true; } catch (error) { this.spinner?.stop( chalk.red('An error occurred while downloading translations') ); logger.error(chalk.red('Error: ') + error); return false; } } private async downloadFilesWithRetry( fileTracker: FileStatusTracker, files: BatchedFiles, forceDownload?: boolean, maxRetries: number = 3, initialDelay: number = 1000 ): Promise { let remainingFiles = files; let allSuccessful: BatchedFiles = []; let retryCount = 0; let allSkipped: BatchedFiles = []; while (remainingFiles.length > 0 && retryCount <= maxRetries) { const batchResult = await downloadFileBatch( fileTracker, remainingFiles, this.settings, forceDownload, this.inlineLibrary ); allSuccessful = [...allSuccessful, ...batchResult.successful]; allSkipped = [...allSkipped, ...batchResult.skipped]; this.spinner?.advance( batchResult.successful.length + batchResult.skipped.length + batchResult.failed.length ); // If no failures or we've exhausted retries, we're done if (batchResult.failed.length === 0 || retryCount === maxRetries) { return { successful: allSuccessful, failed: batchResult.failed, skipped: allSkipped, }; } // Calculate exponential backoff delay const delay = initialDelay * Math.pow(2, retryCount); logger.error( chalk.yellow( `Retrying ${batchResult.failed.length} failed file(s) in ${delay}ms (attempt ${retryCount + 1}/${maxRetries})...` ) ); // Wait before retrying await new Promise((resolve) => setTimeout(resolve, delay)); remainingFiles = batchResult.failed; retryCount++; } return { successful: allSuccessful, failed: remainingFiles, skipped: allSkipped, }; } }