Skip to content

Commit e655c53

Browse files
authored
Merge pull request #1474 from contentstack/fix/DX-888
fixed the SRE issues
2 parents fb2617a + a5b13f1 commit e655c53

File tree

7 files changed

+42
-50
lines changed

7 files changed

+42
-50
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contentstack-audit/src/messages/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import memoize from 'lodash/memoize';
2-
import { escapeRegExp, validateRegex } from '@contentstack/cli-utilities';
32

43
const errors = {};
54

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

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

7877
for (const key of Object.keys(args)) {
79-
const escapedKey = escapeRegExp(key);
80-
const escapedKeyRegex = new RegExp(`{${escapedKey}}`, 'g');
81-
let { status } = validateRegex(escapedKeyRegex)
82-
if (status === 'safe') {
83-
const sanitizedValue = args[key] ? escapeRegExp(args[key]) : '';
84-
msg = msg.replace(escapedKeyRegex, sanitizedValue || escapedKey);
85-
}
78+
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
79+
const placeholder = `{${escapedKey}}`;
80+
msg = msg.split(placeholder).join(args[key]);
8681
}
8782

8883
return msg;

packages/contentstack-import/src/utils/asset-helper.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,7 @@ export const lookupAssets = function (
251251
assetUrls.forEach(function (assetUrl: any) {
252252
let mappedAssetUrl = mappedAssetUrls[assetUrl];
253253
if (typeof mappedAssetUrl !== 'undefined') {
254-
//NOTE - This code was added to resolve the SRE issue but once the code was merged Assets URLs in JSON RTE started breaking
255-
// const sanitizedUrl = escapeRegExp(assetUrl).replace(/\.\./g, '\\$&');
256-
// const escapedMappedUrl = escapeRegExp(mappedAssetUrl).replace(/\.\./g, '\\$&');
257-
// entry = entry.replace(new RegExp(sanitizedUrl, 'img'), escapedMappedUrl);
258-
entry = entry.replace(new RegExp(assetUrl, 'img'), mappedAssetUrl);
254+
entry = entry.split(assetUrl).join(mappedAssetUrl);
259255
matchedUrls.push(mappedAssetUrl);
260256
} else {
261257
unmatchedUrls.push(assetUrl);
@@ -266,12 +262,8 @@ export const lookupAssets = function (
266262
let uid = mappedAssetUids[assetUid];
267263
if (typeof uid !== 'undefined') {
268264
const escapedAssetUid = assetUid.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
269-
const regex = new RegExp(`\\b${escapedAssetUid}\\b`, 'img');
270-
let { status } = validateRegex(new RegExp(regex, 'img'));
271-
if (status === 'safe') {
272-
entry = entry.replace(regex, uid);
273-
matchedUids.push(assetUid);
274-
}
265+
entry = entry.split(escapedAssetUid).join(uid);
266+
matchedUids.push(assetUid);
275267
} else {
276268
unmatchedUids.push(assetUid);
277269
}

packages/contentstack-import/src/utils/entries-helper.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ export const lookupEntries = function (
8989
} else {
9090
const key = _parent[j];
9191
if (Object.prototype.hasOwnProperty.call(_entry, key)) {
92-
_entry = _entry[key];
92+
const tempEntry = Object.create(null);
93+
_.merge(tempEntry, _entry);
94+
_entry = tempEntry[key];
9395
let _keys = _.clone(_parent).splice(j + 1, len);
9496
if (Array.isArray(_entry)) {
9597
for (let i = 0, _i = _entry?.length; i < _i; i++) {
@@ -580,9 +582,8 @@ export const restoreJsonRteEntryRefs = (
580582

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

588589
function setDirtyTrue(jsonRteChild: any) {

packages/contentstack-migration/examples/05-Update-reference-entry-from-mapper.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ module.exports = async ({ migration, stackSDKInstance, managementAPIClient, conf
77
const modules = ['entries', 'assets', 'extensions', 'marketplace_apps'];
88

99
const readAllModulesUids = (filePath) => {
10-
let uidMapping = {};
10+
let uidMapping = Object.create(null);
1111

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

1717
if (module === 'marketplace_apps') {
18-
Object.values(mappedIds).forEach((ids) => Object.assign(uidMapping, ids));
18+
Object.values(mappedIds).forEach((ids) => {
19+
uidMapping = { ...uidMapping, ...sanitizeObject(ids) };
20+
});
1921
} else {
20-
Object.assign(uidMapping, sanitizeObject(mappedIds));
22+
uidMapping = { ...uidMapping, ...sanitizeObject(mappedIds) };
2123
}
2224
}
2325
});
@@ -33,7 +35,7 @@ module.exports = async ({ migration, stackSDKInstance, managementAPIClient, conf
3335
}
3436
}
3537
return sanitized;
36-
}
38+
};
3739

3840
const getEntries = async (ct) => {
3941
try {
@@ -73,13 +75,10 @@ module.exports = async ({ migration, stackSDKInstance, managementAPIClient, conf
7375
let oldUids = Object.keys(uidMapping);
7476
matches.forEach((m) => {
7577
if (oldUids.includes(m)) {
76-
let regex = new RegExp(m, 'g');
77-
let { status } = validateRegex(regex);
78-
if (status === 'safe') {
79-
stringifiedEntry = stringifiedEntry.replace(regex, uidMapping[m]);
80-
console.log(chalk.green(`Replacing the UID '${m}' with '${uidMapping[m]}'...`));
81-
isUpdated = true;
82-
}
78+
let sanitizedUid = m;
79+
stringifiedEntry = stringifiedEntry.split(sanitizedUid).join(uisdMapping[sanitizedUid]);
80+
console.log(chalk.green(`Replacing the UID '${m}' with '${uidMapping[m]}'...`));
81+
isUpdated = true;
8382
}
8483
});
8584
return { stringifiedEntry, isUpdated };

packages/contentstack-migration/src/utils/modules.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fs = require('fs');
2-
const { execSync } = require('child_process');
2+
const { execFileSync } = require('child_process');
33
const path = require('path');
44
const { sanitizePath } = require('@contentstack/cli-utilities');
55
const os = require('os');
@@ -70,8 +70,13 @@ function installDependencies(dependencies, directory) {
7070

7171
function executeShellCommand(command, directory = '') {
7272
try {
73-
execSync(command, { stdio: 'inherit', cwd: directory });
74-
console.log(`The '${command}' command has been executed successfully.`);
73+
if (command.startsWith('npm i')) {
74+
const [cmd, ...args] = command.split(' ');
75+
execFileSync(cmd, args, { stdio: 'inherit', cwd: directory });
76+
console.log(`Command executed successfully: ${command}`);
77+
} else {
78+
console.log(`Command should only be 'npm i <package-name>'`);
79+
}
7580
} catch (error) {
7681
console.error(`Command execution failed. Error: ${error.message}`);
7782
}

pnpm-lock.yaml

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)