-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathentries.ts
More file actions
1028 lines (969 loc) · 39.1 KB
/
entries.ts
File metadata and controls
1028 lines (969 loc) · 39.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable no-prototype-builtins */
/*!
* Contentstack Import
* Copyright (c) 2024 Contentstack LLC
* MIT Licensed
*/
import * as path from 'path';
import { writeFileSync } from 'fs';
import { isEmpty, values, cloneDeep, find, indexOf, forEach, remove } from 'lodash';
import { FsUtility, sanitizePath } from '@contentstack/cli-utilities';
import {
fsUtil,
log,
formatError,
lookupExtension,
suppressSchemaReference,
removeUidsFromJsonRteFields,
removeEntryRefsFromJSONRTE,
restoreJsonRteEntryRefs,
lookupEntries,
lookupAssets,
fileHelper,
lookUpTerms,
} from '../../utils';
import { ModuleClassParams } from '../../types';
import BaseClass, { ApiOptions } from './base-class';
export default class EntriesImport extends BaseClass {
private assetUidMapperPath: string;
private assetUidMapper: Record<string, any>;
private assetUrlMapper: Record<string, any>;
private assetUrlMapperPath: string;
private entriesMapperPath: string;
private envPath: string;
private entriesUIDMapperPath: string;
private uniqueUidMapperPath: string;
private modifiedCTsPath: string;
private marketplaceAppMapperPath: string;
private entriesConfig: Record<string, any>;
private cTsPath: string;
private localesPath: string;
private importConcurrency: number;
private entriesPath: string;
private cTs: Record<string, any>[];
private modifiedCTs: Record<string, any>[];
private refCTs: string[];
private jsonRteCTs: Record<string, any>;
private jsonRteCTsWithRef: Record<string, any>;
private jsonRteEntries: Record<string, any>;
private installedExtensions: Record<string, any>[];
private createdEntries: Record<string, any>[];
private failedEntries: Record<string, any>[];
private locales: Record<string, any>[];
private entriesUidMapper: Record<string, any>;
private envs: Record<string, any>;
private autoCreatedEntries: Record<string, any>[];
private taxonomiesPath: string;
public taxonomies: Record<string, unknown>;
public rteCTs: any;
public rteCTsWithRef: any;
public entriesForVariant: { content_type: string; locale: string; entry_uid: string }[] = [];
constructor({ importConfig, stackAPIClient }: ModuleClassParams) {
super({ importConfig, stackAPIClient });
this.assetUidMapperPath = path.resolve(sanitizePath(importConfig.data), 'mapper', 'assets', 'uid-mapping.json');
this.assetUrlMapperPath = path.resolve(sanitizePath(importConfig.data), 'mapper', 'assets', 'url-mapping.json');
this.entriesMapperPath = path.resolve(sanitizePath(importConfig.data), 'mapper', 'entries');
this.envPath = path.resolve(sanitizePath(importConfig.data), 'environments', 'environments.json');
this.entriesUIDMapperPath = path.join(sanitizePath(this.entriesMapperPath), 'uid-mapping.json');
this.uniqueUidMapperPath = path.join(sanitizePath(this.entriesMapperPath), 'unique-mapping.json');
this.modifiedCTsPath = path.join(sanitizePath(this.entriesMapperPath), 'modified-schemas.json');
this.marketplaceAppMapperPath = path.join(
sanitizePath(this.importConfig.data),
'mapper',
'marketplace_apps',
'uid-mapping.json',
);
this.taxonomiesPath = path.join(
sanitizePath(this.importConfig.data),
'mapper',
'taxonomies',
'terms',
'success.json',
);
this.entriesConfig = importConfig.modules.entries;
this.entriesPath = path.resolve(sanitizePath(importConfig.data), sanitizePath(this.entriesConfig.dirName));
this.cTsPath = path.resolve(
sanitizePath(importConfig.data),
sanitizePath(importConfig.modules['content-types'].dirName),
);
this.localesPath = path.resolve(
sanitizePath(importConfig.data),
sanitizePath(importConfig.modules.locales.dirName),
sanitizePath(importConfig.modules.locales.fileName),
);
this.importConcurrency = this.entriesConfig.importConcurrency || importConfig.importConcurrency;
this.entriesUidMapper = {};
this.modifiedCTs = [];
this.refCTs = [];
this.jsonRteCTs = [];
this.jsonRteCTsWithRef = [];
this.envs = {};
this.autoCreatedEntries = [];
this.failedEntries = [];
this.rteCTs = [];
this.rteCTsWithRef = [];
}
async start(): Promise<any> {
try {
this.cTs = fsUtil.readFile(path.join(this.cTsPath, 'schema.json')) as Record<string, unknown>[];
if (!this.cTs || isEmpty(this.cTs)) {
log(this.importConfig, 'No content type found', 'info');
return;
}
this.installedExtensions = (
((await fsUtil.readFile(this.marketplaceAppMapperPath)) as any) || { extension_uid: {} }
).extension_uid;
this.assetUidMapper = (fsUtil.readFile(this.assetUidMapperPath) as Record<string, any>) || {};
this.assetUrlMapper = (fsUtil.readFile(this.assetUrlMapperPath) as Record<string, any>) || {};
this.taxonomies = fsUtil.readFile(this.taxonomiesPath) as Record<string, any>;
fsUtil.makeDirectory(this.entriesMapperPath);
await this.disableMandatoryCTReferences();
this.locales = values(fsUtil.readFile(this.localesPath) as Record<string, unknown>[]);
this.locales.unshift(this.importConfig.master_locale); // adds master locale to the list
//Create Entries
const entryRequestOptions = this.populateEntryCreatePayload();
for (let entryRequestOption of entryRequestOptions) {
await this.createEntries(entryRequestOption);
}
if (this.importConfig.replaceExisting) {
// Note: Instead of using entryRequestOptions, we can prepare request options for replace, to avoid unnecessary operations
for (let entryRequestOption of entryRequestOptions) {
await this.replaceEntries(entryRequestOption).catch((error) => {
log(this.importConfig, `Error while replacing the existing entries ${formatError(error)}`, 'error');
});
}
}
await fileHelper.writeLargeFile(path.join(this.entriesMapperPath, 'uid-mapping.json'), this.entriesUidMapper); // TBD: manages mapper in one file, should find an alternative
fsUtil.writeFile(path.join(this.entriesMapperPath, 'failed-entries.json'), this.failedEntries);
if (this.autoCreatedEntries?.length > 0) {
log(this.importConfig, 'Removing entries from master language which got created by default', 'info');
await this.removeAutoCreatedEntries().catch((error) => {
log(
this.importConfig,
`Error while removing auto created entries in master locale ${formatError(error)}`,
'error',
);
});
}
// Update entries with references
const entryUpdateRequestOptions = this.populateEntryUpdatePayload();
for (let entryUpdateRequestOption of entryUpdateRequestOptions) {
await this.updateEntriesWithReferences(entryUpdateRequestOption).catch((error) => {
log(
this.importConfig,
`Error while updating entries references of ${entryUpdateRequestOption.cTUid} in locale ${entryUpdateRequestOption.locale}`,
'error',
);
log(this.importConfig, formatError(error), 'error');
});
}
fsUtil.writeFile(path.join(this.entriesMapperPath, 'failed-entries.json'), this.failedEntries);
log(this.importConfig, 'Restoring content type changes', 'info');
await this.enableMandatoryCTReferences().catch((error) => {
log(this.importConfig, `Error while updating content type references ${formatError(error)}`, 'error');
});
// Update field rule of content types which are got removed earlier
log(this.importConfig, 'Updating the field rules of content type', 'info');
await this.updateFieldRules().catch((error) => {
log(this.importConfig, `Error while updating field rules of content type ${formatError(error)}`, 'error');
});
log(this.importConfig, 'Entries imported successfully', 'success');
// Publishing entries
if (this.importConfig.entriesPublish) {
log(this.importConfig, 'Publishing entries', 'info');
this.envs = fileHelper.readFileSync(this.envPath);
for (let entryRequestOption of entryRequestOptions) {
await this.publishEntries(entryRequestOption).catch((error) => {
log(
this.importConfig,
`Error in publishing entries of ${entryRequestOption.cTUid} in locale ${
entryRequestOption.locale
} ${formatError(error)}`,
'error',
);
});
}
log(this.importConfig, 'All the entries have been published successfully', 'success');
}
this.createEntryDataForVariantEntry();
} catch (error) {
this.createEntryDataForVariantEntry();
log(this.importConfig, formatError(error), 'error');
throw new Error('Error while importing entries');
}
}
/**
* The function `createEntryDataForVariantEntry` writes the `entriesForVariant` data to a JSON file
* named `data-for-variant-entry.json`.
*/
createEntryDataForVariantEntry() {
const filePath = path.join(this.entriesMapperPath, 'data-for-variant-entry.json');
if (!isEmpty(this.entriesForVariant)) {
writeFileSync(filePath, JSON.stringify(this.entriesForVariant), { encoding: 'utf8' });
}
}
async disableMandatoryCTReferences() {
const onSuccess = ({ response: contentType, apiData: { uid } }: any) => {
log(this.importConfig, `${uid} content type references removed temporarily`, 'success');
};
const onReject = ({ error, apiData: { uid } }: any) => {
log(this.importConfig, formatError(error), 'error');
throw new Error(`${uid} content type references removal failed`);
};
return await this.makeConcurrentCall({
processName: 'Update content types (removing mandatory references temporarily)',
apiContent: this.cTs,
apiParams: {
serializeData: this.serializeUpdateCTs.bind(this),
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'update-cts',
includeParamOnCompletion: true,
},
concurrencyLimit: this.importConcurrency,
}).then(() => {
fsUtil.writeFile(this.modifiedCTsPath, this.modifiedCTs);
});
}
/**
* @method serializeUpdateCTs
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializeUpdateCTs(apiOptions: ApiOptions): ApiOptions {
const { apiData } = apiOptions;
const contentType = cloneDeep(apiData);
if (contentType.field_rules) {
delete contentType.field_rules;
}
const flag = {
suppressed: false,
references: false,
jsonRte: false,
jsonRteEmbeddedEntries: false,
rte: false,
rteEmbeddedEntries: false,
};
suppressSchemaReference(contentType.schema, flag);
if (flag.references) {
this.refCTs.push(contentType.uid);
}
if (flag.jsonRte) {
this.jsonRteCTs.push(contentType.uid);
if (flag.jsonRteEmbeddedEntries) {
this.jsonRteCTsWithRef.push(contentType.uid);
if (this.refCTs.indexOf(contentType.uid) === -1) {
this.refCTs.push(contentType.uid);
}
}
}
if (flag.rte) {
this.rteCTs.push(contentType.uid);
if (flag.rteEmbeddedEntries) {
this.rteCTsWithRef.push(contentType.uid);
if (this.refCTs.indexOf(contentType.uid) === -1) {
this.refCTs.push(contentType.uid);
}
}
}
// Check if suppress modified flag
if (flag.suppressed) {
this.modifiedCTs.push(find(this.cTs, { uid: contentType.uid }));
} else {
// Note: Skips the content type from update if no reference found
apiOptions.apiData = null;
return apiOptions;
}
lookupExtension(
this.importConfig,
contentType.schema,
this.importConfig.preserveStackVersion,
this.installedExtensions,
);
const contentTypePayload = this.stack.contentType(contentType.uid);
Object.assign(contentTypePayload, cloneDeep(contentType));
apiOptions.apiData = contentTypePayload;
return apiOptions;
}
populateEntryCreatePayload(): { cTUid: string; locale: string }[] {
const requestOptions: { cTUid: string; locale: string }[] = [];
for (let locale of this.locales) {
for (let contentType of this.cTs) {
requestOptions.push({
cTUid: contentType.uid,
locale: locale.code,
});
}
}
return requestOptions;
}
async createEntries({ cTUid, locale }: { cTUid: string; locale: string }): Promise<void> {
const processName = 'Create Entries';
const indexFileName = 'index.json';
const basePath = path.join(this.entriesPath, cTUid, locale);
const fs = new FsUtility({ basePath, indexFileName });
const indexer = fs.indexFileContent;
const indexerCount = values(indexer).length;
if (indexerCount === 0) {
return Promise.resolve();
}
// log(this.importConfig, `Starting to create entries for ${cTUid} in locale ${locale}`, 'info');
const isMasterLocale = locale === this.importConfig?.master_locale?.code;
// Write created entries
const entriesCreateFileHelper = new FsUtility({
moduleName: 'entries',
indexFileName: 'index.json',
basePath: path.join(this.entriesMapperPath, cTUid, locale),
chunkFileSize: this.entriesConfig.chunkFileSize,
keepMetadata: false,
omitKeys: this.entriesConfig.invalidKeys,
});
// create file instance for existing entries
const existingEntriesFileHelper = new FsUtility({
moduleName: 'entries',
indexFileName: 'index.json',
basePath: path.join(this.entriesMapperPath, cTUid, locale, 'existing'),
chunkFileSize: this.entriesConfig.chunkFileSize,
keepMetadata: false,
omitKeys: this.entriesConfig.invalidKeys,
});
const contentType = find(this.cTs, { uid: cTUid });
const onSuccess = ({ response, apiData: entry, additionalInfo }: any) => {
if (additionalInfo[entry.uid]?.isLocalized) {
let oldUid = additionalInfo[entry.uid].entryOldUid;
this.entriesForVariant.push({ content_type: cTUid, entry_uid: oldUid, locale });
log(
this.importConfig,
`Localized entry: '${entry.title}' of content type ${cTUid} in locale ${locale}`,
'info',
);
entry.uid = oldUid;
entry.entryOldUid = oldUid;
entry.sourceEntryFilePath = path.join(sanitizePath(basePath), sanitizePath(additionalInfo.entryFileName)); // stores source file path temporarily
entriesCreateFileHelper.writeIntoFile({ [oldUid]: entry } as any, { mapKeyVal: true });
} else {
log(this.importConfig, `Created entry: '${entry.title}' of content type ${cTUid} in locale ${locale}`, 'info');
this.entriesForVariant.push({ content_type: cTUid, entry_uid: entry.uid, locale });
// This is for creating localized entries that do not have a counterpart in master locale.
// For example : To create entry1 in fr-fr, where en-us is the master locale
// entry1 will get created in en-us first, then fr-fr version will be created
// thus entry1 has to be removed from en-us at the end.
if (!isMasterLocale && !additionalInfo[entry.uid]?.isLocalized) {
this.autoCreatedEntries.push({ cTUid, locale, entryUid: response.uid });
}
this.entriesUidMapper[entry.uid] = response.uid;
entry.sourceEntryFilePath = path.join(sanitizePath(basePath), sanitizePath(additionalInfo.entryFileName)); // stores source file path temporarily
entry.entryOldUid = entry.uid; // stores old uid temporarily
entriesCreateFileHelper.writeIntoFile({ [entry.uid]: entry } as any, { mapKeyVal: true });
}
};
const onReject = ({ error, apiData: entry, additionalInfo }: any) => {
const { title, uid } = entry;
// NOTE Remove from list if any entry import failed
remove(this.entriesForVariant, { locale, entry_uid: uid });
// NOTE: write existing entries into files to handler later
if (error.errorCode === 119) {
if (error?.errors?.title || error?.errors?.uid) {
if (this.importConfig.replaceExisting) {
entry.entryOldUid = uid;
entry.sourceEntryFilePath = path.join(sanitizePath(basePath), sanitizePath(additionalInfo.entryFileName)); // stores source file path temporarily
existingEntriesFileHelper.writeIntoFile({ [uid]: entry } as any, { mapKeyVal: true });
}
if (!this.importConfig.skipExisting) {
log(this.importConfig, `Entry '${title}' already exists`, 'info');
}
} else {
log(
this.importConfig,
`${title} entry of content type ${cTUid} in locale ${locale} failed to create`,
'error',
);
log(this.importConfig, formatError(error), 'error');
this.failedEntries.push({ content_type: cTUid, locale, entry: { uid, title } });
}
} else {
log(this.importConfig, `${title} entry of content type ${cTUid} in locale ${locale} failed to create`, 'error');
log(this.importConfig, formatError(error), 'error');
this.failedEntries.push({ content_type: cTUid, locale, entry: { uid, title } });
}
};
for (const index in indexer) {
const chunk = await fs.readChunkFiles.next().catch((error) => {
log(this.importConfig, formatError(error), 'error');
});
if (chunk) {
let apiContent = values(chunk as Record<string, any>[]);
await this.makeConcurrentCall({
apiContent,
processName,
indexerCount,
currentIndexer: +index,
apiParams: {
reject: onReject,
resolve: onSuccess,
entity: 'create-entries',
includeParamOnCompletion: true,
serializeData: this.serializeEntries.bind(this),
additionalInfo: { contentType, locale, cTUid, entryFileName: indexer[index], isMasterLocale },
},
concurrencyLimit: this.importConcurrency,
}).then(() => {
entriesCreateFileHelper?.completeFile(true);
existingEntriesFileHelper?.completeFile(true);
log(this.importConfig, `Created entries for content type ${cTUid} in locale ${locale}`, 'success');
});
}
}
}
/**
* @method serializeEntries
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializeEntries(apiOptions: ApiOptions): ApiOptions {
let {
apiData: entry,
additionalInfo: { cTUid, locale, contentType, isMasterLocale },
} = apiOptions;
try {
if (this.jsonRteCTs.indexOf(cTUid) > -1) {
entry = removeUidsFromJsonRteFields(entry, contentType.schema);
}
// remove entry references from json-rte fields
if (this.jsonRteCTsWithRef.indexOf(cTUid) > -1) {
entry = removeEntryRefsFromJSONRTE(entry, contentType.schema);
}
if (this.rteCTsWithRef.indexOf(cTUid) > -1) {
entry = removeEntryRefsFromJSONRTE(entry, contentType.schema);
}
//will remove term if term doesn't exists in taxonomy
lookUpTerms(contentType?.schema, entry, this.taxonomies, this.importConfig);
// will replace all old asset uid/urls with new ones
entry = lookupAssets(
{
content_type: contentType,
entry: entry,
},
this.assetUidMapper,
this.assetUrlMapper,
path.join(this.entriesPath, cTUid),
this.installedExtensions,
);
delete entry.publish_details;
// checking the entry is a localized one or not
if (!isMasterLocale && this.entriesUidMapper.hasOwnProperty(entry.uid)) {
const entryResponse = this.stack.contentType(contentType.uid).entry(this.entriesUidMapper[entry.uid]);
Object.assign(entryResponse, cloneDeep(entry), { uid: this.entriesUidMapper[entry.uid] });
apiOptions.apiData = entryResponse;
apiOptions.additionalInfo[entryResponse.uid] = {
isLocalized: true,
entryOldUid: entry.uid,
};
return apiOptions;
}
apiOptions.apiData = entry;
} catch (error) {
log(
this.importConfig,
`${entry.title} entry of content type ${cTUid} in locale ${locale} failed to create`,
'error',
);
log(this.importConfig, formatError(error), 'error');
this.failedEntries.push({ content_type: cTUid, locale, entry: { uid: entry.uid, title: entry.title } });
apiOptions.apiData = null;
}
return apiOptions;
}
async replaceEntries({ cTUid, locale }: { cTUid: string; locale: string }): Promise<void> {
const processName = 'Replace existing Entries';
const indexFileName = 'index.json';
const basePath = path.join(this.entriesMapperPath, cTUid, locale, 'existing');
const fs = new FsUtility({ basePath, indexFileName });
const indexer = fs.indexFileContent;
const indexerCount = values(indexer).length;
if (indexerCount === 0) {
return Promise.resolve();
}
// Write updated entries
const entriesReplaceFileHelper = new FsUtility({
moduleName: 'entries',
indexFileName: 'index.json',
basePath: path.join(this.entriesMapperPath, cTUid, locale),
chunkFileSize: this.entriesConfig.chunkFileSize,
keepMetadata: false,
useIndexer: true,
omitKeys: this.entriesConfig.invalidKeys,
});
// log(this.importConfig, `Starting to update entries with references for ${cTUid} in locale ${locale}`, 'info');
const contentType = find(this.cTs, { uid: cTUid });
const onSuccess = ({ response, apiData: entry, additionalInfo }: any) => {
log(this.importConfig, `Replaced entry: '${entry.title}' of content type ${cTUid} in locale ${locale}`, 'info');
this.entriesUidMapper[entry.uid] = response.uid;
entriesReplaceFileHelper.writeIntoFile({ [entry.uid]: entry } as any, { mapKeyVal: true });
};
const onReject = ({ error, apiData: { uid, title } }: any) => {
// NOTE Remove from list if any entry import failed
remove(this.entriesForVariant, { locale, entry_uid: uid });
log(this.importConfig, `${title} entry of content type ${cTUid} in locale ${locale} failed to replace`, 'error');
log(this.importConfig, formatError(error), 'error');
this.failedEntries.push({
content_type: cTUid,
locale,
entry: { uid: this.entriesUidMapper[uid], title },
entryId: uid,
});
};
for (const index in indexer) {
const chunk = await fs.readChunkFiles.next().catch((error) => {
log(this.importConfig, formatError(error), 'error');
});
if (chunk) {
let apiContent = values(chunk as Record<string, any>[]);
await this.makeConcurrentCall(
{
apiContent,
processName,
indexerCount,
currentIndexer: +index,
apiParams: {
reject: onReject,
resolve: onSuccess,
entity: 'update-entries',
includeParamOnCompletion: true,
additionalInfo: { contentType, locale, cTUid },
},
concurrencyLimit: this.importConcurrency,
},
this.replaceEntriesHandler.bind(this),
).then(() => {
entriesReplaceFileHelper?.completeFile(true);
log(this.importConfig, `Replaced entries for content type ${cTUid} in locale ${locale}`, 'success');
});
}
}
}
async replaceEntriesHandler({
apiParams,
element: entry,
}: {
apiParams: ApiOptions;
element: Record<string, string>;
isLastRequest: boolean;
}) {
const { additionalInfo: { cTUid, locale } = {} } = apiParams;
return new Promise(async (resolve, reject) => {
const { items: [entryInStack] = [] }: any =
(await this.stack
.contentType(cTUid)
.entry()
.query({ query: { title: entry.title, locale } })
.findOne()
.catch((error: Error) => {
apiParams.reject({
error,
apiData: entry,
});
reject(true);
})) || {};
if (entryInStack) {
const entryPayload = this.stack.contentType(cTUid).entry(entryInStack.uid);
Object.assign(entryPayload, entryInStack, cloneDeep(entry), {
uid: entryInStack.uid,
urlPath: entryInStack.urlPath,
stackHeaders: entryInStack.stackHeaders,
_version: entryInStack._version,
});
return entryPayload
.update({ locale })
.then((response: any) => {
apiParams.resolve({
response,
apiData: entry,
});
resolve(true);
})
.catch((error: Error) => {
apiParams.reject({
error,
apiData: entry,
});
reject(true);
});
} else {
apiParams.reject({
error: new Error(`Entry with title ${entry.title} not found in the stack`),
apiData: entry,
});
reject(true);
}
});
}
populateEntryUpdatePayload(): { cTUid: string; locale: string }[] {
const requestOptions: { cTUid: string; locale: string }[] = [];
for (let locale of this.locales) {
for (let cTUid of this.refCTs) {
requestOptions.push({
cTUid,
locale: locale.code,
});
}
}
return requestOptions;
}
async updateEntriesWithReferences({ cTUid, locale }: { cTUid: string; locale: string }): Promise<void> {
const processName = 'Update Entries';
const indexFileName = 'index.json';
const basePath = path.join(this.entriesMapperPath, cTUid, locale);
const fs = new FsUtility({ basePath, indexFileName });
const indexer = fs.indexFileContent;
const indexerCount = values(indexer).length;
if (indexerCount === 0) {
return Promise.resolve();
}
// log(this.importConfig, `Starting to update entries with references for ${cTUid} in locale ${locale}`, 'info');
const contentType = find(this.cTs, { uid: cTUid });
const onSuccess = ({ response, apiData: { uid, url, title } }: any) => {
log(this.importConfig, `Updated entry: '${title}' of content type ${cTUid} in locale ${locale}`, 'info');
};
const onReject = ({ error, apiData: { uid, title } }: any) => {
// NOTE Remove from list if any entry import failed
remove(this.entriesForVariant, { locale, entry_uid: uid });
log(this.importConfig, `${title} entry of content type ${cTUid} in locale ${locale} failed to update`, 'error');
log(this.importConfig, formatError(error), 'error');
this.failedEntries.push({
content_type: cTUid,
locale,
entry: { uid: this.entriesUidMapper[uid], title },
entryId: uid,
});
};
for (const index in indexer) {
const chunk = await fs.readChunkFiles.next().catch((error) => {
log(this.importConfig, formatError(error), 'error');
});
if (chunk) {
let apiContent = values(chunk as Record<string, any>[]);
await this.makeConcurrentCall({
apiContent,
processName,
indexerCount,
currentIndexer: +index,
apiParams: {
reject: onReject,
resolve: onSuccess,
entity: 'update-entries',
includeParamOnCompletion: true,
serializeData: this.serializeUpdateEntries.bind(this),
additionalInfo: { contentType, locale, cTUid },
},
concurrencyLimit: this.importConcurrency,
}).then(() => {
log(this.importConfig, `Updated entries for content type ${cTUid} in locale ${locale}`, 'success');
});
}
}
}
/**
* @method serializeUpdateEntries
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializeUpdateEntries(apiOptions: ApiOptions): ApiOptions {
let {
apiData: entry,
additionalInfo: { cTUid, locale, contentType },
} = apiOptions;
try {
const sourceEntryFilePath = entry.sourceEntryFilePath;
const sourceEntry = ((fsUtil.readFile(sourceEntryFilePath) || {}) as Record<any, any>)[entry.entryOldUid];
const newUid = this.entriesUidMapper[entry.entryOldUid];
// Removing temp values
delete entry.sourceEntryFilePath;
delete entry.entryOldUid;
if (this.jsonRteCTs.indexOf(cTUid) > -1 || this.rteCTs.indexOf(cTUid) > -1) {
// the entries stored in eSuccessFilePath, have the same uids as the entries from source data
entry = restoreJsonRteEntryRefs(entry, sourceEntry, contentType.schema, {
uidMapper: this.entriesUidMapper,
mappedAssetUids: this.assetUidMapper,
mappedAssetUrls: this.assetUrlMapper,
});
}
entry = lookupAssets(
{
content_type: contentType,
entry: entry,
},
this.assetUidMapper,
this.assetUrlMapper,
path.join(this.entriesPath, cTUid),
this.installedExtensions,
);
entry = lookupEntries(
{
content_type: contentType,
entry,
},
this.entriesUidMapper,
path.join(this.entriesMapperPath, cTUid, locale),
);
const entryResponse = this.stack.contentType(contentType.uid).entry(newUid);
Object.assign(entryResponse, cloneDeep(entry), { uid: newUid });
delete entryResponse.publish_details;
apiOptions.apiData = entryResponse;
} catch (error) {
log(
this.importConfig,
`${entry.title} entry of content type ${cTUid} in locale ${locale} failed to update`,
'error',
);
log(this.importConfig, formatError(error), 'error');
apiOptions.apiData = null;
}
return apiOptions;
}
async enableMandatoryCTReferences(): Promise<void> {
const onSuccess = ({ response: contentType, apiData: { uid } }: any) => {
log(this.importConfig, `${uid} content type references updated`, 'success');
};
const onReject = ({ error, apiData: { uid } }: any) => {
log(this.importConfig, formatError(error), 'error');
throw new Error(`Failed to update references of content type ${uid}`);
};
return await this.makeConcurrentCall({
processName: 'Update content type references',
apiContent: this.modifiedCTs,
apiParams: {
serializeData: this.serializeUpdateCTsWithRef.bind(this),
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'update-cts',
includeParamOnCompletion: true,
},
concurrencyLimit: this.importConcurrency,
});
}
/**
* @method serializeUpdateCTsWithRef
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializeUpdateCTsWithRef(apiOptions: ApiOptions): ApiOptions {
const { apiData } = apiOptions;
const contentType = cloneDeep(apiData);
if (contentType.field_rules) {
delete contentType.field_rules;
}
lookupExtension(
this.importConfig,
contentType.schema,
this.importConfig.preserveStackVersion,
this.installedExtensions,
);
const contentTypePayload = this.stack.contentType(contentType.uid);
Object.assign(contentTypePayload, cloneDeep(contentType));
apiOptions.apiData = contentTypePayload;
return apiOptions;
}
async removeAutoCreatedEntries(): Promise<void> {
const onSuccess = ({ response, apiData: { entryUid } }: any) => {
// NOTE Remove entry from list
remove(this.entriesForVariant, {
entry_uid: entryUid,
locale: this.importConfig?.master_locale?.code,
});
log(this.importConfig, `Auto created entry in master locale removed - entry uid ${entryUid} `, 'success');
};
const onReject = ({ error, apiData: { entryUid } }: any) => {
// NOTE Remove entry from list
remove(this.entriesForVariant, {
entry_uid: entryUid,
locale: this.importConfig?.master_locale?.code,
});
log(
this.importConfig,
`Failed to remove auto created entry in master locale - entry uid ${entryUid} \n ${formatError(error)}`,
'error',
);
};
return await this.makeConcurrentCall({
processName: 'Remove auto created entry in master locale',
apiContent: this.autoCreatedEntries,
apiParams: {
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'delete-entries',
includeParamOnCompletion: true,
additionalInfo: { locale: this.importConfig?.master_locale?.code },
},
concurrencyLimit: this.importConcurrency,
});
}
async updateFieldRules(): Promise<void> {
let cTsWithFieldRules = fsUtil.readFile(path.join(this.cTsPath + '/field_rules_uid.json')) as Record<string, any>[];
if (!cTsWithFieldRules || cTsWithFieldRules?.length === 0) {
return;
}
for (let cTUid of cTsWithFieldRules) {
const cTs: Record<string, any>[] = fsUtil.readFile(path.join(this.cTsPath, 'schema.json')) as Record<
string,
unknown
>[];
const contentType: any = find(cTs, { uid: cTUid });
if (contentType.field_rules) {
const fieldDatatypeMap: { [key: string]: string } = {};
for (let i = 0; i < contentType.schema?.length; i++) {
const field = contentType.schema[i].uid;
fieldDatatypeMap[field] = contentType.schema[i].data_type;
}
let fieldRuleLength = contentType.field_rules?.length;
for (let k = 0; k < fieldRuleLength; k++) {
let fieldRuleConditionLength = contentType.field_rules[k].conditions?.length;
for (let i = 0; i < fieldRuleConditionLength; i++) {
if (fieldDatatypeMap[contentType.field_rules[k].conditions[i].operand_field] === 'reference') {
let fieldRulesValue = contentType.field_rules[k].conditions[i].value;
let fieldRulesArray = fieldRulesValue.split('.');
let updatedValue = [];
for (const element of fieldRulesArray) {
let splittedFieldRulesValue = element;
if (this.entriesUidMapper.hasOwnProperty(splittedFieldRulesValue)) {
updatedValue.push(this.entriesUidMapper[splittedFieldRulesValue]);
} else {
updatedValue.push(element);
}
}
contentType.field_rules[k].conditions[i].value = updatedValue.join('.');
}
}
}
const contentTypeResponse: any = await this.stack
.contentType(contentType.uid)
.fetch()
.catch((error) => {
log(this.importConfig, `failed to update the field rules of ${cTUid} ${formatError(error)}`, 'error');
});
if (!contentTypeResponse) {
continue;
}
contentTypeResponse.field_rules = contentType.field_rules;
await contentTypeResponse.update().catch((error: Error) => {
log(this.importConfig, `failed to update the field rules of ${cTUid} ${formatError(error)}`, 'error');
});
log(this.importConfig, `Updated the field rules of ${cTUid}`, 'info');
} else {
log(this.importConfig, `No field rules found in content type ${cTUid} to update`, 'info');
}
}
}
async publishEntries({ cTUid, locale }: { cTUid: string; locale: string }): Promise<void> {
const processName = 'Publish Entries';
const indexFileName = 'index.json';
const basePath = path.join(this.entriesPath, cTUid, locale);
const fs = new FsUtility({ basePath, indexFileName });
const indexer = fs.indexFileContent;
const indexerCount = values(indexer).length;
const contentType = find(this.cTs, { uid: cTUid });
if (indexerCount === 0) {
return Promise.resolve();
}
// log(this.importConfig, `Starting publish entries for ${cTUid} in locale ${locale}`, 'info');
const onSuccess = ({ response, apiData: { environments, entryUid, locales }, additionalInfo }: any) => {
log(
this.importConfig,
`Published the entry: '${entryUid}' of Content Type '${cTUid}' and Locale '${locale}' in Environments '${environments?.join(
',',
)}' and Locales '${locales?.join(',')}'`,
'info',
);
};
const onReject = ({ error, apiData: { environments, entryUid, locales }, additionalInfo }: any) => {
log(
this.importConfig,
`Failed to publish: '${entryUid}' entry of Content Type '${cTUid}' and Locale '${locale}' in Environments '${environments?.join(
',',
)}' and Locales '${locales?.join(',')}'`,
'error',
);
log(this.importConfig, formatError(error), 'error');
};
for (const index in indexer) {
const chunk = await fs.readChunkFiles.next().catch((error) => {
log(this.importConfig, formatError(error), 'error');
});
if (chunk) {
let apiContent = values(chunk as Record<string, any>[]);
let apiContentDuplicate: any = [];
apiContentDuplicate = apiContent.flatMap((content: Record<string, any>) => {
if (content?.publish_details?.length > 0) {
return content.publish_details.map((publish: Record<string, any>) => ({
...content,
locale: publish.locale,
publish_details: [publish],
}));
}
return []; // Return an empty array if publish_details is empty
});
apiContent = apiContentDuplicate;
if (apiContent?.length === 0) {
continue;
} else {
await this.makeConcurrentCall({
apiContent,
processName,
indexerCount,
currentIndexer: +index,
apiParams: {
reject: onReject,
resolve: onSuccess,
entity: 'publish-entries',
includeParamOnCompletion: true,
serializeData: this.serializePublishEntries.bind(this),
additionalInfo: { contentType, locale, cTUid },
},
concurrencyLimit: this.importConcurrency,
}).then(() => {
log(this.importConfig, `Published entries for content type ${cTUid} in locale ${locale}`, 'success');
});
}
}
}
}
/**
* @method serializeEntries
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializePublishEntries(apiOptions: ApiOptions): ApiOptions {
let { apiData: entry, additionalInfo } = apiOptions;
const requestObject: {
environments: Array<string>;
locales: Array<string>;
entryUid: string;
} = {