Skip to content

Commit

Permalink
Merge pull request #1474 from contentstack/fix/DX-888
Browse files Browse the repository at this point in the history
fixed the SRE issues
  • Loading branch information
cs-raj authored Aug 7, 2024
2 parents fb2617a + a5b13f1 commit e655c53
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 50 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

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

13 changes: 4 additions & 9 deletions packages/contentstack-audit/src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import memoize from 'lodash/memoize';
import { escapeRegExp, validateRegex } from '@contentstack/cli-utilities';

const errors = {};

Expand Down Expand Up @@ -47,7 +46,7 @@ const auditFixMsg = {
AUDIT_FIX_CMD_DESCRIPTION: 'Perform audits and fix possible errors in the exported Contentstack data.',
WF_FIX_MSG: 'Successfully removed the workflow {uid} named {name}.',
ENTRY_MANDATORY_FIELD_FIX: `Removing the publish details from the entry with UID '{uid}' in Locale '{locale}'...`,
ENTRY_SELECT_FIELD_FIX: `Adding the value '{value}' in the select field of entry UID '{uid}'...`
ENTRY_SELECT_FIELD_FIX: `Adding the value '{value}' in the select field of entry UID '{uid}'...`,
};

const messages: typeof errors &
Expand Down Expand Up @@ -76,13 +75,9 @@ function $t(msg: string, args: Record<string, string>): string {
if (!msg) return '';

for (const key of Object.keys(args)) {
const escapedKey = escapeRegExp(key);
const escapedKeyRegex = new RegExp(`{${escapedKey}}`, 'g');
let { status } = validateRegex(escapedKeyRegex)
if (status === 'safe') {
const sanitizedValue = args[key] ? escapeRegExp(args[key]) : '';
msg = msg.replace(escapedKeyRegex, sanitizedValue || escapedKey);
}
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const placeholder = `{${escapedKey}}`;
msg = msg.split(placeholder).join(args[key]);
}

return msg;
Expand Down
14 changes: 3 additions & 11 deletions packages/contentstack-import/src/utils/asset-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,7 @@ export const lookupAssets = function (
assetUrls.forEach(function (assetUrl: any) {
let mappedAssetUrl = mappedAssetUrls[assetUrl];
if (typeof mappedAssetUrl !== 'undefined') {
//NOTE - This code was added to resolve the SRE issue but once the code was merged Assets URLs in JSON RTE started breaking
// const sanitizedUrl = escapeRegExp(assetUrl).replace(/\.\./g, '\\$&');
// const escapedMappedUrl = escapeRegExp(mappedAssetUrl).replace(/\.\./g, '\\$&');
// entry = entry.replace(new RegExp(sanitizedUrl, 'img'), escapedMappedUrl);
entry = entry.replace(new RegExp(assetUrl, 'img'), mappedAssetUrl);
entry = entry.split(assetUrl).join(mappedAssetUrl);
matchedUrls.push(mappedAssetUrl);
} else {
unmatchedUrls.push(assetUrl);
Expand All @@ -266,12 +262,8 @@ export const lookupAssets = function (
let uid = mappedAssetUids[assetUid];
if (typeof uid !== 'undefined') {
const escapedAssetUid = assetUid.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(`\\b${escapedAssetUid}\\b`, 'img');
let { status } = validateRegex(new RegExp(regex, 'img'));
if (status === 'safe') {
entry = entry.replace(regex, uid);
matchedUids.push(assetUid);
}
entry = entry.split(escapedAssetUid).join(uid);
matchedUids.push(assetUid);
} else {
unmatchedUids.push(assetUid);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/contentstack-import/src/utils/entries-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export const lookupEntries = function (
} else {
const key = _parent[j];
if (Object.prototype.hasOwnProperty.call(_entry, key)) {
_entry = _entry[key];
const tempEntry = Object.create(null);
_.merge(tempEntry, _entry);
_entry = tempEntry[key];
let _keys = _.clone(_parent).splice(j + 1, len);
if (Array.isArray(_entry)) {
for (let i = 0, _i = _entry?.length; i < _i; i++) {
Expand Down Expand Up @@ -580,9 +582,8 @@ export const restoreJsonRteEntryRefs = (

function updateUids(str: string, match: string, uidMapper: Record<string, string>) {
const sanitizedMatch = escapeRegExp(match);
const regex = new RegExp(`\\b${sanitizedMatch}\\b`, 'g');
let { status } = validateRegex(regex);
if (status === 'safe') return str.replace(regex, (matchedString) => uidMapper[matchedString]);
const replacement = uidMapper[match] ?? sanitizedMatch;
return str.split(sanitizedMatch).join(replacement);
}

function setDirtyTrue(jsonRteChild: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ module.exports = async ({ migration, stackSDKInstance, managementAPIClient, conf
const modules = ['entries', 'assets', 'extensions', 'marketplace_apps'];

const readAllModulesUids = (filePath) => {
let uidMapping = {};
let uidMapping = Object.create(null);

modules.forEach((module) => {
const mappingFilePath = path.join(sanitizePath(filePath), 'mapper', sanitizePath(module), 'uid-mapping.json');
if (fs.existsSync(mappingFilePath)) {
const mappedIds = JSON.parse(fs.readFileSync(sanitizePath(mappingFilePath), 'utf-8'));

if (module === 'marketplace_apps') {
Object.values(mappedIds).forEach((ids) => Object.assign(uidMapping, ids));
Object.values(mappedIds).forEach((ids) => {
uidMapping = { ...uidMapping, ...sanitizeObject(ids) };
});
} else {
Object.assign(uidMapping, sanitizeObject(mappedIds));
uidMapping = { ...uidMapping, ...sanitizeObject(mappedIds) };
}
}
});
Expand All @@ -33,7 +35,7 @@ module.exports = async ({ migration, stackSDKInstance, managementAPIClient, conf
}
}
return sanitized;
}
};

const getEntries = async (ct) => {
try {
Expand Down Expand Up @@ -73,13 +75,10 @@ module.exports = async ({ migration, stackSDKInstance, managementAPIClient, conf
let oldUids = Object.keys(uidMapping);
matches.forEach((m) => {
if (oldUids.includes(m)) {
let regex = new RegExp(m, 'g');
let { status } = validateRegex(regex);
if (status === 'safe') {
stringifiedEntry = stringifiedEntry.replace(regex, uidMapping[m]);
console.log(chalk.green(`Replacing the UID '${m}' with '${uidMapping[m]}'...`));
isUpdated = true;
}
let sanitizedUid = m;
stringifiedEntry = stringifiedEntry.split(sanitizedUid).join(uisdMapping[sanitizedUid]);
console.log(chalk.green(`Replacing the UID '${m}' with '${uidMapping[m]}'...`));
isUpdated = true;
}
});
return { stringifiedEntry, isUpdated };
Expand Down
11 changes: 8 additions & 3 deletions packages/contentstack-migration/src/utils/modules.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const { execSync } = require('child_process');
const { execFileSync } = require('child_process');
const path = require('path');
const { sanitizePath } = require('@contentstack/cli-utilities');
const os = require('os');
Expand Down Expand Up @@ -70,8 +70,13 @@ function installDependencies(dependencies, directory) {

function executeShellCommand(command, directory = '') {
try {
execSync(command, { stdio: 'inherit', cwd: directory });
console.log(`The '${command}' command has been executed successfully.`);
if (command.startsWith('npm i')) {
const [cmd, ...args] = command.split(' ');
execFileSync(cmd, args, { stdio: 'inherit', cwd: directory });
console.log(`Command executed successfully: ${command}`);
} else {
console.log(`Command should only be 'npm i <package-name>'`);
}
} catch (error) {
console.error(`Command execution failed. Error: ${error.message}`);
}
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

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

0 comments on commit e655c53

Please sign in to comment.