@contentstack/cli-cm-bulk-publish 1.6.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.
@@ -9,6 +9,7 @@ const retryFailedLogs = require('../util/retryfailed');
9
9
  const { validateFile } = require('../util/fs');
10
10
  const { isEmpty } = require('../util');
11
11
  const { fetchBulkPublishLimit } = require('../util/common-utility');
12
+ const VARIANTS_PUBLISH_API_VERSION = '3.2';
12
13
 
13
14
  const queue = getQueue();
14
15
 
@@ -27,6 +28,8 @@ async function getEntries(
27
28
  environments,
28
29
  apiVersion,
29
30
  bulkPublishLimit,
31
+ variantsFlag = false,
32
+ entry_uid = undefined,
30
33
  skip = 0,
31
34
  ) {
32
35
  return new Promise((resolve, reject) => {
@@ -39,6 +42,13 @@ async function getEntries(
39
42
  include_publish_details: true,
40
43
  };
41
44
 
45
+ if (variantsFlag) {
46
+ queryParams.apiVersion = VARIANTS_PUBLISH_API_VERSION;
47
+ }
48
+ if (entry_uid) {
49
+ queryParams.uid = entry_uid;
50
+ }
51
+
42
52
  stack
43
53
  .contentType(contentType)
44
54
  .entry()
@@ -47,16 +57,31 @@ async function getEntries(
47
57
  .then(async (entriesResponse) => {
48
58
  skipCount += entriesResponse.items.length;
49
59
  let entries = entriesResponse.items;
50
- for (let index = 0; index < entriesResponse.items.length; index++) {
60
+
61
+ for (let index = 0; index < entries.length; index++) {
62
+ let variants = [];
51
63
  if (bulkPublish) {
64
+ let entry;
52
65
  if (bulkPublishSet.length < bulkPublishLimit) {
53
- bulkPublishSet.push({
66
+ entry = {
54
67
  uid: entries[index].uid,
55
68
  content_type: contentType,
56
69
  locale,
57
70
  publish_details: entries[index].publish_details || [],
58
- });
71
+ };
72
+
73
+ if (variantsFlag) {
74
+ variants = await getVariantEntries(stack, contentType, entries, index, queryParams);
75
+ if (variants.length > 0) {
76
+ entry.variant_rules = {
77
+ publish_latest_base: false,
78
+ publish_latest_base_conditionally: true,
79
+ };
80
+ entry.variants = variants;
81
+ }
82
+ }
59
83
  }
84
+ bulkPublishSet.push(entry);
60
85
 
61
86
  if (bulkPublishSet.length === bulkPublishLimit) {
62
87
  await queue.Enqueue({
@@ -71,7 +96,7 @@ async function getEntries(
71
96
  }
72
97
 
73
98
  if (
74
- index === entriesResponse.items.length - 1 &&
99
+ index === entries.length - 1 &&
75
100
  bulkPublishSet.length <= bulkPublishLimit &&
76
101
  bulkPublishSet.length > 0
77
102
  ) {
@@ -84,7 +109,7 @@ async function getEntries(
84
109
  apiVersion,
85
110
  });
86
111
  bulkPublishSet = [];
87
- } // bulkPublish
112
+ }
88
113
  } else {
89
114
  await queue.Enqueue({
90
115
  content_type: contentType,
@@ -110,6 +135,8 @@ async function getEntries(
110
135
  environments,
111
136
  apiVersion,
112
137
  bulkPublishLimit,
138
+ variantsFlag,
139
+ entry_uid,
113
140
  skipCount,
114
141
  );
115
142
  return resolve();
@@ -118,6 +145,56 @@ async function getEntries(
118
145
  });
119
146
  }
120
147
 
148
+ async function getVariantEntries(stack, contentType, entries, index, queryParams, skip = 0) {
149
+ try {
150
+ let variantQueryParams = {
151
+ locale: queryParams.locale || 'en-us',
152
+ include_count: true,
153
+ skip: skip, // Adding skip parameter for pagination
154
+ limit: 100, // Set a limit to fetch up to 100 entries per request
155
+ include_publish_details: true,
156
+ };
157
+
158
+ const variantsEntriesResponse = await stack
159
+ .contentType(contentType)
160
+ .entry(entries[index].uid)
161
+ .variants()
162
+ .query(variantQueryParams)
163
+ .find();
164
+
165
+ // Map the response items to extract variant UIDs
166
+ const variants = variantsEntriesResponse.items.map((entry) => ({
167
+ uid: entry.variants._variant._uid,
168
+ }));
169
+
170
+ // Check if there are more entries to fetch
171
+ if (variantsEntriesResponse.items.length === variantQueryParams.limit) {
172
+ // Recursively fetch the next set of variants with updated skip value
173
+ const nextVariants = await getVariantEntries(
174
+ stack,
175
+ contentType,
176
+ entries,
177
+ index,
178
+ queryParams,
179
+ skip + variantQueryParams.limit,
180
+ );
181
+
182
+ // Ensure nextVariants is an array before concatenation
183
+ return Array.isArray(nextVariants) ? variants.concat(nextVariants) : variants;
184
+ }
185
+
186
+ return variants;
187
+ } catch (error) {
188
+ // Handle error message retrieval from different properties
189
+ const errorMessage =
190
+ error?.errorMessage ||
191
+ error?.message ||
192
+ error?.errors ||
193
+ 'Falied to fetch the variant entries, Please contact the admin for support.';
194
+ throw new Error(`Error fetching variants: ${errorMessage}`);
195
+ }
196
+ }
197
+
121
198
  async function getContentTypes(stack, skip = 0, contentTypes = []) {
122
199
  return new Promise((resolve, reject) => {
123
200
  skipCount = skip;
@@ -154,7 +231,17 @@ function setConfig(conf, bp) {
154
231
  }
155
232
 
156
233
  async function start(
157
- { retryFailed, bulkPublish, publishAllContentTypes, contentTypes, locales, environments, apiVersion },
234
+ {
235
+ retryFailed,
236
+ bulkPublish,
237
+ publishAllContentTypes,
238
+ contentTypes,
239
+ locales,
240
+ environments,
241
+ apiVersion,
242
+ includeVariants,
243
+ entryUid,
244
+ },
158
245
  stack,
159
246
  config,
160
247
  ) {
@@ -168,12 +255,16 @@ async function start(
168
255
  }
169
256
  process.exit(0);
170
257
  });
258
+
259
+ if (includeVariants) {
260
+ apiVersion = VARIANTS_PUBLISH_API_VERSION;
261
+ }
262
+
171
263
  if (retryFailed) {
172
264
  if (typeof retryFailed === 'string') {
173
265
  if (!validateFile(retryFailed, ['publish-entries', 'bulk-publish-entries'])) {
174
266
  return false;
175
267
  }
176
-
177
268
  bulkPublish = retryFailed.match(new RegExp('bulk')) ? true : false;
178
269
  setConfig(config, bulkPublish);
179
270
  if (bulkPublish) {
@@ -201,6 +292,8 @@ async function start(
201
292
  environments,
202
293
  apiVersion,
203
294
  bulkPublishLimit,
295
+ includeVariants,
296
+ entryUid,
204
297
  );
205
298
  /* eslint-enable no-await-in-loop */
206
299
  }
@@ -16,6 +16,7 @@ const { Command } = require('@contentstack/cli-command');
16
16
  const command = new Command();
17
17
  const { isEmpty } = require('../util');
18
18
  const { fetchBulkPublishLimit } = require('../util/common-utility');
19
+ const VARIANTS_UNPUBLISH_API_VERSION = '3.2';
19
20
 
20
21
  let bulkUnPublishSet = [];
21
22
  let bulkUnPulishAssetSet = [];
@@ -51,19 +52,26 @@ function getQueryParams(filter) {
51
52
  return queryString;
52
53
  }
53
54
 
54
- function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion, bulkPublishLimit) {
55
+ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion, bulkPublishLimit, variantsFlag = false) {
55
56
  return new Promise(async (resolve) => {
56
57
  for (let index = 0; index < items.length; index++) {
57
58
  changedFlag = true;
58
59
 
59
60
  if (bulkUnpublish) {
60
61
  if (bulkUnPublishSet.length < bulkPublishLimit && items[index].type === 'entry_published') {
61
- bulkUnPublishSet.push({
62
+ const entryData = {
62
63
  uid: items[index].data.uid,
63
64
  content_type: items[index].content_type_uid,
64
65
  locale: items[index].data.locale || 'en-us',
65
- publish_details: [items[index].data.publish_details] || [],
66
- });
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
+ }
67
75
  }
68
76
 
69
77
  if (bulkUnPulishAssetSet.length < bulkPublishLimit && items[index].type === 'asset_published') {
@@ -122,7 +130,7 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
122
130
  }
123
131
  } else {
124
132
  if (items[index].type === 'entry_published') {
125
- await entryQueue.Enqueue({
133
+ await entryQueue.Enqueue({
126
134
  content_type: items[index].content_type_uid,
127
135
  publish_details: [items[index].data.publish_details],
128
136
  environments: [environment],
@@ -130,6 +138,7 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
130
138
  locale: items[index].data.locale || 'en-us',
131
139
  Type: 'entry',
132
140
  stack: stack,
141
+ apiVersion,
133
142
  });
134
143
  }
135
144
  if (items[index].type === 'asset_published') {
@@ -157,6 +166,7 @@ async function getSyncEntries(
157
166
  deliveryToken,
158
167
  apiVersion,
159
168
  bulkPublishLimit,
169
+ variantsFlag,
160
170
  paginationToken = null,
161
171
  ) {
162
172
  return new Promise(async (resolve, reject) => {
@@ -205,10 +215,17 @@ async function getSyncEntries(
205
215
  }
206
216
 
207
217
  const entriesResponse = await Stack.sync(syncData);
208
-
209
218
  if (entriesResponse.items.length > 0) {
210
- await bulkAction(stack, entriesResponse.items, bulkUnpublish, environment, locale, apiVersion, bulkPublishLimit);
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);
211
227
  }
228
+
212
229
  if (entriesResponse.items.length === 0) {
213
230
  if (!changedFlag) console.log('No Entries/Assets Found published on specified environment');
214
231
  return resolve();
@@ -224,6 +241,7 @@ async function getSyncEntries(
224
241
  deliveryToken,
225
242
  apiVersion,
226
243
  bulkPublishLimit,
244
+ variantsFlag,
227
245
  null,
228
246
  );
229
247
  }, 3000);
@@ -232,6 +250,52 @@ async function getSyncEntries(
232
250
  }
233
251
  });
234
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
+ }
235
299
 
236
300
  async function start(
237
301
  {
@@ -245,6 +309,7 @@ async function start(
245
309
  onlyEntries,
246
310
  f_types,
247
311
  apiVersion,
312
+ includeVariants,
248
313
  },
249
314
  stack,
250
315
  config,
@@ -259,7 +324,9 @@ async function start(
259
324
  }
260
325
  process.exit(0);
261
326
  });
262
-
327
+ if (includeVariants) {
328
+ apiVersion = VARIANTS_UNPUBLISH_API_VERSION;
329
+ }
263
330
  if (retryFailed) {
264
331
  if (typeof retryFailed === 'string' && retryFailed.length > 0) {
265
332
  if (!validateFile(retryFailed, ['unpublish', 'bulk-unpublish'])) {
@@ -298,7 +365,7 @@ async function start(
298
365
  setConfig(config, bulkUnpublish);
299
366
  const queryParams = getQueryParams(filter);
300
367
  const bulkPublishLimit = fetchBulkPublishLimit(stack?.org_uid);
301
- await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion, bulkPublishLimit);
368
+ await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion, bulkPublishLimit, includeVariants);
302
369
  }
303
370
  }
304
371
 
@@ -309,4 +376,4 @@ module.exports = {
309
376
  setConfig,
310
377
  getQueryParams,
311
378
  start,
312
- };
379
+ };