Added Scripts:
- export-translations.js: Export all translations from MongoDB to JSON
- import-translations.js: Import translations into production database
Purpose:
- Avoid re-running DeepL API on production (saves quota)
- Enable dev-to-prod translation deployment workflow
- Support dry-run and force-overwrite modes
Usage:
- Export: node scripts/export-translations.js /tmp/translations-export.json
- Import: node scripts/import-translations.js /tmp/translations-export.json
Deployment Workflow:
1. Export translations from dev
2. Deploy code to production via deploy.sh
3. Copy export file to production
4. Import translations on production
🌐 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
96 lines
3.3 KiB
JavaScript
96 lines
3.3 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Export Translations Script
|
|
*
|
|
* Exports all translations from the local database to a JSON file
|
|
* for deployment to production without re-running DeepL API
|
|
*
|
|
* Usage:
|
|
* node scripts/export-translations.js [output-file]
|
|
*
|
|
* Default output: /tmp/translations-export.json
|
|
*/
|
|
|
|
require('dotenv').config();
|
|
const mongoose = require('mongoose');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const Document = require('../src/models/Document.model');
|
|
|
|
async function main() {
|
|
const outputFile = process.argv[2] || '/tmp/translations-export.json';
|
|
|
|
console.log('═══════════════════════════════════════════════════════════');
|
|
console.log(' EXPORT TRANSLATIONS');
|
|
console.log('═══════════════════════════════════════════════════════════\n');
|
|
|
|
// Connect to MongoDB
|
|
console.log('📡 Connecting to MongoDB...');
|
|
await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/tractatus_dev', {
|
|
serverSelectionTimeoutMS: 5000
|
|
});
|
|
console.log('✓ Connected\n');
|
|
|
|
// Fetch all documents with translations
|
|
console.log('📚 Fetching documents with translations...');
|
|
const documents = await Document.list({
|
|
filter: { visibility: 'public' },
|
|
limit: 1000,
|
|
sort: { order: 1 }
|
|
});
|
|
|
|
const exportData = {
|
|
exported_at: new Date().toISOString(),
|
|
source_database: process.env.MONGODB_URI || 'mongodb://localhost:27017/tractatus_dev',
|
|
total_documents: documents.length,
|
|
documents: []
|
|
};
|
|
|
|
let totalTranslations = 0;
|
|
|
|
for (const doc of documents) {
|
|
if (doc.translations && Object.keys(doc.translations).length > 0) {
|
|
const docExport = {
|
|
slug: doc.slug,
|
|
_id: doc._id.toString(),
|
|
translations: doc.translations
|
|
};
|
|
|
|
exportData.documents.push(docExport);
|
|
|
|
const langCount = Object.keys(doc.translations).length;
|
|
totalTranslations += langCount;
|
|
console.log(` ✓ ${doc.slug}: ${langCount} translation(s)`);
|
|
}
|
|
}
|
|
|
|
exportData.total_translations = totalTranslations;
|
|
|
|
// Write to file
|
|
console.log(`\n💾 Writing to ${outputFile}...`);
|
|
fs.writeFileSync(outputFile, JSON.stringify(exportData, null, 2), 'utf8');
|
|
console.log('✓ Export complete\n');
|
|
|
|
// Summary
|
|
console.log('═══════════════════════════════════════════════════════════');
|
|
console.log(' EXPORT SUMMARY');
|
|
console.log('═══════════════════════════════════════════════════════════\n');
|
|
console.log(` Documents with translations: ${exportData.documents.length}`);
|
|
console.log(` Total translations: ${totalTranslations}`);
|
|
console.log(` Output file: ${outputFile}`);
|
|
console.log(` File size: ${(fs.statSync(outputFile).size / 1024).toFixed(2)} KB\n`);
|
|
|
|
await mongoose.disconnect();
|
|
console.log('✓ Database disconnected\n');
|
|
|
|
process.exit(0);
|
|
}
|
|
|
|
// Run
|
|
main().catch(err => {
|
|
console.error('\n❌ Fatal error:', err.message);
|
|
console.error(err.stack);
|
|
process.exit(1);
|
|
});
|