1
1
import { 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' ;
4
4
5
5
/**
6
6
* Runs compatibility checks to ensure the current version of root supports
@@ -11,12 +11,10 @@ import type {CMSPlugin} from './plugin.js';
11
11
* latest version, a backwards-friendly "migration" is done to ensure
12
12
* compatibility on both the old and new versions.
13
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 ( ) ;
14
+ export async function runCompatibilityChecks ( rootConfig : RootConfig ) {
15
+ const cmsClient = new RootCMSClient ( rootConfig ) ;
16
+ const projectId = cmsClient . projectId ;
17
+ const db = cmsClient . db ;
20
18
const projectConfigDocRef = db . doc ( `Projects/${ projectId } ` ) ;
21
19
const projectConfigDoc = await projectConfigDocRef . get ( ) ;
22
20
const projectConfig = projectConfigDoc . data ( ) || { } ;
@@ -26,7 +24,7 @@ export async function runCompatibilityChecks(
26
24
27
25
const translationsVersion = compatibilityVersions . translations || 0 ;
28
26
if ( translationsVersion < 2 ) {
29
- await migrateTranslationsToV2 ( rootConfig , cmsPlugin ) ;
27
+ await migrateTranslationsToV2 ( rootConfig , cmsClient ) ;
30
28
compatibilityVersions . translations = 2 ;
31
29
versionsChanged = true ;
32
30
}
@@ -42,14 +40,14 @@ export async function runCompatibilityChecks(
42
40
*/
43
41
async function migrateTranslationsToV2 (
44
42
rootConfig : RootConfig ,
45
- cmsPlugin : CMSPlugin
43
+ cmsClient : RootCMSClient
46
44
) {
47
45
if ( rootConfig . experiments ?. rootCmsDisableTranslationsToV2Check ) {
48
46
return ;
49
47
}
50
48
51
- const projectId = cmsPlugin . getConfig ( ) . id || 'default' ;
52
- const db = cmsPlugin . getFirestore ( ) ;
49
+ const projectId = cmsClient . projectId ;
50
+ const db = cmsClient . db ;
53
51
const dbPath = `Projects/${ projectId } /Translations` ;
54
52
const query = db . collection ( dbPath ) ;
55
53
const querySnapshot = await query . get ( ) ;
@@ -58,42 +56,65 @@ async function migrateTranslationsToV2(
58
56
}
59
57
60
58
console . log ( '[root cms] updating translations v2 compatibility' ) ;
61
- const translationsFiles : Record < string , Record < string , any > > = { } ;
59
+
60
+ const translationsDocs : Record < string , TranslationsDoc > = { } ;
62
61
querySnapshot . forEach ( ( doc ) => {
63
62
const hash = doc . id ;
64
- const translation = doc . data ( ) ;
63
+ const translation = doc . data ( ) as Translation ;
65
64
const tags = translation . tags || [ ] ;
66
65
delete translation . tags ;
67
66
for ( const tag of tags ) {
68
67
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 ;
72
80
}
73
81
}
74
82
} ) ;
75
83
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
+
76
104
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 ] ) => {
86
106
const draftRef = db . doc (
87
107
`Projects/${ projectId } /TranslationsManager/draft/Translations/${ translationsId } `
88
108
) ;
89
109
const publishedRef = db . doc (
90
110
`Projects/${ projectId } /TranslationsManager/published/Translations/${ translationsId } `
91
111
) ;
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 ;
95
115
console . log ( `[root cms] saving ${ len } string(s) to ${ translationsId } ...` ) ;
96
116
} ) ;
97
117
await batch . commit ( ) ;
118
+
98
119
console . log ( '[root cms] done migrating translations to v2' ) ;
99
120
}
0 commit comments