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
310 changes: 155 additions & 155 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/contentstack-bulk-publish/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-bulk-publish",
"description": "Contentstack CLI plugin for bulk publish actions",
"version": "1.10.3",
"version": "1.10.4",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
13 changes: 8 additions & 5 deletions packages/contentstack-bulk-publish/src/consumer/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ function removePublishDetails(elements) {
function displayEntriesDetails(sanitizedData, action, mapping = []) {
if (action === 'bulk_publish') {
sanitizedData.forEach((entry) => {
entry?.publish_details.forEach((pd) => {
if (Object.keys(mapping).includes(pd.environment)) {
if (Array.isArray(entry?.publish_details) && entry.publish_details.length > 0) {
const matchingPublishDetails = entry.publish_details.filter((pd) =>
Object.keys(mapping).includes(pd.environment)
);
if (matchingPublishDetails.length > 0) {
const pd = matchingPublishDetails[0];
console.log(
chalk.green(
`Entry UID: '${entry.uid}', Content Type: '${entry.content_type}', Locale: '${entry.locale}', Version: '${pd.version}', Environment: '${pd.environment}'`,
),
)
);
}
});
if(!Array.isArray(entry.publish_details)){
} else if (!Array.isArray(entry.publish_details)) {
console.log(chalk.green(`Entry UID: '${entry.uid}', Content Type: '${entry.content_type}', Locale: '${entry.locale}'`));
}
});
Expand Down
153 changes: 149 additions & 4 deletions packages/contentstack-bulk-publish/src/producer/add-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,124 @@ function removeUnwanted(entry, unwantedkeys) {
return entry;
}

function isLinkObject(obj, keyName) {
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
return false;
}

const linkKeyNames = ['link', 'card_link'];
if (linkKeyNames.includes(keyName)) {
return true;
}

const hasTitle = 'title' in obj && obj.title !== undefined;
const hasUrl = 'url' in obj && obj.url !== undefined;
const hasHref = 'href' in obj && obj.href !== undefined;

return hasTitle && (hasUrl || hasHref);
}

function ensureHrefIsString(linkObj) {
if (linkObj.href === undefined || linkObj.href === null) {
linkObj.href = '';
} else if (typeof linkObj.href !== 'string') {
linkObj.href = String(linkObj.href);
}
}

function isValidJsonRte(obj) {
return obj !== null &&
typeof obj === 'object' &&
!Array.isArray(obj) &&
typeof obj.type === 'string' &&
obj.type !== '';
}

function cleanJsonFields(obj) {
if (obj === null || obj === undefined || typeof obj !== 'object') {
return obj;
}

if (Array.isArray(obj)) {
return obj.map((item) => cleanJsonFields(item));
}

const cleaned = {};
for (const key in obj) {
if (!obj.hasOwnProperty(key)) {
continue;
}
let value = obj[key];
const isJsonField = key.endsWith('_rte') || key === 'json_rte';
const isAccessibilityField = key.endsWith('_accessibility') || key === 'image_preset_accessibility';

if (isJsonField) {
if (value === '' || value === null || value === undefined) {
continue;
}
if (typeof value === 'object' && !Array.isArray(value)) {
const keyCount = Object.keys(value).length;
if (keyCount === 0) {
continue;
}
if (!isValidJsonRte(value)) {
continue;
}
cleaned[key] = value;
} else {
continue;
}
} else if (isAccessibilityField && value === '') {
cleaned[key] = {};
} else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
value = cleanJsonFields(value);
if (value !== null && typeof value === 'object') {
cleaned[key] = value;
}
} else {
cleaned[key] = value;
}
}
return cleaned;
}

function convertUrlToHref(obj) {
if (obj === null || obj === undefined) {
return obj;
}

if (Array.isArray(obj)) {
return obj.map((item) => convertUrlToHref(item));
}

if (typeof obj === 'object') {
const converted = {};
for (const key in obj) {
const value = obj[key];

if (isLinkObject(value, key)) {
converted[key] = { ...value };
if (converted[key].url !== undefined && converted[key].href === undefined) {
if (typeof converted[key].url === 'string') {
converted[key].href = converted[key].url;
} else if (converted[key].url === null || converted[key].url === undefined) {
converted[key].href = '';
} else {
converted[key].href = String(converted[key].url);
}
delete converted[key].url;
}
ensureHrefIsString(converted[key]);
} else {
converted[key] = convertUrlToHref(value);
}
}
return converted;
}

return obj;
}

function fileFields(entry, uid, multiple) {
if (entry[uid]) {
if (typeof entry[uid] === 'object' || Array.isArray(entry[uid])) {
Expand Down Expand Up @@ -106,6 +224,11 @@ function addFields(contentType, entry) {
}
} else if (schema.enum) {
entry[schema.uid] = null;
} else if (schema.data_type === 'json') {
const isJsonRteField = schema.uid && (schema.uid.endsWith('_rte') || schema.uid === 'json_rte');
if (!isJsonRteField) {
entry[schema.uid] = {};
}
} else if (Object.prototype.hasOwnProperty.call(defaults, schema.data_type)) {
entry[schema.uid] = defaults[schema.data_type];
} else {
Expand All @@ -126,19 +249,31 @@ function addFields(contentType, entry) {

if (schema.data_type === 'group' && !schema.multiple) {
addFields(schema.schema, entry[schema.uid]);
if (entry[schema.uid]) {
entry[schema.uid] = convertUrlToHref(entry[schema.uid]);
}
}
if (schema.data_type === 'group' && schema.multiple) {
entry[schema.uid].forEach((field) => {
addFields(schema.schema, field);
});
if (entry[schema.uid]) {
entry[schema.uid] = convertUrlToHref(entry[schema.uid]);
}
}
if (schema.data_type === 'global_field' && !schema.multiple) {
addFields(schema.schema, entry[schema.uid]);
if (entry[schema.uid]) {
entry[schema.uid] = convertUrlToHref(entry[schema.uid]);
}
}
if (schema.data_type === 'global_field' && schema.multiple) {
entry[schema.uid].forEach((field) => {
addFields(schema.schema, field);
});
if (entry[schema.uid]) {
entry[schema.uid] = convertUrlToHref(entry[schema.uid]);
}
}
if (schema.data_type === 'blocks') {
if (!entry[schema.uid] && !Array.isArray(entry[schema.uid])) {
Expand All @@ -156,6 +291,9 @@ function addFields(contentType, entry) {
if (filterBlockFields.length > 0) {
filterBlockFields.forEach((bfield) => {
addFields(block.schema, bfield[block.uid]);
if (bfield[block.uid]) {
bfield[block.uid] = convertUrlToHref(bfield[block.uid]);
}
});
} else {
entry[schema.uid].push({ [block.uid]: {} });
Expand All @@ -169,6 +307,9 @@ function addFields(contentType, entry) {
if (filterBlockFields.length > 0) {
filterBlockFields.forEach((bfield) => {
addFields(block.schema, bfield[block.uid]);
if (bfield[block.uid]) {
bfield[block.uid] = convertUrlToHref(bfield[block.uid]);
}
});
}
}
Expand Down Expand Up @@ -221,8 +362,14 @@ async function getEntries(
for (let index = 0; index < entriesResponse.items.length; index++) {
let updatedEntry = addFields(schema, entries[index]);
if (updatedEntry.changedFlag || forceUpdate) {
updatedEntry = removeUnwanted(entries[index], deleteFields);
const flag = await updateEntry(updatedEntry, locale);
let entryData = JSON.parse(JSON.stringify(updatedEntry.entry));
entryData = removeUnwanted(entryData, deleteFields);
entryData = cleanJsonFields(entryData);
entryData = convertUrlToHref(entryData);
entryData = cleanJsonFields(entryData);
const entry = stack.contentType(contentType).entry(entries[index].uid);
Object.assign(entry, entryData);
const flag = await updateEntry(entry, locale);
if (flag) {
if (bulkPublish) {
if (bulkPublishSet.length < bulkPublishLimit) {
Expand Down Expand Up @@ -353,8 +500,6 @@ async function start(
}
}

// start()

module.exports = {
start,
getContentTypeSchema,
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@contentstack/cli-auth": "~1.6.2",
"@contentstack/cli-cm-bootstrap": "~1.17.1",
"@contentstack/cli-cm-branches": "~1.6.1",
"@contentstack/cli-cm-bulk-publish": "~1.10.3",
"@contentstack/cli-cm-bulk-publish": "~1.10.4",
"@contentstack/cli-cm-clone": "~1.18.0",
"@contentstack/cli-cm-export-to-csv": "~1.10.1",
"@contentstack/cli-cm-import-setup": "~1.7.1",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading