@contentstack/cli-cm-bulk-publish 1.6.0 → 1.7.1

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) => {
@@ -203,12 +213,22 @@ async function getSyncEntries(
203
213
  if (queryParamsObj.type) {
204
214
  syncData['type'] = queryParamsObj.type;
205
215
  }
216
+ if(queryParamsObj.content_type_uid) {
217
+ syncData['content_type_uid'] = queryParamsObj.content_type_uid;
218
+ }
206
219
 
207
220
  const entriesResponse = await Stack.sync(syncData);
208
-
209
221
  if (entriesResponse.items.length > 0) {
210
- await bulkAction(stack, entriesResponse.items, bulkUnpublish, environment, locale, apiVersion, bulkPublishLimit);
222
+ if (variantsFlag) {
223
+ queryParamsObj.apiVersion = VARIANTS_UNPUBLISH_API_VERSION;
224
+ const itemsWithVariants = await attachVariantsToItems(stack, entriesResponse.items, queryParamsObj);
225
+ // Call bulkAction for entries with variants
226
+ await bulkAction(stack, itemsWithVariants, bulkUnpublish, environment, locale, apiVersion, bulkPublishLimit, variantsFlag);
227
+ }
228
+ // Call bulkAction for entries without variants
229
+ await bulkAction(stack, entriesResponse.items, bulkUnpublish, environment, locale, apiVersion, bulkPublishLimit, false);
211
230
  }
231
+
212
232
  if (entriesResponse.items.length === 0) {
213
233
  if (!changedFlag) console.log('No Entries/Assets Found published on specified environment');
214
234
  return resolve();
@@ -224,6 +244,7 @@ async function getSyncEntries(
224
244
  deliveryToken,
225
245
  apiVersion,
226
246
  bulkPublishLimit,
247
+ variantsFlag,
227
248
  null,
228
249
  );
229
250
  }, 3000);
@@ -232,6 +253,52 @@ async function getSyncEntries(
232
253
  }
233
254
  });
234
255
  }
256
+ async function attachVariantsToItems(stack, items, queryParams) {
257
+ for (const item of items) {
258
+ const { content_type_uid, data } = item;
259
+ const variantEntries = await getVariantEntries(stack, content_type_uid, item, queryParams); // Fetch the variants using fetchVariants method
260
+ item.data.variants = variantEntries; // Attach the fetched variants to the data object in the item
261
+ }
262
+ return items;
263
+ }
264
+
265
+ async function getVariantEntries(stack, contentType, entries, queryParams, skip = 0) {
266
+ try {
267
+ let variantQueryParams = {
268
+ locale: queryParams.locale || 'en-us',
269
+ include_count: true,
270
+ skip: skip, // Adding skip parameter for pagination
271
+ limit: 100, // Set a limit to fetch up to 100 entries per request
272
+ };
273
+
274
+ const variantsEntriesResponse = await stack
275
+ .contentType(contentType)
276
+ .entry(entries.data.uid)
277
+ .variants()
278
+ .query(variantQueryParams)
279
+ .find();
280
+
281
+ // Map the response items to extract variant UIDs
282
+ const variants = variantsEntriesResponse.items.map(entry => ({
283
+ uid: entry.variants._variant._uid,
284
+ }));
285
+
286
+ // Check if there are more entries to fetch
287
+ if (variantsEntriesResponse.items.length === variantQueryParams.limit) {
288
+ // Recursively fetch the next set of variants with updated skip value
289
+ const nextVariants = await getVariantEntries(stack, contentType, entries, queryParams, skip + variantQueryParams.limit);
290
+
291
+ // Ensure nextVariants is an array before concatenation
292
+ return Array.isArray(nextVariants) ? variants.concat(nextVariants) : variants;
293
+ }
294
+
295
+ return variants;
296
+ } catch (error) {
297
+ // Handle error message retrieval from different properties
298
+ const errorMessage = error?.errorMessage || error?.message || error?.errors || 'Falied to fetch the variant entries, Please contact the admin for support.';
299
+ throw new Error(`Error fetching variants: ${errorMessage}`);
300
+ }
301
+ }
235
302
 
236
303
  async function start(
237
304
  {
@@ -245,6 +312,7 @@ async function start(
245
312
  onlyEntries,
246
313
  f_types,
247
314
  apiVersion,
315
+ includeVariants,
248
316
  },
249
317
  stack,
250
318
  config,
@@ -259,7 +327,9 @@ async function start(
259
327
  }
260
328
  process.exit(0);
261
329
  });
262
-
330
+ if (includeVariants) {
331
+ apiVersion = VARIANTS_UNPUBLISH_API_VERSION;
332
+ }
263
333
  if (retryFailed) {
264
334
  if (typeof retryFailed === 'string' && retryFailed.length > 0) {
265
335
  if (!validateFile(retryFailed, ['unpublish', 'bulk-unpublish'])) {
@@ -298,7 +368,7 @@ async function start(
298
368
  setConfig(config, bulkUnpublish);
299
369
  const queryParams = getQueryParams(filter);
300
370
  const bulkPublishLimit = fetchBulkPublishLimit(stack?.org_uid);
301
- await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion, bulkPublishLimit);
371
+ await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion, bulkPublishLimit, includeVariants);
302
372
  }
303
373
  }
304
374
 
@@ -309,4 +379,4 @@ module.exports = {
309
379
  setConfig,
310
380
  getQueryParams,
311
381
  start,
312
- };
382
+ };