UNPKG

@akson/cortex-shopify-translations

Version:

Unified Shopify translations management client with product extraction, translation sync, and CLI tools

169 lines (129 loc) • 4.42 kB
#!/usr/bin/env node import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import chalk from 'chalk'; import dotenv from 'dotenv'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Load environment variables dotenv.config(); // Configuration const CONFIG = { targetLanguages: ['de', 'it', 'en'], sourceLanguage: 'fr' }; // Parse command line arguments function parseArgs() { const args = process.argv.slice(2); // First argument is the file (default: translations-to-edit.json) let file = 'translations-to-edit.json'; if (args.length > 0 && !args[0].startsWith('-')) { file = args[0]; args.shift(); } const options = { file, help: args.includes('--help') || args.includes('-h'), verbose: args.includes('--verbose') }; return options; } // Show help function printHelp() { console.log(` šŸ”„ Translation Reset Tool for Shopify Usage: npx @akson/cortex-shopify-translations reset [file] [options] Arguments: file Translation file (default: translations-to-edit.json) Options: --verbose Show detailed progress -h, --help Show this help Description: Resets all fixed translation keys and status fields, allowing the translate command to re-process all strings without using --force flag. This is useful when you want to regenerate translations from scratch. Examples: npx @akson/cortex-shopify-translations reset npx @akson/cortex-shopify-translations reset my-translations.json npx @akson/cortex-shopify-translations reset --verbose `); } // Load translation file function loadTranslationFile(filePath) { if (!fs.existsSync(filePath)) { console.error(chalk.red(`āŒ File not found: ${filePath}`)); process.exit(1); } const data = JSON.parse(fs.readFileSync(filePath, 'utf8')); console.log(chalk.blue(`šŸ“„ Loaded ${path.basename(filePath)} (${data.translations.length} items)`)); return data; } // Reset fixed keys and status function resetTranslations(data, options) { const languages = CONFIG.targetLanguages; let resetsCount = 0; data.translations.forEach(item => { languages.forEach(lang => { const fixedField = `${lang}_fixed`; const statusField = `${lang}_status`; const currentField = `${lang}_current`; // Always reset fixed field (unconditionally) if (item[fixedField] !== undefined && item[fixedField] !== '') { if (options.verbose) { console.log(chalk.gray(` Resetting ${item.key} (${lang})`)); } resetsCount++; } item[fixedField] = ''; // Reset status to pending (force re-translation) item[statusField] = 'pending'; // Also reset current field to match original for proper comparison if (item[currentField] !== undefined) { item[currentField] = ''; } }); }); return resetsCount; } // Save file function saveFile(filePath, data) { data.metadata = data.metadata || {}; data.metadata.lastReset = new Date().toISOString(); fs.writeFileSync(filePath, JSON.stringify(data, null, 2)); } // Main async function main() { const options = parseArgs(); if (options.help) { printHelp(); process.exit(0); } const filePath = path.resolve(process.cwd(), options.file); console.log(chalk.blue('šŸ”„ Translation Reset Tool')); console.log(chalk.gray('Powered by Cortex\n')); try { const data = loadTranslationFile(filePath); console.log(chalk.yellow('\nšŸ”„ Resetting fixed keys and status fields...')); const resetsCount = resetTranslations(data, options); console.log(chalk.yellow(`\nšŸ“ Reset ${resetsCount} translation fields`)); saveFile(filePath, data); console.log(chalk.green('\nāœ… Reset complete!')); console.log(chalk.cyan('\nšŸ“ Next steps:')); console.log(chalk.cyan(' Run: npx @akson/cortex-shopify-translations translate ' + options.file)); console.log(chalk.cyan(' This will re-process all translations without using --force flag\n')); process.exit(0); } catch (error) { console.error(chalk.red(`\nāŒ Error: ${error.message}`)); if (options.verbose) { console.error(error.stack); } process.exit(1); } } // Run if executed directly if (import.meta.url === `file://${process.argv[1]}`) { main().catch(console.error); } export { resetTranslations };