@contentstack/cli-cm-branches 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ $ npm install -g @contentstack/cli-cm-branches
|
|
|
37
37
|
$ csdx COMMAND
|
|
38
38
|
running command...
|
|
39
39
|
$ csdx (--version)
|
|
40
|
-
@contentstack/cli-cm-branches/1.
|
|
40
|
+
@contentstack/cli-cm-branches/1.3.0 linux-x64 node-v22.14.0
|
|
41
41
|
$ csdx --help [COMMAND]
|
|
42
42
|
USAGE
|
|
43
43
|
$ csdx COMMAND
|
|
@@ -47,7 +47,7 @@ function entryCreateScript(contentType) {
|
|
|
47
47
|
function getValueByPath(obj, path) {
|
|
48
48
|
return path.split('[').reduce((o, key) => o && o[key.replace(/\]$/, '')], obj);
|
|
49
49
|
}
|
|
50
|
-
|
|
50
|
+
|
|
51
51
|
function updateValueByPath(obj, path, newValue) {
|
|
52
52
|
path.split('[').reduce((o, key, index, arr) => {
|
|
53
53
|
if (index === arr.length - 1) {
|
|
@@ -57,10 +57,10 @@ function entryCreateScript(contentType) {
|
|
|
57
57
|
}
|
|
58
58
|
}, obj);
|
|
59
59
|
}
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
const findReference = function (schema, path, flag) {
|
|
62
62
|
let references = [];
|
|
63
|
-
|
|
63
|
+
|
|
64
64
|
for (const i in schema) {
|
|
65
65
|
const currentPath = path ? path + '[' + schema[i].uid : schema[i].uid;
|
|
66
66
|
if (schema[i].data_type === 'group' || schema[i].data_type === 'global_field') {
|
|
@@ -80,7 +80,7 @@ function entryCreateScript(contentType) {
|
|
|
80
80
|
references.push(currentPath);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
|
|
83
|
+
|
|
84
84
|
return references;
|
|
85
85
|
};
|
|
86
86
|
|
|
@@ -172,7 +172,7 @@ function entryCreateScript(contentType) {
|
|
|
172
172
|
}
|
|
173
173
|
parent.pop();
|
|
174
174
|
}
|
|
175
|
-
|
|
175
|
+
|
|
176
176
|
function findAssetIdsFromHtmlRte(entryObj, ctSchema) {
|
|
177
177
|
const regex = /<img asset_uid=\\"([^"]+)\\"/g;
|
|
178
178
|
let match;
|
|
@@ -182,7 +182,7 @@ function entryCreateScript(contentType) {
|
|
|
182
182
|
cAssetDetails.push({uid: match[1]});
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
|
-
|
|
185
|
+
|
|
186
186
|
function findFileUrls(schema, _entry) {
|
|
187
187
|
let markdownRegEx;
|
|
188
188
|
let markdownMatch;
|
|
@@ -194,7 +194,7 @@ function entryCreateScript(contentType) {
|
|
|
194
194
|
text = JSON.stringify(_entry);
|
|
195
195
|
}
|
|
196
196
|
markdownRegEx = new RegExp(
|
|
197
|
-
'(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))',
|
|
197
|
+
'(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-|gcp-eu-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))',
|
|
198
198
|
'g',
|
|
199
199
|
);
|
|
200
200
|
while ((markdownMatch = markdownRegEx.exec(text)) !== null) {
|
|
@@ -210,7 +210,7 @@ function entryCreateScript(contentType) {
|
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
212
|
}
|
|
213
|
-
|
|
213
|
+
|
|
214
214
|
function findAssetIdsFromJsonRte(entryObj, ctSchema) {
|
|
215
215
|
if(ctSchema !== undefined){
|
|
216
216
|
for (const element of ctSchema) {
|
|
@@ -256,7 +256,7 @@ function entryCreateScript(contentType) {
|
|
|
256
256
|
}
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
|
-
|
|
259
|
+
|
|
260
260
|
function gatherJsonRteAssetIds(jsonRteData) {
|
|
261
261
|
jsonRteData.children.forEach((element) => {
|
|
262
262
|
if (element.type) {
|
|
@@ -300,7 +300,7 @@ function entryCreateScript(contentType) {
|
|
|
300
300
|
}
|
|
301
301
|
});
|
|
302
302
|
}
|
|
303
|
-
|
|
303
|
+
|
|
304
304
|
const updateAssetDetailsInEntries = function (entry) {
|
|
305
305
|
let updatedEntry = Object.assign({},entry);
|
|
306
306
|
entry = updateFileFields(updatedEntry, entry, null)
|
|
@@ -313,7 +313,7 @@ function entryCreateScript(contentType) {
|
|
|
313
313
|
entry = entry.replace(assetUrl, mappedAssetUrl);
|
|
314
314
|
}
|
|
315
315
|
});
|
|
316
|
-
|
|
316
|
+
|
|
317
317
|
assetUIDs.forEach(function (assetUid) {
|
|
318
318
|
let uid = assetUIDMapper[assetUid];
|
|
319
319
|
if (typeof uid !== 'undefined') {
|
|
@@ -338,11 +338,11 @@ function entryCreateScript(contentType) {
|
|
|
338
338
|
parent[pos] = '';
|
|
339
339
|
}
|
|
340
340
|
};
|
|
341
|
-
|
|
341
|
+
|
|
342
342
|
if (parent.uid && assetUIDMapper[parent.uid]) {
|
|
343
343
|
parent.uid = assetUIDMapper[parent.uid];
|
|
344
344
|
}
|
|
345
|
-
|
|
345
|
+
|
|
346
346
|
if (
|
|
347
347
|
object &&
|
|
348
348
|
isObject(parent[pos]) &&
|
|
@@ -359,7 +359,7 @@ function entryCreateScript(contentType) {
|
|
|
359
359
|
) {
|
|
360
360
|
parent = omit(parent, ['asset']);
|
|
361
361
|
}
|
|
362
|
-
|
|
362
|
+
|
|
363
363
|
if (object.uid && assetUIDMapper[object.uid]) {
|
|
364
364
|
object.uid = assetUIDMapper[object.uid];
|
|
365
365
|
}
|
|
@@ -376,12 +376,12 @@ function entryCreateScript(contentType) {
|
|
|
376
376
|
} else if (isArray(object) && object.length) {
|
|
377
377
|
for (let i = 0; i <= object.length; i++){
|
|
378
378
|
updateFileFields(object[i], object, i);
|
|
379
|
-
}
|
|
379
|
+
}
|
|
380
380
|
parent[pos] = compact(object);
|
|
381
381
|
}
|
|
382
382
|
return object;
|
|
383
383
|
}
|
|
384
|
-
|
|
384
|
+
|
|
385
385
|
const checkAndDownloadAsset = async function (cAsset) {
|
|
386
386
|
const assetUID = cAsset?.uid;
|
|
387
387
|
if (cAsset && assetUID) {
|
|
@@ -434,24 +434,24 @@ function entryCreateScript(contentType) {
|
|
|
434
434
|
}
|
|
435
435
|
return cAsset;
|
|
436
436
|
};
|
|
437
|
-
|
|
437
|
+
|
|
438
438
|
const uploadAssets = async function () {
|
|
439
439
|
const assetFolderMap = JSON.parse(fs.readFileSync(path.resolve(filePath, 'folder-mapper.json'), 'utf8'));
|
|
440
440
|
const stackAPIClient = managementAPIClient.stack({ api_key: stackSDKInstance.api_key, branch_uid: branch });
|
|
441
441
|
for (let i = 0; i < downloadedAssets?.length; i++) {
|
|
442
442
|
const asset = downloadedAssets[i];
|
|
443
443
|
let requestOption = {};
|
|
444
|
-
|
|
444
|
+
|
|
445
445
|
requestOption.parent_uid = assetFolderMap[asset.parent_uid] || asset.parent_uid;
|
|
446
|
-
|
|
446
|
+
|
|
447
447
|
if (asset.hasOwnProperty('description') && typeof asset.description === 'string') {
|
|
448
448
|
requestOption.description = asset.description;
|
|
449
449
|
}
|
|
450
|
-
|
|
450
|
+
|
|
451
451
|
if (asset.hasOwnProperty('tags') && Array.isArray(asset.tags)) {
|
|
452
452
|
requestOption.tags = asset.tags;
|
|
453
453
|
}
|
|
454
|
-
|
|
454
|
+
|
|
455
455
|
if (asset.hasOwnProperty('title') && typeof asset.title === 'string') {
|
|
456
456
|
requestOption.title = asset.title;
|
|
457
457
|
}
|
|
@@ -467,7 +467,7 @@ function entryCreateScript(contentType) {
|
|
|
467
467
|
assetUrlMapper[asset.url] = res && res.url;
|
|
468
468
|
}
|
|
469
469
|
};
|
|
470
|
-
|
|
470
|
+
|
|
471
471
|
function handleErrorMsg(err) {
|
|
472
472
|
if (err?.errorMessage) {
|
|
473
473
|
console.log(err.errorMessage);
|
|
@@ -482,14 +482,14 @@ function entryCreateScript(contentType) {
|
|
|
482
482
|
limit,
|
|
483
483
|
include_count: true,
|
|
484
484
|
};
|
|
485
|
-
|
|
485
|
+
|
|
486
486
|
const entriesSearchResponse = await managementAPIClient
|
|
487
487
|
.stack({ api_key: stackSDKInstance.api_key, branch_uid: branchName })
|
|
488
488
|
.contentType(contentType)
|
|
489
489
|
.entry()
|
|
490
490
|
.query(requestObject)
|
|
491
491
|
.find();
|
|
492
|
-
|
|
492
|
+
|
|
493
493
|
if (entriesSearchResponse?.items?.length > 0) {
|
|
494
494
|
skip += limit || 100;
|
|
495
495
|
entries = [...entries, ...entriesSearchResponse.items];
|
|
@@ -510,12 +510,12 @@ function entryCreateScript(contentType) {
|
|
|
510
510
|
task: async () => {
|
|
511
511
|
|
|
512
512
|
const compareBranchEntries = await getEntries(compareBranch, '${contentType}')
|
|
513
|
-
|
|
513
|
+
|
|
514
514
|
const compareFilteredProperties = compareBranchEntries.map((entry) => {
|
|
515
515
|
keysToRemove.map((key) => delete entry[key]);
|
|
516
516
|
return entry;
|
|
517
517
|
});
|
|
518
|
-
|
|
518
|
+
|
|
519
519
|
let contentType = await managementAPIClient
|
|
520
520
|
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
|
|
521
521
|
.contentType('${contentType}')
|
|
@@ -556,7 +556,7 @@ function entryCreateScript(contentType) {
|
|
|
556
556
|
for (let i in references) {
|
|
557
557
|
let compareEntryRef = getValueByPath(entryDetails, references[i]);
|
|
558
558
|
let baseEntryRef = getValueByPath(baseEntry, references[i]);
|
|
559
|
-
|
|
559
|
+
|
|
560
560
|
if (compareEntryRef && compareEntryRef.length > 0 && baseEntryRef && baseEntryRef.length >= 0) {
|
|
561
561
|
let compareRefEntry = await managementAPIClient
|
|
562
562
|
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
|
|
@@ -568,7 +568,7 @@ function entryCreateScript(contentType) {
|
|
|
568
568
|
.entry()
|
|
569
569
|
.query({ query: { title: compareRefEntry.title } })
|
|
570
570
|
.find();
|
|
571
|
-
|
|
571
|
+
|
|
572
572
|
if(baseRefEntry?.items?.length > 0 && baseRefEntry.items[0]?.uid){
|
|
573
573
|
updateValueByPath(entryDetails, references[i], baseRefEntry.items[0].uid);
|
|
574
574
|
}
|
|
@@ -33,7 +33,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
33
33
|
'setWorkflowStage',
|
|
34
34
|
'import',
|
|
35
35
|
];
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
let compareBranch = config['compare-branch'];
|
|
38
38
|
let filePath = config['file-path'] || process.cwd();
|
|
39
39
|
let assetDirPath = path.resolve(filePath, 'assets');
|
|
@@ -44,7 +44,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
44
44
|
let assetUrlMapper = {};
|
|
45
45
|
let assetRefPath = {};
|
|
46
46
|
let parent=[];
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
function converter(data) {
|
|
49
49
|
let arr = [];
|
|
50
50
|
for (const elm of data) {
|
|
@@ -53,23 +53,23 @@ function entryCreateUpdateScript(contentType) {
|
|
|
53
53
|
}
|
|
54
54
|
return arr;
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
function deleteUnwantedKeysFromObject(obj, keysToRemove) {
|
|
58
58
|
if(obj){
|
|
59
59
|
keysToRemove.map((key) => delete obj[key]);
|
|
60
60
|
return obj;
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
|
|
63
|
+
|
|
64
64
|
function uniquelyConcatenateArrays(compareArr, baseArr) {
|
|
65
65
|
let uniqueArray = compareArr.concat(baseArr.filter((item) => compareArr.indexOf(item) < 0));
|
|
66
66
|
return uniqueArray;
|
|
67
67
|
}
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
function getValueByPath(obj, path) {
|
|
70
70
|
return path.split('[').reduce((o, key) => o && o[key.replace(/\]$/, '')], obj);
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
|
|
73
73
|
function updateValueByPath(obj, path, newValue) {
|
|
74
74
|
path.split('[').reduce((o, key, index, arr) => {
|
|
75
75
|
if (index === arr.length - 1) {
|
|
@@ -82,7 +82,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
82
82
|
|
|
83
83
|
const findReference = function (schema, path, flag) {
|
|
84
84
|
let references = [];
|
|
85
|
-
|
|
85
|
+
|
|
86
86
|
for (const i in schema) {
|
|
87
87
|
const currentPath = path ? path + '[' + schema[i].uid : schema[i].uid;
|
|
88
88
|
if (schema[i].data_type === 'group' || schema[i].data_type === 'global_field') {
|
|
@@ -102,7 +102,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
102
102
|
references.push(currentPath);
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
|
|
105
|
+
|
|
106
106
|
return references;
|
|
107
107
|
};
|
|
108
108
|
|
|
@@ -159,7 +159,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
};
|
|
162
|
-
|
|
162
|
+
|
|
163
163
|
function fetchAssetFromFileFields (parent, schema, entry) {
|
|
164
164
|
parent.push(schema.uid);
|
|
165
165
|
let updatedEntry = entry;
|
|
@@ -205,7 +205,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
205
205
|
assetDetails.push({uid: match[1]});
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
|
-
|
|
208
|
+
|
|
209
209
|
function findFileUrls(schema, _entry) {
|
|
210
210
|
let markdownRegEx;
|
|
211
211
|
let markdownMatch;
|
|
@@ -217,7 +217,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
217
217
|
text = JSON.stringify(_entry);
|
|
218
218
|
}
|
|
219
219
|
markdownRegEx = new RegExp(
|
|
220
|
-
'(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))',
|
|
220
|
+
'(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-|gcp-eu-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))',
|
|
221
221
|
'g',
|
|
222
222
|
);
|
|
223
223
|
while ((markdownMatch = markdownRegEx.exec(text)) !== null) {
|
|
@@ -233,7 +233,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
233
233
|
}
|
|
234
234
|
}
|
|
235
235
|
}
|
|
236
|
-
|
|
236
|
+
|
|
237
237
|
function findAssetIdsFromJsonRte(entryObj, ctSchema) {
|
|
238
238
|
if(ctSchema !== undefined){
|
|
239
239
|
for (const element of ctSchema) {
|
|
@@ -279,7 +279,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
279
279
|
}
|
|
280
280
|
}
|
|
281
281
|
}
|
|
282
|
-
|
|
282
|
+
|
|
283
283
|
function gatherJsonRteAssetIds(jsonRteData) {
|
|
284
284
|
jsonRteData.children.forEach((element) => {
|
|
285
285
|
if (element.type) {
|
|
@@ -323,7 +323,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
323
323
|
}
|
|
324
324
|
});
|
|
325
325
|
}
|
|
326
|
-
|
|
326
|
+
|
|
327
327
|
const updateAssetDetailsInEntries = function (entry) {
|
|
328
328
|
let updatedEntry = Object.assign({},entry);
|
|
329
329
|
entry = updateFileFields(updatedEntry, entry, null)
|
|
@@ -336,7 +336,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
336
336
|
entry = entry.replace(assetUrl, mappedAssetUrl);
|
|
337
337
|
}
|
|
338
338
|
});
|
|
339
|
-
|
|
339
|
+
|
|
340
340
|
assetUIDs.forEach(function (assetUid) {
|
|
341
341
|
let uid = assetUIDMapper[assetUid];
|
|
342
342
|
if (typeof uid !== 'undefined') {
|
|
@@ -361,11 +361,11 @@ function entryCreateUpdateScript(contentType) {
|
|
|
361
361
|
parent[pos] = '';
|
|
362
362
|
}
|
|
363
363
|
};
|
|
364
|
-
|
|
364
|
+
|
|
365
365
|
if (parent.uid && assetUIDMapper[parent.uid]) {
|
|
366
366
|
parent.uid = assetUIDMapper[parent.uid];
|
|
367
367
|
}
|
|
368
|
-
|
|
368
|
+
|
|
369
369
|
if (
|
|
370
370
|
object &&
|
|
371
371
|
isObject(parent[pos]) &&
|
|
@@ -382,7 +382,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
382
382
|
) {
|
|
383
383
|
parent = omit(parent, ['asset']);
|
|
384
384
|
}
|
|
385
|
-
|
|
385
|
+
|
|
386
386
|
if (object.uid && assetUIDMapper[object.uid]) {
|
|
387
387
|
object.uid = assetUIDMapper[object.uid];
|
|
388
388
|
}
|
|
@@ -399,12 +399,12 @@ function entryCreateUpdateScript(contentType) {
|
|
|
399
399
|
} else if (isArray(object) && object.length) {
|
|
400
400
|
for (let i = 0; i <= object.length; i++){
|
|
401
401
|
updateFileFields(object[i], object, i);
|
|
402
|
-
}
|
|
402
|
+
}
|
|
403
403
|
parent[pos] = compact(object);
|
|
404
404
|
}
|
|
405
405
|
return object;
|
|
406
406
|
}
|
|
407
|
-
|
|
407
|
+
|
|
408
408
|
const checkAndDownloadAsset = async function (cAsset) {
|
|
409
409
|
if (cAsset) {
|
|
410
410
|
const assetUID = cAsset.uid;
|
|
@@ -457,24 +457,24 @@ function entryCreateUpdateScript(contentType) {
|
|
|
457
457
|
}
|
|
458
458
|
return cAsset;
|
|
459
459
|
};
|
|
460
|
-
|
|
460
|
+
|
|
461
461
|
const uploadAssets = async function () {
|
|
462
462
|
const assetFolderMap = JSON.parse(fs.readFileSync(path.resolve(filePath, 'folder-mapper.json'), 'utf8'));
|
|
463
463
|
const stackAPIClient = managementAPIClient.stack({ api_key: stackSDKInstance.api_key, branch_uid: branch });
|
|
464
464
|
for (let i = 0; i < downloadedAssets?.length; i++) {
|
|
465
465
|
const asset = downloadedAssets[i];
|
|
466
466
|
let requestOption = {};
|
|
467
|
-
|
|
467
|
+
|
|
468
468
|
requestOption.parent_uid = assetFolderMap[asset.parent_uid] || asset.parent_uid;
|
|
469
|
-
|
|
469
|
+
|
|
470
470
|
if (asset.hasOwnProperty('description') && typeof asset.description === 'string') {
|
|
471
471
|
requestOption.description = asset.description;
|
|
472
472
|
}
|
|
473
|
-
|
|
473
|
+
|
|
474
474
|
if (asset.hasOwnProperty('tags') && Array.isArray(asset.tags)) {
|
|
475
475
|
requestOption.tags = asset.tags;
|
|
476
476
|
}
|
|
477
|
-
|
|
477
|
+
|
|
478
478
|
if (asset.hasOwnProperty('title') && typeof asset.title === 'string') {
|
|
479
479
|
requestOption.title = asset.title;
|
|
480
480
|
}
|
|
@@ -497,14 +497,14 @@ function entryCreateUpdateScript(contentType) {
|
|
|
497
497
|
limit,
|
|
498
498
|
include_count: true,
|
|
499
499
|
};
|
|
500
|
-
|
|
500
|
+
|
|
501
501
|
const entriesSearchResponse = await managementAPIClient
|
|
502
502
|
.stack({ api_key: stackSDKInstance.api_key, branch_uid: branchName })
|
|
503
503
|
.contentType(contentType)
|
|
504
504
|
.entry()
|
|
505
505
|
.query(requestObject)
|
|
506
506
|
.find();
|
|
507
|
-
|
|
507
|
+
|
|
508
508
|
if (entriesSearchResponse?.items?.length > 0) {
|
|
509
509
|
skip += limit || 100;
|
|
510
510
|
entries = [...entries, ...entriesSearchResponse.items];
|
|
@@ -523,14 +523,14 @@ function entryCreateUpdateScript(contentType) {
|
|
|
523
523
|
failedMessage: 'Failed to update entries',
|
|
524
524
|
task: async () => {
|
|
525
525
|
let compareBranchEntries = await getEntries(compareBranch, '${contentType}')
|
|
526
|
-
|
|
526
|
+
|
|
527
527
|
let baseBranchEntries = await getEntries(branch, '${contentType}');
|
|
528
|
-
|
|
528
|
+
|
|
529
529
|
let contentType = await managementAPIClient
|
|
530
530
|
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
|
|
531
531
|
.contentType('${contentType}')
|
|
532
532
|
.fetch();
|
|
533
|
-
|
|
533
|
+
|
|
534
534
|
for (let i = 0; i < compareBranchEntries?.length; i++) {
|
|
535
535
|
assetRefPath[compareBranchEntries[i].uid] = []
|
|
536
536
|
findAssets(contentType.schema, compareBranchEntries[i], assetRefPath[compareBranchEntries[i].uid]);
|
|
@@ -557,13 +557,13 @@ function entryCreateUpdateScript(contentType) {
|
|
|
557
557
|
}
|
|
558
558
|
if (downloadedAssets?.length) await uploadAssets();
|
|
559
559
|
}
|
|
560
|
-
|
|
560
|
+
|
|
561
561
|
let flag = {
|
|
562
562
|
references: false
|
|
563
563
|
};
|
|
564
|
-
|
|
564
|
+
|
|
565
565
|
const references = await findReference(contentType.schema, '', flag);
|
|
566
|
-
|
|
566
|
+
|
|
567
567
|
async function updateEntry(entry, entryDetails) {
|
|
568
568
|
if (entry) {
|
|
569
569
|
Object.assign(entry, { ...entryDetails });
|
|
@@ -575,7 +575,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
575
575
|
for (let i in references) {
|
|
576
576
|
let compareEntryRef = getValueByPath(entryDetails, references[i]);
|
|
577
577
|
let baseEntryRef = getValueByPath(baseEntry, references[i]);
|
|
578
|
-
|
|
578
|
+
|
|
579
579
|
if (compareEntryRef && compareEntryRef.length > 0 && baseEntryRef && baseEntryRef.length >= 0) {
|
|
580
580
|
let compareRefEntry = await managementAPIClient
|
|
581
581
|
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
|
|
@@ -587,7 +587,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
587
587
|
.entry()
|
|
588
588
|
.query({ query: { title: compareRefEntry.title } })
|
|
589
589
|
.find();
|
|
590
|
-
|
|
590
|
+
|
|
591
591
|
if(baseRefEntry?.items?.length > 0 && baseRefEntry.items[0]?.uid){
|
|
592
592
|
updateValueByPath(entryDetails, references[i], baseRefEntry.items[0].uid);
|
|
593
593
|
}
|
|
@@ -601,23 +601,23 @@ function entryCreateUpdateScript(contentType) {
|
|
|
601
601
|
let entryDetails = deleteUnwantedKeysFromObject(el, keysToRemove);
|
|
602
602
|
if(entryDetails !== undefined){
|
|
603
603
|
entryDetails = updateAssetDetailsInEntries(entryDetails);
|
|
604
|
-
|
|
604
|
+
|
|
605
605
|
if (baseBranchEntries && baseBranchEntries.length) {
|
|
606
606
|
let baseEntryUid = baseBranchEntries[0].uid;
|
|
607
607
|
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid);
|
|
608
|
-
|
|
608
|
+
|
|
609
609
|
if (flag.references) {
|
|
610
610
|
await updateReferences(entryDetails, baseBranchEntries[0], references);
|
|
611
611
|
}
|
|
612
|
-
|
|
612
|
+
|
|
613
613
|
await updateEntry(entry, entryDetails);
|
|
614
614
|
} else {
|
|
615
615
|
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails });
|
|
616
|
-
|
|
616
|
+
|
|
617
617
|
if (flag.references) {
|
|
618
618
|
await updateReferences(entryDetails, createdEntry, references);
|
|
619
619
|
}
|
|
620
|
-
|
|
620
|
+
|
|
621
621
|
await updateEntry(createdEntry, entryDetails);
|
|
622
622
|
}
|
|
623
623
|
}
|
|
@@ -630,7 +630,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
630
630
|
//TODO: Need to discuss this approach and replace it with uid condition
|
|
631
631
|
let arr = uniquelyConcatenateArrays(Array.from(compareMap.keys()), Array.from(baseMap.keys()));
|
|
632
632
|
|
|
633
|
-
// Change
|
|
633
|
+
// Change
|
|
634
634
|
for(let i = 0; i < arr.length ; i++){
|
|
635
635
|
let entryDetails = deleteUnwantedKeysFromObject(compareMap.get(arr[i]), keysToRemove);
|
|
636
636
|
//NOTE: In the compare branch, entry must exist. Condition of deleted entry not handled
|
|
@@ -645,17 +645,17 @@ function entryCreateUpdateScript(contentType) {
|
|
|
645
645
|
if(createdEntry){
|
|
646
646
|
if (flag.references) {
|
|
647
647
|
await updateReferences(entryDetails, createdEntry, references);
|
|
648
|
-
}
|
|
648
|
+
}
|
|
649
649
|
await updateEntry(createdEntry, entryDetails);
|
|
650
650
|
}
|
|
651
651
|
} else if (compareMap.get(arr[i]) && baseMap.get(arr[i])) {
|
|
652
652
|
let baseEntry = baseMap.get(arr[i]);
|
|
653
653
|
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid);
|
|
654
|
-
|
|
654
|
+
|
|
655
655
|
if (flag.references) {
|
|
656
656
|
await updateReferences(entryDetails, baseEntry, references);
|
|
657
657
|
}
|
|
658
|
-
|
|
658
|
+
|
|
659
659
|
await updateEntry(entry, entryDetails);
|
|
660
660
|
}
|
|
661
661
|
}
|
|
@@ -667,7 +667,7 @@ function entryCreateUpdateScript(contentType) {
|
|
|
667
667
|
},
|
|
668
668
|
};
|
|
669
669
|
};
|
|
670
|
-
|
|
670
|
+
|
|
671
671
|
if (compareBranch && branch.length !== 0 && apiKey.length !== 0) {
|
|
672
672
|
migration.addTask(updateEntryTask());
|
|
673
673
|
} else {
|
|
@@ -33,7 +33,7 @@ function entryUpdateScript(contentType) {
|
|
|
33
33
|
'setWorkflowStage',
|
|
34
34
|
'import',
|
|
35
35
|
];
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
let compareBranch = config['compare-branch'];
|
|
38
38
|
let filePath = config['file-path'] || process.cwd();
|
|
39
39
|
let assetDirPath = path.resolve(filePath, 'assets');
|
|
@@ -44,7 +44,7 @@ function entryUpdateScript(contentType) {
|
|
|
44
44
|
let assetUrlMapper = {};
|
|
45
45
|
let assetRefPath = {};
|
|
46
46
|
let parent=[];
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
function converter(data) {
|
|
49
49
|
let arr = [];
|
|
50
50
|
for (const elm of data) {
|
|
@@ -53,23 +53,23 @@ function entryUpdateScript(contentType) {
|
|
|
53
53
|
}
|
|
54
54
|
return arr;
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
|
|
57
57
|
function deleteUnwantedKeysFromObject(obj, keysToRemove) {
|
|
58
58
|
if(obj){
|
|
59
59
|
keysToRemove.map((key) => delete obj[key]);
|
|
60
60
|
return obj;
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
|
|
63
|
+
|
|
64
64
|
function uniquelyConcatenateArrays(compareArr, baseArr) {
|
|
65
65
|
let uniqueArray = compareArr.concat(baseArr.filter((item) => compareArr.indexOf(item) < 0));
|
|
66
66
|
return uniqueArray;
|
|
67
67
|
}
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
function getValueByPath(obj, path) {
|
|
70
70
|
return path.split('[').reduce((o, key) => o && o[key.replace(/\]$/, '')], obj);
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
|
|
73
73
|
function updateValueByPath(obj, path, newValue) {
|
|
74
74
|
path.split('[').reduce((o, key, index, arr) => {
|
|
75
75
|
if (index === arr.length - 1) {
|
|
@@ -82,7 +82,7 @@ function entryUpdateScript(contentType) {
|
|
|
82
82
|
|
|
83
83
|
const findReference = function (schema, path, flag) {
|
|
84
84
|
let references = [];
|
|
85
|
-
|
|
85
|
+
|
|
86
86
|
for (const i in schema) {
|
|
87
87
|
const currentPath = path ? path + '[' + schema[i].uid : schema[i].uid;
|
|
88
88
|
if (schema[i].data_type === 'group' || schema[i].data_type === 'global_field') {
|
|
@@ -102,7 +102,7 @@ function entryUpdateScript(contentType) {
|
|
|
102
102
|
references.push(currentPath);
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
|
|
105
|
+
|
|
106
106
|
return references;
|
|
107
107
|
};
|
|
108
108
|
|
|
@@ -194,7 +194,7 @@ function entryUpdateScript(contentType) {
|
|
|
194
194
|
}
|
|
195
195
|
parent.pop();
|
|
196
196
|
}
|
|
197
|
-
|
|
197
|
+
|
|
198
198
|
function findAssetIdsFromHtmlRte(entryObj, ctSchema) {
|
|
199
199
|
const regex = /<img asset_uid=\\"([^"]+)\\"/g;
|
|
200
200
|
let match;
|
|
@@ -204,7 +204,7 @@ function entryUpdateScript(contentType) {
|
|
|
204
204
|
assetDetails.push({uid: match[1]});
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
|
-
|
|
207
|
+
|
|
208
208
|
function findFileUrls(schema, _entry) {
|
|
209
209
|
let markdownRegEx;
|
|
210
210
|
let markdownMatch;
|
|
@@ -216,7 +216,7 @@ function entryUpdateScript(contentType) {
|
|
|
216
216
|
text = JSON.stringify(_entry);
|
|
217
217
|
}
|
|
218
218
|
markdownRegEx = new RegExp(
|
|
219
|
-
'(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))',
|
|
219
|
+
'(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-|gcp-eu-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))',
|
|
220
220
|
'g',
|
|
221
221
|
);
|
|
222
222
|
while ((markdownMatch = markdownRegEx.exec(text)) !== null) {
|
|
@@ -232,7 +232,7 @@ function entryUpdateScript(contentType) {
|
|
|
232
232
|
}
|
|
233
233
|
}
|
|
234
234
|
}
|
|
235
|
-
|
|
235
|
+
|
|
236
236
|
function findAssetIdsFromJsonRte(entryObj, ctSchema) {
|
|
237
237
|
if(ctSchema !== undefined){
|
|
238
238
|
for (const element of ctSchema) {
|
|
@@ -278,7 +278,7 @@ function entryUpdateScript(contentType) {
|
|
|
278
278
|
}
|
|
279
279
|
}
|
|
280
280
|
}
|
|
281
|
-
|
|
281
|
+
|
|
282
282
|
function gatherJsonRteAssetIds(jsonRteData) {
|
|
283
283
|
jsonRteData.children.forEach((element) => {
|
|
284
284
|
if (element.type) {
|
|
@@ -322,7 +322,7 @@ function entryUpdateScript(contentType) {
|
|
|
322
322
|
}
|
|
323
323
|
});
|
|
324
324
|
}
|
|
325
|
-
|
|
325
|
+
|
|
326
326
|
const updateAssetDetailsInEntries = function (entry) {
|
|
327
327
|
let updatedEntry = Object.assign({},entry);
|
|
328
328
|
entry = updateFileFields(updatedEntry, entry, null)
|
|
@@ -335,7 +335,7 @@ function entryUpdateScript(contentType) {
|
|
|
335
335
|
entry = entry.replace(assetUrl, mappedAssetUrl);
|
|
336
336
|
}
|
|
337
337
|
});
|
|
338
|
-
|
|
338
|
+
|
|
339
339
|
assetUIDs.forEach(function (assetUid) {
|
|
340
340
|
let uid = assetUIDMapper[assetUid];
|
|
341
341
|
if (typeof uid !== 'undefined') {
|
|
@@ -360,11 +360,11 @@ function entryUpdateScript(contentType) {
|
|
|
360
360
|
parent[pos] = '';
|
|
361
361
|
}
|
|
362
362
|
};
|
|
363
|
-
|
|
363
|
+
|
|
364
364
|
if (parent.uid && assetUIDMapper[parent.uid]) {
|
|
365
365
|
parent.uid = assetUIDMapper[parent.uid];
|
|
366
366
|
}
|
|
367
|
-
|
|
367
|
+
|
|
368
368
|
if (
|
|
369
369
|
object &&
|
|
370
370
|
isObject(parent[pos]) &&
|
|
@@ -381,7 +381,7 @@ function entryUpdateScript(contentType) {
|
|
|
381
381
|
) {
|
|
382
382
|
parent = omit(parent, ['asset']);
|
|
383
383
|
}
|
|
384
|
-
|
|
384
|
+
|
|
385
385
|
if (object.uid && assetUIDMapper[object.uid]) {
|
|
386
386
|
object.uid = assetUIDMapper[object.uid];
|
|
387
387
|
}
|
|
@@ -398,12 +398,12 @@ function entryUpdateScript(contentType) {
|
|
|
398
398
|
} else if (isArray(object) && object.length) {
|
|
399
399
|
for (let i = 0; i <= object.length; i++){
|
|
400
400
|
updateFileFields(object[i], object, i);
|
|
401
|
-
}
|
|
401
|
+
}
|
|
402
402
|
parent[pos] = compact(object);
|
|
403
403
|
}
|
|
404
404
|
return object;
|
|
405
405
|
}
|
|
406
|
-
|
|
406
|
+
|
|
407
407
|
const checkAndDownloadAsset = async function (cAsset) {
|
|
408
408
|
if (cAsset) {
|
|
409
409
|
const assetUID = cAsset.uid;
|
|
@@ -456,24 +456,24 @@ function entryUpdateScript(contentType) {
|
|
|
456
456
|
}
|
|
457
457
|
return cAsset;
|
|
458
458
|
};
|
|
459
|
-
|
|
459
|
+
|
|
460
460
|
const uploadAssets = async function () {
|
|
461
461
|
const assetFolderMap = JSON.parse(fs.readFileSync(path.resolve(filePath, 'folder-mapper.json'), 'utf8'));
|
|
462
462
|
const stackAPIClient = managementAPIClient.stack({ api_key: stackSDKInstance.api_key, branch_uid: branch });
|
|
463
463
|
for (let i = 0; i < downloadedAssets?.length; i++) {
|
|
464
464
|
const asset = downloadedAssets[i];
|
|
465
465
|
let requestOption = {};
|
|
466
|
-
|
|
466
|
+
|
|
467
467
|
requestOption.parent_uid = assetFolderMap[asset.parent_uid] || asset.parent_uid;
|
|
468
|
-
|
|
468
|
+
|
|
469
469
|
if (asset.hasOwnProperty('description') && typeof asset.description === 'string') {
|
|
470
470
|
requestOption.description = asset.description;
|
|
471
471
|
}
|
|
472
|
-
|
|
472
|
+
|
|
473
473
|
if (asset.hasOwnProperty('tags') && Array.isArray(asset.tags)) {
|
|
474
474
|
requestOption.tags = asset.tags;
|
|
475
475
|
}
|
|
476
|
-
|
|
476
|
+
|
|
477
477
|
if (asset.hasOwnProperty('title') && typeof asset.title === 'string') {
|
|
478
478
|
requestOption.title = asset.title;
|
|
479
479
|
}
|
|
@@ -496,14 +496,14 @@ function entryUpdateScript(contentType) {
|
|
|
496
496
|
limit,
|
|
497
497
|
include_count: true,
|
|
498
498
|
};
|
|
499
|
-
|
|
499
|
+
|
|
500
500
|
const entriesSearchResponse = await managementAPIClient
|
|
501
501
|
.stack({ api_key: stackSDKInstance.api_key, branch_uid: branchName })
|
|
502
502
|
.contentType(contentType)
|
|
503
503
|
.entry()
|
|
504
504
|
.query(requestObject)
|
|
505
505
|
.find();
|
|
506
|
-
|
|
506
|
+
|
|
507
507
|
if (entriesSearchResponse?.items?.length > 0) {
|
|
508
508
|
skip += limit || 100;
|
|
509
509
|
entries = [...entries, ...entriesSearchResponse.items];
|
|
@@ -514,18 +514,18 @@ function entryUpdateScript(contentType) {
|
|
|
514
514
|
}
|
|
515
515
|
return entries;
|
|
516
516
|
};
|
|
517
|
-
|
|
517
|
+
|
|
518
518
|
const updateEntryTask = () => {
|
|
519
519
|
return {
|
|
520
520
|
title: 'Update Entries',
|
|
521
521
|
successMessage: 'Entries Updated Successfully',
|
|
522
522
|
failedMessage: 'Failed to update entries',
|
|
523
523
|
task: async () => {
|
|
524
|
-
|
|
524
|
+
|
|
525
525
|
let compareBranchEntries = await getEntries(compareBranch, '${contentType}');
|
|
526
|
-
|
|
526
|
+
|
|
527
527
|
let baseBranchEntries = await getEntries(branch, '${contentType}');
|
|
528
|
-
|
|
528
|
+
|
|
529
529
|
let contentType = await managementAPIClient
|
|
530
530
|
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
|
|
531
531
|
.contentType('${contentType}')
|
|
@@ -561,9 +561,9 @@ function entryUpdateScript(contentType) {
|
|
|
561
561
|
let flag = {
|
|
562
562
|
references: false
|
|
563
563
|
};
|
|
564
|
-
|
|
564
|
+
|
|
565
565
|
const references = await findReference(contentType.schema, '', flag);
|
|
566
|
-
|
|
566
|
+
|
|
567
567
|
async function updateEntry(entry, entryDetails) {
|
|
568
568
|
Object.assign(entry, { ...entryDetails });
|
|
569
569
|
await entry.update();
|
|
@@ -573,7 +573,7 @@ function entryUpdateScript(contentType) {
|
|
|
573
573
|
for (let i in references) {
|
|
574
574
|
let compareEntryRef = getValueByPath(entryDetails, references[i]);
|
|
575
575
|
let baseEntryRef = getValueByPath(baseEntry, references[i]);
|
|
576
|
-
|
|
576
|
+
|
|
577
577
|
if (compareEntryRef && compareEntryRef.length > 0 && baseEntryRef && baseEntryRef.length >= 0) {
|
|
578
578
|
let compareRefEntry = await managementAPIClient
|
|
579
579
|
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
|
|
@@ -585,7 +585,7 @@ function entryUpdateScript(contentType) {
|
|
|
585
585
|
.entry()
|
|
586
586
|
.query({ query: { title: compareRefEntry.title } })
|
|
587
587
|
.find();
|
|
588
|
-
|
|
588
|
+
|
|
589
589
|
if(baseRefEntry?.items?.length > 0 && baseRefEntry.items[0]?.uid){
|
|
590
590
|
updateValueByPath(entryDetails, references[i], baseRefEntry.items[0].uid);
|
|
591
591
|
}
|
|
@@ -602,19 +602,19 @@ function entryUpdateScript(contentType) {
|
|
|
602
602
|
if (baseBranchEntries && baseBranchEntries.length) {
|
|
603
603
|
let baseEntryUid = baseBranchEntries[0].uid;
|
|
604
604
|
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntryUid);
|
|
605
|
-
|
|
605
|
+
|
|
606
606
|
if (flag.references) {
|
|
607
607
|
await updateReferences(entryDetails, baseBranchEntries[0], references);
|
|
608
608
|
}
|
|
609
|
-
|
|
609
|
+
|
|
610
610
|
await updateEntry(entry, entryDetails);
|
|
611
611
|
} else {
|
|
612
612
|
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails });
|
|
613
|
-
|
|
613
|
+
|
|
614
614
|
if (flag.references) {
|
|
615
615
|
await updateReferences(entryDetails, createdEntry, references);
|
|
616
616
|
}
|
|
617
|
-
|
|
617
|
+
|
|
618
618
|
await updateEntry(createdEntry, entryDetails);
|
|
619
619
|
}
|
|
620
620
|
}
|
|
@@ -626,7 +626,7 @@ function entryUpdateScript(contentType) {
|
|
|
626
626
|
//NOTE: Filter distinct entries from the base and compare branches according to their titles.
|
|
627
627
|
//TODO: Need to discuss this approach and replace it with uid approach
|
|
628
628
|
let arr = uniquelyConcatenateArrays(Array.from(compareMap.keys()), Array.from(baseMap.keys()));
|
|
629
|
-
|
|
629
|
+
|
|
630
630
|
arr.map(async (el) => {
|
|
631
631
|
let entryDetails = deleteUnwantedKeysFromObject(compareMap.get(el), keysToRemove);
|
|
632
632
|
//NOTE: In the compare branch, entry must exist. Condition of deleted entry not handled
|
|
@@ -637,22 +637,22 @@ function entryUpdateScript(contentType) {
|
|
|
637
637
|
.contentType('${contentType}')
|
|
638
638
|
.entry()
|
|
639
639
|
.create({ entry: entryDetails })
|
|
640
|
-
|
|
640
|
+
|
|
641
641
|
if(createdEntry){
|
|
642
642
|
if (flag.references) {
|
|
643
643
|
await updateReferences(entryDetails, createdEntry, references);
|
|
644
644
|
}
|
|
645
|
-
|
|
645
|
+
|
|
646
646
|
await updateEntry(createdEntry, entryDetails);
|
|
647
647
|
}
|
|
648
648
|
} else if (compareMap.get(el) && baseMap.get(el)) {
|
|
649
649
|
let baseEntry = baseMap.get(el);
|
|
650
650
|
let entry = await stackSDKInstance.contentType('${contentType}').entry(baseEntry.uid);
|
|
651
|
-
|
|
651
|
+
|
|
652
652
|
if (flag.references) {
|
|
653
653
|
await updateReferences(entryDetails, baseEntry, references);
|
|
654
654
|
}
|
|
655
|
-
|
|
655
|
+
|
|
656
656
|
await updateEntry(entry, entryDetails);
|
|
657
657
|
}
|
|
658
658
|
}
|
|
@@ -664,7 +664,7 @@ function entryUpdateScript(contentType) {
|
|
|
664
664
|
},
|
|
665
665
|
};
|
|
666
666
|
};
|
|
667
|
-
|
|
667
|
+
|
|
668
668
|
if (compareBranch && branch.length !== 0 && apiKey.length !== 0) {
|
|
669
669
|
migration.addTask(updateEntryTask());
|
|
670
670
|
} else {
|
package/oclif.manifest.json
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentstack/cli-cm-branches",
|
|
3
3
|
"description": "Contentstack CLI plugin to do branches operations",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"author": "Contentstack",
|
|
6
6
|
"bugs": "https://github.com/contentstack/cli/issues",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@contentstack/cli-command": "~1.3.3",
|
|
9
|
-
"@contentstack/cli-utilities": "~1.8.
|
|
9
|
+
"@contentstack/cli-utilities": "~1.8.4",
|
|
10
10
|
"@oclif/core": "^3.27.0",
|
|
11
11
|
"chalk": "^4.1.2",
|
|
12
12
|
"just-diff": "^6.0.2",
|