11import { RootConfig } from '@blinkk/root' ;
2- import { FieldValue } from 'firebase-admin/firestore' ;
3- import type { CMSPlugin } from './plugin .js' ;
2+ import { Timestamp } from 'firebase-admin/firestore' ;
3+ import { RootCMSClient , Translation , TranslationsDoc } from './client .js' ;
44
55/**
66 * Runs compatibility checks to ensure the current version of root supports
@@ -11,12 +11,10 @@ import type {CMSPlugin} from './plugin.js';
1111 * latest version, a backwards-friendly "migration" is done to ensure
1212 * compatibility on both the old and new versions.
1313 */
14- export async function runCompatibilityChecks (
15- rootConfig : RootConfig ,
16- cmsPlugin : CMSPlugin
17- ) {
18- const projectId = cmsPlugin . getConfig ( ) . id || 'default' ;
19- const db = cmsPlugin . getFirestore ( ) ;
14+ export async function runCompatibilityChecks ( rootConfig : RootConfig ) {
15+ const cmsClient = new RootCMSClient ( rootConfig ) ;
16+ const projectId = cmsClient . projectId ;
17+ const db = cmsClient . db ;
2018 const projectConfigDocRef = db . doc ( `Projects/${ projectId } ` ) ;
2119 const projectConfigDoc = await projectConfigDocRef . get ( ) ;
2220 const projectConfig = projectConfigDoc . data ( ) || { } ;
@@ -26,7 +24,7 @@ export async function runCompatibilityChecks(
2624
2725 const translationsVersion = compatibilityVersions . translations || 0 ;
2826 if ( translationsVersion < 2 ) {
29- await migrateTranslationsToV2 ( rootConfig , cmsPlugin ) ;
27+ await migrateTranslationsToV2 ( rootConfig , cmsClient ) ;
3028 compatibilityVersions . translations = 2 ;
3129 versionsChanged = true ;
3230 }
@@ -42,14 +40,14 @@ export async function runCompatibilityChecks(
4240 */
4341async function migrateTranslationsToV2 (
4442 rootConfig : RootConfig ,
45- cmsPlugin : CMSPlugin
43+ cmsClient : RootCMSClient
4644) {
4745 if ( rootConfig . experiments ?. rootCmsDisableTranslationsToV2Check ) {
4846 return ;
4947 }
5048
51- const projectId = cmsPlugin . getConfig ( ) . id || 'default' ;
52- const db = cmsPlugin . getFirestore ( ) ;
49+ const projectId = cmsClient . projectId ;
50+ const db = cmsClient . db ;
5351 const dbPath = `Projects/${ projectId } /Translations` ;
5452 const query = db . collection ( dbPath ) ;
5553 const querySnapshot = await query . get ( ) ;
@@ -58,42 +56,65 @@ async function migrateTranslationsToV2(
5856 }
5957
6058 console . log ( '[root cms] updating translations v2 compatibility' ) ;
61- const translationsFiles : Record < string , Record < string , any > > = { } ;
59+
60+ const translationsDocs : Record < string , TranslationsDoc > = { } ;
6261 querySnapshot . forEach ( ( doc ) => {
6362 const hash = doc . id ;
64- const translation = doc . data ( ) ;
63+ const translation = doc . data ( ) as Translation ;
6564 const tags = translation . tags || [ ] ;
6665 delete translation . tags ;
6766 for ( const tag of tags ) {
6867 if ( tag . includes ( '/' ) ) {
69- const translationsId = tag . replaceAll ( '/' , '--' ) ;
70- translationsFiles [ translationsId ] ??= { } ;
71- translationsFiles [ translationsId ] [ hash ] = translation ;
68+ const translationsId = tag ;
69+ translationsDocs [ translationsId ] ??= {
70+ id : translationsId ,
71+ sys : {
72+ modifiedAt : Timestamp . now ( ) ,
73+ modifiedBy : 'root-cms-client' ,
74+ publishedAt : Timestamp . now ( ) ,
75+ publishedBy : 'root-cms-client' ,
76+ } ,
77+ strings : { } ,
78+ } ;
79+ translationsDocs [ translationsId ] . strings [ hash ] = translation ;
7280 }
7381 }
7482 } ) ;
7583
84+ if ( Object . keys ( translationsDocs ) . length === 0 ) {
85+ console . log ( '[root cms] no translations to save' ) ;
86+ return ;
87+ }
88+
89+ // Move the doc's "l10nSheet" to the translations doc's "linkedSheet".
90+
91+ for ( const docId in translationsDocs ) {
92+ const [ collection , slug ] = docId . split ( '/' ) ;
93+ if ( collection && slug ) {
94+ const doc : any = await cmsClient . getDoc ( collection , slug , {
95+ mode : 'draft' ,
96+ } ) ;
97+ const linkedSheet = doc ?. sys ?. l10nSheet ;
98+ if ( linkedSheet ) {
99+ translationsDocs [ docId ] . sys . linkedSheet = linkedSheet ;
100+ }
101+ }
102+ }
103+
76104 const batch = db . batch ( ) ;
77- Object . entries ( translationsFiles ) . forEach ( ( [ translationsId , strings ] ) => {
78- const updates = {
79- id : translationsId . replaceAll ( '--' , '/' ) ,
80- sys : {
81- modifiedAt : FieldValue . serverTimestamp ( ) ,
82- modifiedBy : 'root-cms-client' ,
83- } ,
84- strings : strings ,
85- } ;
105+ Object . entries ( translationsDocs ) . forEach ( ( [ translationsId , data ] ) => {
86106 const draftRef = db . doc (
87107 `Projects/${ projectId } /TranslationsManager/draft/Translations/${ translationsId } `
88108 ) ;
89109 const publishedRef = db . doc (
90110 `Projects/${ projectId } /TranslationsManager/published/Translations/${ translationsId } `
91111 ) ;
92- batch . set ( draftRef , updates , { merge : true } ) ;
93- batch . set ( publishedRef , updates , { merge : true } ) ;
94- const len = Object . keys ( strings ) . length ;
112+ batch . set ( draftRef , data , { merge : true } ) ;
113+ batch . set ( publishedRef , data , { merge : true } ) ;
114+ const len = Object . keys ( data . strings ) . length ;
95115 console . log ( `[root cms] saving ${ len } string(s) to ${ translationsId } ...` ) ;
96116 } ) ;
97117 await batch . commit ( ) ;
118+
98119 console . log ( '[root cms] done migrating translations to v2' ) ;
99120}
0 commit comments