@contentstack/cli-cm-bulk-publish 1.5.0 → 1.7.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.
@@ -15,6 +15,8 @@ const assetQueue = getQueue();
15
15
  const { Command } = require('@contentstack/cli-command');
16
16
  const command = new Command();
17
17
  const { isEmpty } = require('../util');
18
+ const { fetchBulkPublishLimit } = require('../util/common-utility');
19
+ const VARIANTS_UNPUBLISH_API_VERSION = '3.2';
18
20
 
19
21
  let bulkUnPublishSet = [];
20
22
  let bulkUnPulishAssetSet = [];
@@ -50,22 +52,29 @@ function getQueryParams(filter) {
50
52
  return queryString;
51
53
  }
52
54
 
53
- function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion) {
55
+ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion, bulkPublishLimit, variantsFlag = false) {
54
56
  return new Promise(async (resolve) => {
55
57
  for (let index = 0; index < items.length; index++) {
56
58
  changedFlag = true;
57
59
 
58
60
  if (bulkUnpublish) {
59
- if (bulkUnPublishSet.length < 10 && items[index].type === 'entry_published') {
60
- bulkUnPublishSet.push({
61
+ if (bulkUnPublishSet.length < bulkPublishLimit && items[index].type === 'entry_published') {
62
+ const entryData = {
61
63
  uid: items[index].data.uid,
62
64
  content_type: items[index].content_type_uid,
63
65
  locale: items[index].data.locale || 'en-us',
64
- publish_details: [items[index].data.publish_details] || [],
65
- });
66
+ publish_details: items[index].data.publish_details || [],
67
+ };
68
+
69
+ if (variantsFlag && Array.isArray(items[index].data.variants) && items[index].data.variants.length > 0) {
70
+ const entryWithVariants = { ...entryData, variants: items[index].data.variants };
71
+ bulkUnPublishSet.push(entryWithVariants);
72
+ } else {
73
+ bulkUnPublishSet.push(entryData);
74
+ }
66
75
  }
67
76
 
68
- if (bulkUnPulishAssetSet.length < 10 && items[index].type === 'asset_published') {
77
+ if (bulkUnPulishAssetSet.length < bulkPublishLimit && items[index].type === 'asset_published') {
69
78
  bulkUnPulishAssetSet.push({
70
79
  uid: items[index].data.uid,
71
80
  version: items[index].data._version,
@@ -73,7 +82,7 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
73
82
  });
74
83
  }
75
84
 
76
- if (bulkUnPulishAssetSet.length === 10) {
85
+ if (bulkUnPulishAssetSet.length === bulkPublishLimit) {
77
86
  await queue.Enqueue({
78
87
  assets: bulkUnPulishAssetSet,
79
88
  Type: 'asset',
@@ -85,7 +94,7 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
85
94
  bulkUnPulishAssetSet = [];
86
95
  }
87
96
 
88
- if (bulkUnPublishSet.length === 10) {
97
+ if (bulkUnPublishSet.length === bulkPublishLimit) {
89
98
  await queue.Enqueue({
90
99
  entries: bulkUnPublishSet,
91
100
  locale: locale,
@@ -96,7 +105,7 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
96
105
  });
97
106
  bulkUnPublishSet = [];
98
107
  }
99
- if (index === items.length - 1 && bulkUnPulishAssetSet.length <= 10 && bulkUnPulishAssetSet.length > 0) {
108
+ if (index === items.length - 1 && bulkUnPulishAssetSet.length <= bulkPublishLimit && bulkUnPulishAssetSet.length > 0) {
100
109
  await queue.Enqueue({
101
110
  assets: bulkUnPulishAssetSet,
102
111
  Type: 'asset',
@@ -108,7 +117,7 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
108
117
  bulkUnPulishAssetSet = [];
109
118
  }
110
119
 
111
- if (index === items.length - 1 && bulkUnPublishSet.length <= 10 && bulkUnPublishSet.length > 0) {
120
+ if (index === items.length - 1 && bulkUnPublishSet.length <= bulkPublishLimit && bulkUnPublishSet.length > 0) {
112
121
  await queue.Enqueue({
113
122
  entries: bulkUnPublishSet,
114
123
  locale: locale,
@@ -121,7 +130,7 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
121
130
  }
122
131
  } else {
123
132
  if (items[index].type === 'entry_published') {
124
- await entryQueue.Enqueue({
133
+ await entryQueue.Enqueue({
125
134
  content_type: items[index].content_type_uid,
126
135
  publish_details: [items[index].data.publish_details],
127
136
  environments: [environment],
@@ -129,6 +138,7 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
129
138
  locale: items[index].data.locale || 'en-us',
130
139
  Type: 'entry',
131
140
  stack: stack,
141
+ apiVersion,
132
142
  });
133
143
  }
134
144
  if (items[index].type === 'asset_published') {
@@ -155,6 +165,8 @@ async function getSyncEntries(
155
165
  environment,
156
166
  deliveryToken,
157
167
  apiVersion,
168
+ bulkPublishLimit,
169
+ variantsFlag,
158
170
  paginationToken = null,
159
171
  ) {
160
172
  return new Promise(async (resolve, reject) => {
@@ -203,10 +215,17 @@ async function getSyncEntries(
203
215
  }
204
216
 
205
217
  const entriesResponse = await Stack.sync(syncData);
206
-
207
218
  if (entriesResponse.items.length > 0) {
208
- await bulkAction(stack, entriesResponse.items, bulkUnpublish, environment, locale, apiVersion);
219
+ if (variantsFlag) {
220
+ queryParamsObj.apiVersion = VARIANTS_UNPUBLISH_API_VERSION;
221
+ const itemsWithVariants = await attachVariantsToItems(stack, entriesResponse.items, queryParamsObj);
222
+ // Call bulkAction for entries with variants
223
+ await bulkAction(stack, itemsWithVariants, bulkUnpublish, environment, locale, apiVersion, bulkPublishLimit, variantsFlag);
224
+ }
225
+ // Call bulkAction for entries without variants
226
+ await bulkAction(stack, entriesResponse.items, bulkUnpublish, environment, locale, apiVersion, bulkPublishLimit, false);
209
227
  }
228
+
210
229
  if (entriesResponse.items.length === 0) {
211
230
  if (!changedFlag) console.log('No Entries/Assets Found published on specified environment');
212
231
  return resolve();
@@ -221,6 +240,8 @@ async function getSyncEntries(
221
240
  environment,
222
241
  deliveryToken,
223
242
  apiVersion,
243
+ bulkPublishLimit,
244
+ variantsFlag,
224
245
  null,
225
246
  );
226
247
  }, 3000);
@@ -229,6 +250,52 @@ async function getSyncEntries(
229
250
  }
230
251
  });
231
252
  }
253
+ async function attachVariantsToItems(stack, items, queryParams) {
254
+ for (const item of items) {
255
+ const { content_type_uid, data } = item;
256
+ const variantEntries = await getVariantEntries(stack, content_type_uid, item, queryParams); // Fetch the variants using fetchVariants method
257
+ item.data.variants = variantEntries; // Attach the fetched variants to the data object in the item
258
+ }
259
+ return items;
260
+ }
261
+
262
+ async function getVariantEntries(stack, contentType, entries, queryParams, skip = 0) {
263
+ try {
264
+ let variantQueryParams = {
265
+ locale: queryParams.locale || 'en-us',
266
+ include_count: true,
267
+ skip: skip, // Adding skip parameter for pagination
268
+ limit: 100, // Set a limit to fetch up to 100 entries per request
269
+ };
270
+
271
+ const variantsEntriesResponse = await stack
272
+ .contentType(contentType)
273
+ .entry(entries.data.uid)
274
+ .variants()
275
+ .query(variantQueryParams)
276
+ .find();
277
+
278
+ // Map the response items to extract variant UIDs
279
+ const variants = variantsEntriesResponse.items.map(entry => ({
280
+ uid: entry.variants._variant._uid,
281
+ }));
282
+
283
+ // Check if there are more entries to fetch
284
+ if (variantsEntriesResponse.items.length === variantQueryParams.limit) {
285
+ // Recursively fetch the next set of variants with updated skip value
286
+ const nextVariants = await getVariantEntries(stack, contentType, entries, queryParams, skip + variantQueryParams.limit);
287
+
288
+ // Ensure nextVariants is an array before concatenation
289
+ return Array.isArray(nextVariants) ? variants.concat(nextVariants) : variants;
290
+ }
291
+
292
+ return variants;
293
+ } catch (error) {
294
+ // Handle error message retrieval from different properties
295
+ const errorMessage = error?.errorMessage || error?.message || error?.errors || 'Falied to fetch the variant entries, Please contact the admin for support.';
296
+ throw new Error(`Error fetching variants: ${errorMessage}`);
297
+ }
298
+ }
232
299
 
233
300
  async function start(
234
301
  {
@@ -242,6 +309,7 @@ async function start(
242
309
  onlyEntries,
243
310
  f_types,
244
311
  apiVersion,
312
+ includeVariants,
245
313
  },
246
314
  stack,
247
315
  config,
@@ -256,7 +324,9 @@ async function start(
256
324
  }
257
325
  process.exit(0);
258
326
  });
259
-
327
+ if (includeVariants) {
328
+ apiVersion = VARIANTS_UNPUBLISH_API_VERSION;
329
+ }
260
330
  if (retryFailed) {
261
331
  if (typeof retryFailed === 'string' && retryFailed.length > 0) {
262
332
  if (!validateFile(retryFailed, ['unpublish', 'bulk-unpublish'])) {
@@ -294,7 +364,8 @@ async function start(
294
364
  }
295
365
  setConfig(config, bulkUnpublish);
296
366
  const queryParams = getQueryParams(filter);
297
- await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion);
367
+ const bulkPublishLimit = fetchBulkPublishLimit(stack?.org_uid);
368
+ await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion, bulkPublishLimit, includeVariants);
298
369
  }
299
370
  }
300
371
 
@@ -305,4 +376,4 @@ module.exports = {
305
376
  setConfig,
306
377
  getQueryParams,
307
378
  start,
308
- };
379
+ };