Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ class PublishEntriesCommand extends Command {
entriesFlags['publish-all-content-types'] || entriesFlags.publishAllContentTypes || false;
entriesFlags.apiVersion = entriesFlags['api-version'] || '3';
entriesFlags.includeVariants = entriesFlags['include-variants'] || entriesFlags.includeVariants || false;
entriesFlags.publishWithoutBase = entriesFlags['publish-without-base'] || entriesFlags.publishWithoutBase || false;
entriesFlags.entryUid = entriesFlags['entry-uid'] || entriesFlags.entryUid;
if(entriesFlags.entryUid === undefined){

if (entriesFlags.entryUid === undefined) {
delete entriesFlags['entryUid'];
}
delete entriesFlags['api-version'];
Expand All @@ -33,6 +35,7 @@ class PublishEntriesCommand extends Command {
delete entriesFlags['publish-all-content-types'];
delete entriesFlags['include-variants'];
delete entriesFlags['entry-uid'];
delete entriesFlags['publish-without-base'];

let updatedFlags;
try {
Expand Down Expand Up @@ -64,7 +67,9 @@ class PublishEntriesCommand extends Command {
} else {
this.error('Please use `--alias` or `--stack-api-key` to proceed.', { exit: 2 });
}
updatedFlags.includeVariants = entriesFlags.includeVariants;
if (updatedFlags.publishWithoutBase && !updatedFlags.includeVariants) {
this.error('Please use `--include-variants` to proceed.', { exit: 2 });
}
updatedFlags.bulkPublish = updatedFlags.bulkPublish !== 'false';
stack = await getStack(config);
}
Expand Down Expand Up @@ -205,7 +210,7 @@ PublishEntriesCommand.flags = {
default: 'true',
}),
'api-version': flags.string({
description : "API Version to be used. Values [Default: 3, Nested Reference Publishing: 3.2].",
description: 'API Version to be used. Values [Default: 3, Nested Reference Publishing: 3.2].',
}),
'publish-all-content-types': flags.boolean({
description: '(optional) Publish all contenttypes (cannot be set when contentTypes flag is set)',
Expand Down Expand Up @@ -253,9 +258,13 @@ PublishEntriesCommand.flags = {
'delivery-token': flags.string({ description: 'Delivery token for source environment' }),
'source-env': flags.string({ description: 'Source environment' }),
'entry-uid': flags.string({ description: 'Entry Uid for publish all associated variant entries.' }),
'include-variants': flags.boolean({
'include-variants': flags.boolean({
default: false, // set the default value to false
description: 'Include Variants flag will publish all associated variant entries.'
description: 'Include Variants flag will publish all associated variant entries with base entry.',
}),
'publish-without-base': flags.boolean({
default: false,
description: 'Publish without base flag will publish all associated variant entries except base entry.',
}),
};

Expand Down Expand Up @@ -286,11 +295,14 @@ PublishEntriesCommand.examples = [
'',
'Using --entry-uid and --include-variants',
'csdx cm:entries:publish --content-types [CONTENT TYPE 1] [CONTENT TYPE 2] -e [ENVIRONMENT 1] [ENVIRONMENT 2] --locales [LOCALE 1] [LOCALE 2] --stack-api-key [STACK API KEY] --source-env [SOURCE ENVIRONMENT] --delivery-token [DELIVERY TOKEN] --entry-uid [ENTRY UID] [--include-variants]',
'',
'Using --include-variants and --publish-without-base',
'csdx cm:entries:publish --content-types [CONTENT TYPE 1] [CONTENT TYPE 2] -e [ENVIRONMENT 1] [ENVIRONMENT 2] --locales [LOCALE 1] [LOCALE 2] --stack-api-key [STACK API KEY] --source-env [SOURCE ENVIRONMENT] --delivery-token [DELIVERY TOKEN] [--include-variants][--publish-without-base]',
];

PublishEntriesCommand.aliases = ['cm:bulk-publish:entries'];

PublishEntriesCommand.usage =
'cm:entries:publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--publish-all-content-types] [--content-types <value>] [--locales <value>] [-e <value>] [-c <value>] [-y] [--branch <value>] [--delivery-token <value>] [--source-env <value>] [--entry-uid <value>] [--include-variants]';
'cm:entries:publish [-a <value>] [--retry-failed <value>] [--bulk-publish <value>] [--publish-all-content-types] [--content-types <value>] [--locales <value>] [-e <value>] [-c <value>] [-y] [--branch <value>] [--delivery-token <value>] [--source-env <value>] [--entry-uid <value>] [--include-variants][--publish-without-base]';

module.exports = PublishEntriesCommand;
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ class UnpublishCommand extends Command {
unpublishFlags.onlyAssets = false;
unpublishFlags.onlyEntries = true;
unpublishFlags.apiVersion = unpublishFlags['api-version'] || '3';
unpublishFlags.includeVariants = unpublishFlags['include-variants'];
unpublishFlags.includeVariants = unpublishFlags['include-variants'] || false;
delete unpublishFlags['api-version'];
delete unpublishFlags['retry-failed'];
delete unpublishFlags['bulk-unpublish'];
delete unpublishFlags['content-type'];
delete unpublishFlags['delivery-token'];
delete unpublishFlags['include-variants'];

let updatedFlags;
try {
Expand Down Expand Up @@ -60,7 +61,6 @@ class UnpublishCommand extends Command {
updatedFlags.deliveryToken = await cliux.prompt('Enter delivery token of your source environment');
}
updatedFlags.bulkUnpublish = updatedFlags.bulkUnpublish === 'false' ? false : true;
updatedFlags.includeVariantsFlag = updatedFlags.includeVariants;
stack = await getStack(config);
}
if (!updatedFlags.deliveryToken && updatedFlags.deliveryToken.length === 0) {
Expand Down
103 changes: 80 additions & 23 deletions packages/contentstack-bulk-publish/src/producer/publish-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ let allContentTypes = [];
let bulkPublishSet = [];
let filePath;

async function getEntries(stack, contentType, locale, bulkPublish, environments, apiVersion, variantsFlag = false, entry_uid = undefined, skip = 0) {
async function getEntries(
stack,
contentType,
locale,
bulkPublish,
environments,
apiVersion,
variantsFlag = false,
entry_uid = undefined,
publishWithoutBaseFlag = false,
skip = 0,
) {
return new Promise((resolve, reject) => {
skipCount = skip;

Expand Down Expand Up @@ -60,15 +71,22 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments,

if (variantsFlag) {
variants = await getVariantEntries(stack, contentType, entries, index, queryParams);
if(variants.length > 0){
entry.variant_rules = {
publish_latest_base: true,
publish_latest_base_conditionally: true
};
if (variants.length > 0) {
if (publishWithoutBaseFlag) {
entry.variant_rules = {
publish_latest_base: true,
publish_latest_base_conditionally: false,
};
} else {
entry.variant_rules = {
publish_latest_base: false,
publish_latest_base_conditionally: true,
};
}

entry.variants = variants;
}

}
}
}
bulkPublishSet.push(entry);

Expand All @@ -84,11 +102,7 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments,
bulkPublishSet = [];
}

if (
index === entries.length - 1 &&
bulkPublishSet.length <= 10 &&
bulkPublishSet.length > 0
) {
if (index === entries.length - 1 && bulkPublishSet.length <= 10 && bulkPublishSet.length > 0) {
await queue.Enqueue({
entries: bulkPublishSet,
locale,
Expand Down Expand Up @@ -116,7 +130,18 @@ async function getEntries(stack, contentType, locale, bulkPublish, environments,
bulkPublishSet = [];
return resolve();
}
await getEntries(stack, contentType, locale, bulkPublish, environments, apiVersion, variantsFlag, skipCount);
await getEntries(
stack,
contentType,
locale,
bulkPublish,
environments,
apiVersion,
variantsFlag,
entry_uid,
publishWithoutBaseFlag,
skipCount,
);
return resolve();
})
.catch((error) => reject(error));
Expand All @@ -128,8 +153,8 @@ async function getVariantEntries(stack, contentType, entries, index, queryParams
let variantQueryParams = {
locale: queryParams.locale || 'en-us',
include_count: true,
skip: skip, // Adding skip parameter for pagination
limit: 100, // Set a limit to fetch up to 100 entries per request
skip: skip, // Adding skip parameter for pagination
limit: 100, // Set a limit to fetch up to 100 entries per request
include_publish_details: true,
};

Expand All @@ -141,23 +166,34 @@ async function getVariantEntries(stack, contentType, entries, index, queryParams
.find();

// Map the response items to extract variant UIDs
const variants = variantsEntriesResponse.items.map(entry => ({
uid: entry.variants_uid,
const variants = variantsEntriesResponse.items.map((entry) => ({
uid: entry.variants._variant._uid,
}));

// Check if there are more entries to fetch
if (variantsEntriesResponse.items.length === variantQueryParams.limit) {
// Recursively fetch the next set of variants with updated skip value
const nextVariants = await getVariantEntries(stack, contentType, entries, index, queryParams, skip + variantQueryParams.limit);

const nextVariants = await getVariantEntries(
stack,
contentType,
entries,
index,
queryParams,
skip + variantQueryParams.limit,
);

// Ensure nextVariants is an array before concatenation
return Array.isArray(nextVariants) ? variants.concat(nextVariants) : variants;
}

return variants;
} catch (error) {
// Handle error message retrieval from different properties
const errorMessage = error?.errorMessage || error?.message || error?.errors || 'Falied to fetch the variant entries, Please contact the admin for support.';
const errorMessage =
error?.errorMessage ||
error?.message ||
error?.errors ||
'Falied to fetch the variant entries, Please contact the admin for support.';
throw new Error(`Error fetching variants: ${errorMessage}`);
}
}
Expand Down Expand Up @@ -198,7 +234,18 @@ function setConfig(conf, bp) {
}

async function start(
{ retryFailed, bulkPublish, publishAllContentTypes, contentTypes, locales, environments, apiVersion, includeVariants, entryUid },
{
retryFailed,
bulkPublish,
publishAllContentTypes,
contentTypes,
locales,
environments,
apiVersion,
includeVariants,
entryUid,
publishWithoutBase,
},
stack,
config,
) {
Expand Down Expand Up @@ -240,7 +287,17 @@ async function start(
for (let loc = 0; loc < locales.length; loc += 1) {
for (let i = 0; i < allContentTypes.length; i += 1) {
/* eslint-disable no-await-in-loop */
await getEntries(stack, allContentTypes[i].uid || allContentTypes[i], locales[loc], bulkPublish, environments, apiVersion, includeVariants, entryUid);
await getEntries(
stack,
allContentTypes[i].uid || allContentTypes[i],
locales[loc],
bulkPublish,
environments,
apiVersion,
includeVariants,
entryUid,
publishWithoutBase,
);
/* eslint-enable no-await-in-loop */
}
}
Expand Down
26 changes: 15 additions & 11 deletions packages/contentstack-bulk-publish/src/producer/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function getQueryParams(filter) {
return queryString;
}

function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion, variantsFlag) {
function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion, variantsFlag = false) {
return new Promise(async (resolve) => {
for (let index = 0; index < items.length; index++) {
changedFlag = true;
Expand All @@ -64,12 +64,14 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
locale: items[index].data.locale || 'en-us',
publish_details: items[index].data.publish_details || [],
};

if (variantsFlag) {
entryData.publish_with_base_entry = true;
entryData.variants = items[index].data.variants;

if (variantsFlag && Array.isArray(items[index].data.variants) && items[index].data.variants.length > 0) {
const entryWithVariants = { ...entryData, variants: items[index].data.variants };
bulkUnPublishSet.push(entryWithVariants);
bulkUnPublishSet.push(entryData);
} else {
bulkUnPublishSet.push(entryData);
}
bulkUnPublishSet.push(entryData);
}

if (bulkUnPulishAssetSet.length < 10 && items[index].type === 'asset_published') {
Expand Down Expand Up @@ -128,14 +130,15 @@ function bulkAction(stack, items, bulkUnpublish, environment, locale, apiVersion
}
} else {
if (items[index].type === 'entry_published') {
await entryQueue.Enqueue({
await entryQueue.Enqueue({
content_type: items[index].content_type_uid,
publish_details: [items[index].data.publish_details],
environments: [environment],
entryUid: items[index].data.uid,
locale: items[index].data.locale || 'en-us',
Type: 'entry',
stack: stack,
apiVersion,
});
}
if (items[index].type === 'asset_published') {
Expand Down Expand Up @@ -233,6 +236,7 @@ async function getSyncEntries(
environment,
deliveryToken,
apiVersion,
variantsFlag,
null,
);
}, 3000);
Expand Down Expand Up @@ -269,7 +273,7 @@ async function getVariantEntries(stack, contentType, entries, queryParams, skip

// Map the response items to extract variant UIDs
const variants = variantsEntriesResponse.items.map(entry => ({
uid: entry.variants_uid,
uid: entry.variants._variant._uid,
}));

// Check if there are more entries to fetch
Expand Down Expand Up @@ -301,7 +305,7 @@ async function start(
onlyEntries,
f_types,
apiVersion,
includeVariantsFlag,
includeVariants,
},
stack,
config,
Expand All @@ -316,7 +320,7 @@ async function start(
}
process.exit(0);
});
if (includeVariantsFlag) {
if (includeVariants) {
apiVersion = VARIANTS_UNPUBLISH_API_VERSION;
}
if (retryFailed) {
Expand Down Expand Up @@ -356,7 +360,7 @@ async function start(
}
setConfig(config, bulkUnpublish);
const queryParams = getQueryParams(filter);
await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion, includeVariantsFlag);
await getSyncEntries(stack, config, locale, queryParams, bulkUnpublish, environment, deliveryToken, apiVersion, includeVariants);
}
}

Expand Down