|
| 1 | +import {RootConfig} from '@blinkk/root'; |
| 2 | +import {FieldValue} from 'firebase-admin/firestore'; |
| 3 | +import type {CMSPlugin} from './plugin.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Runs compatibility checks to ensure the current version of root supports |
| 7 | + * any backwards-incompatible db changes. |
| 8 | + * |
| 9 | + * Compatibility versions are stored in the db in Projects/<projectId> under |
| 10 | + * the "compatibility" key. If the compatibility version is less than the |
| 11 | + * latest version, a backwards-friendly "migration" is done to ensure |
| 12 | + * compatibility on both the old and new versions. |
| 13 | + */ |
| 14 | +export async function runCompatibilityChecks( |
| 15 | + rootConfig: RootConfig, |
| 16 | + cmsPlugin: CMSPlugin |
| 17 | +) { |
| 18 | + const projectId = cmsPlugin.getConfig().id || 'default'; |
| 19 | + const db = cmsPlugin.getFirestore(); |
| 20 | + const projectConfigDocRef = db.doc(`Projects/${projectId}`); |
| 21 | + const projectConfigDoc = await projectConfigDocRef.get(); |
| 22 | + const projectConfig = projectConfigDoc.data() || {}; |
| 23 | + |
| 24 | + const compatibilityVersions = projectConfig.compatibility || {}; |
| 25 | + let versionsChanged = false; |
| 26 | + |
| 27 | + const translationsVersion = compatibilityVersions.translations || 0; |
| 28 | + if (translationsVersion < 2) { |
| 29 | + await migrateTranslationsToV2(rootConfig, cmsPlugin); |
| 30 | + compatibilityVersions.translations = 2; |
| 31 | + versionsChanged = true; |
| 32 | + } |
| 33 | + |
| 34 | + if (versionsChanged) { |
| 35 | + await projectConfigDocRef.update({compatibility: compatibilityVersions}); |
| 36 | + console.log('[root cms] updated db compatibility'); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Migrates translations from the "v1" format to "v2". |
| 42 | + */ |
| 43 | +async function migrateTranslationsToV2( |
| 44 | + rootConfig: RootConfig, |
| 45 | + cmsPlugin: CMSPlugin |
| 46 | +) { |
| 47 | + if (rootConfig.experiments?.rootCmsDisableTranslationsToV2Check) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const projectId = cmsPlugin.getConfig().id || 'default'; |
| 52 | + const db = cmsPlugin.getFirestore(); |
| 53 | + const dbPath = `Projects/${projectId}/Translations`; |
| 54 | + const query = db.collection(dbPath); |
| 55 | + const querySnapshot = await query.get(); |
| 56 | + if (querySnapshot.size === 0) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + console.log('[root cms] updating translations v2 compatibility'); |
| 61 | + const translationsMemories: Record<string, Record<string, any>> = {}; |
| 62 | + querySnapshot.forEach((doc) => { |
| 63 | + const hash = doc.id; |
| 64 | + const translation = doc.data(); |
| 65 | + const tags = translation.tags || []; |
| 66 | + delete translation.tags; |
| 67 | + for (const tag of tags) { |
| 68 | + if (tag.includes('/')) { |
| 69 | + const translationsMemoryId = tag.replaceAll('/', '--'); |
| 70 | + translationsMemories[translationsMemoryId] ??= {}; |
| 71 | + translationsMemories[translationsMemoryId][hash] = translation; |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + const batch = db.batch(); |
| 77 | + Object.entries(translationsMemories).forEach( |
| 78 | + ([translationsMemoryId, strings]) => { |
| 79 | + const updates = { |
| 80 | + sys: { |
| 81 | + modifiedAt: FieldValue.serverTimestamp(), |
| 82 | + modifiedBy: 'root-cms-client', |
| 83 | + }, |
| 84 | + strings: strings, |
| 85 | + }; |
| 86 | + const draftRef = db.doc( |
| 87 | + `Projects/${projectId}/TranslationsMemory/draft/Translations/${translationsMemoryId}` |
| 88 | + ); |
| 89 | + const publishedRef = db.doc( |
| 90 | + `Projects/${projectId}/TranslationsMemory/published/Translations/${translationsMemoryId}` |
| 91 | + ); |
| 92 | + batch.set(draftRef, updates, {merge: true}); |
| 93 | + batch.set(publishedRef, updates, {merge: true}); |
| 94 | + const len = Object.keys(strings).length; |
| 95 | + console.log( |
| 96 | + `[root cms] saving ${len} string(s) to ${translationsMemoryId}...` |
| 97 | + ); |
| 98 | + } |
| 99 | + ); |
| 100 | + await batch.commit(); |
| 101 | + console.log('[root cms] done migrating translations to v2'); |
| 102 | +} |
0 commit comments