Skip to content

Commit

Permalink
fixed look up replace assets in entries
Browse files Browse the repository at this point in the history
  • Loading branch information
shafeeqd959 committed Jul 15, 2022
1 parent bf8926f commit 2fe30ab
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 43 deletions.
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ echo 'Installing Contentstack CLI'
npm install || { echo -e '\e[31mFailed to install root dependencies.' && exit 1; }
npx lerna clean -y
npx lerna bootstrap
npx lerna run pack || { echo -e '\e[31mInstallable tarball creation failed.' && exit 1; }
npx lerna run prepack || { echo -e '\e[31mInstallable tarball creation failed.' && exit 1; }
echo 'Installtion, Done!!!'
cd packages/contentstack
./bin/run --help
4 changes: 2 additions & 2 deletions packages/contentstack-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-cm-export": "^1.0.0",
"@contentstack/cli-command": "^1.0.0",
"@contentstack/cli-cm-export": "^1.0.0",
"@contentstack/cli-utilities": "^1.0.0",
"@contentstack/management": "^1.3.0",
"@oclif/command": "^1.8.16",
Expand Down Expand Up @@ -69,4 +69,4 @@
"repositoryPrefix": "<%- repo %>/blob/v<%- version %>/packages/contentstack-import/<%- commandPath %>"
},
"repository": "https://github.com/contentstack/cli"
}
}
78 changes: 68 additions & 10 deletions packages/contentstack-import/src/lib/import/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,35 @@ importEntries.prototype = {
locale: lang,
error: error,
});
return rejectUpdatedUids(error);
if (error) {
addlogs(
config,
chalk.red(
'Entry Uid: ' +
entry.uid +
' of Content Type: ' +
ctUid +
' failed to update in locale: ' +
lang +
error.message,
),
'error',
);
addlogs(
config,
chalk.red(
'Entry Uid: ' +
entry.uid +
' of Content Type: ' +
ctUid +
' failed to update in locale: ' +
lang +
error.errorMessage,
),
'error',
);
}
return resolveUpdatedUids();
});
});
},
Expand All @@ -704,7 +732,19 @@ importEntries.prototype = {
.catch(function (error) {
// error while executing entry in batch
addlogs(config, chalk.red('Failed re-post entries at batch no: ' + (index + 1)), 'error');
throw error;
if (error) {
addlogs(
config,
chalk.red('Failed re-post entries at batch no: ' + (index + 1) + error.message),
'error',
);
addlogs(
config,
chalk.red('Failed re-post entries at batch no: ' + (index + 1) + error.errorMessage),
'error',
);
}
// throw error;
});
},
{
Expand All @@ -726,16 +766,34 @@ importEntries.prototype = {
// error while updating entries with references
addlogs(
config,
chalk.red(
"Failed while importing entries of Content Type: '" +
ctUid +
"' in language: '" +
lang +
"' successfully!",
),
chalk.red("Failed while importing entries of Content Type: '" + ctUid + "' in language: '" + lang),
'error',
);
throw error;
if (error) {
addlogs(
config,
chalk.red(
"Failed while importing entries of Content Type: '" +
ctUid +
"' in language: '" +
lang +
error.message,
),
'error',
);
addlogs(
config,
chalk.red(
"Failed while importing entries of Content Type: '" +
ctUid +
"' in language: '" +
lang +
error.errorMessage,
),
'error',
);
}
// throw error;
});
},
{
Expand Down
61 changes: 34 additions & 27 deletions packages/contentstack-import/src/lib/util/lookupReplaceAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
}
// added rich_text_type field check because some marketplace extensions also
// have data_type has json
if (schema[i].data_type === "json" && schema[i].field_metadata.rich_text_type) {
parent.push(schema[i].uid)
if (schema[i].data_type === 'json' && schema[i].field_metadata.rich_text_type) {
parent.push(schema[i].uid);
// findFileUrls(schema[i], entry, assetUrls)
findAssetIdsFromJsonRte(data.entry, data.content_type.schema)
findAssetIdsFromJsonRte(data.entry, data.content_type.schema);
// maybe only one of these checks would be enough
parent.pop()
parent.pop();
}
}
};
Expand All @@ -75,11 +75,11 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
case 'blocks': {
if (entry[ctSchema[i].uid]) {
if (ctSchema[i].multiple) {
entry[ctSchema[i].uid].forEach(e => {
let key = Object.keys(e).pop()
let subBlock = ctSchema[i].blocks.filter(e => e.uid === key).pop()
findAssetIdsFromJsonRte(e[key], subBlock.schema)
})
entry[ctSchema[i].uid].forEach((e) => {
let key = Object.keys(e).pop();
let subBlock = ctSchema[i].blocks.filter((e) => e.uid === key).pop();
findAssetIdsFromJsonRte(e[key], subBlock.schema);
});
}
}
break;
Expand All @@ -88,69 +88,69 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
case 'group': {
if (entry[ctSchema[i].uid]) {
if (ctSchema[i].multiple) {
entry[ctSchema[i].uid].forEach(e => {
findAssetIdsFromJsonRte(e, ctSchema[i].schema)
})
entry[ctSchema[i].uid].forEach((e) => {
findAssetIdsFromJsonRte(e, ctSchema[i].schema);
});
} else {
findAssetIdsFromJsonRte(entry[ctSchema[i].uid], ctSchema[i].schema)
findAssetIdsFromJsonRte(entry[ctSchema[i].uid], ctSchema[i].schema);
}
}
break;
}
case 'json': {
if (entry[ctSchema[i].uid] && ctSchema[i].field_metadata.rich_text_type) {
if (ctSchema[i].multiple) {
entry[ctSchema[i].uid].forEach(jsonRteData => {
gatherJsonRteAssetIds(jsonRteData)
})
entry[ctSchema[i].uid].forEach((jsonRteData) => {
gatherJsonRteAssetIds(jsonRteData);
});
} else {
gatherJsonRteAssetIds(entry[ctSchema[i].uid])
gatherJsonRteAssetIds(entry[ctSchema[i].uid]);
}
}
break;
}
}
}
}

function gatherJsonRteAssetIds(jsonRteData) {
jsonRteData.children.forEach(element => {
jsonRteData.children.forEach((element) => {
if (element.type) {
switch (element.type) {
case 'a':
case 'p': {
if (element.children && element.children.length > 0) {
gatherJsonRteAssetIds(element)
gatherJsonRteAssetIds(element);
}
break;
}
case 'reference': {
if (Object.keys(element.attrs).length > 0 && element.attrs.type === "asset") {
if (Object.keys(element.attrs).length > 0 && element.attrs.type === 'asset') {
if (assetUids.indexOf(element.attrs['asset-uid']) === -1) {
assetUids.push(element.attrs['asset-uid'])
assetUids.push(element.attrs['asset-uid']);
}
// assets references inserted as link inside entry reference inserted as link did not have asset-link property
// instead it had an 'href' property. I haven't seen 'asset-link' and 'href' together yet
// writing this condition assuming that this never occurs, need to confirm
// (element.attrs['asset-link']) ? assetUrls.push(element.attrs['asset-link']) : assetUrls.push(element.attrs['asset-link'])
if (element.attrs['asset-link']) {
if (assetUrls.indexOf(element.attrs['asset-link']) === -1) {
assetUrls.push(element.attrs['asset-link'])
assetUrls.push(element.attrs['asset-link']);
}
} else if (element.attrs['href']) {
if (assetUrls.indexOf(element.attrs['href']) === -1) {
assetUrls.push(element.attrs['href'])
assetUrls.push(element.attrs['href']);
}
}
}
if (element.children && element.children.length > 0) {
gatherJsonRteAssetIds(element)
gatherJsonRteAssetIds(element);
}
break;
}
}
}
})
});
}

find(data.content_type.schema, data.entry);
Expand All @@ -162,7 +162,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
assetUrls.forEach(function (assetUrl) {
var mappedAssetUrl = mappedAssetUrls[assetUrl];
if (typeof mappedAssetUrl !== 'undefined') {
entry = entry.replace(new RegExp(assetUrl, 'img'), mappedAssetUrl);
entry = entry.replace(new RegExp(escapeUrl(assetUrl), 'img'), mappedAssetUrl);
matchedUrls.push(mappedAssetUrl);
} else {
unmatchedUrls.push(assetUrl);
Expand Down Expand Up @@ -299,3 +299,10 @@ function updateFileFields(objekt, parent, pos, mappedAssetUids, matchedUids, unm
parent[pos] = _.compact(objekt);
}
}

function escapeUrl(assetUrl) {
if (typeof assetUrl !== 'string') {
throw new TypeError('url should be a string ' + assetUrl);
}
return assetUrl.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
}
6 changes: 3 additions & 3 deletions packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
"lint": "eslint src/**/*.ts"
},
"dependencies": {
"@contentstack/cli-utilities": "^1.0.0",
"@contentstack/cli-command": "^1.0.0",
"@contentstack/cli-auth": "^1.0.0",
"@contentstack/cli-cm-bootstrap": "^1.0.7",
"@contentstack/cli-cm-bulk-publish": "^1.0.0",
"@contentstack/cli-cm-clone": "^1.0.0",
"@contentstack/cli-cm-export": "^1.0.0",
"@contentstack/cli-cm-export-to-csv": "^1.0.0",
"@contentstack/cli-cm-import": "^1.0.0",
"@contentstack/cli-cm-migrate-rte": "^1.0.5",
"@contentstack/cli-cm-seed": "^1.0.11",
"@contentstack/cli-command": "^1.0.0",
"@contentstack/cli-cm-bootstrap": "^1.0.7",
"@contentstack/cli-config": "^1.0.0",
"@contentstack/cli-migration": "^1.0.0",
"@contentstack/cli-utilities": "^1.0.0",
"@contentstack/management": "^1.3.0",
"@oclif/command": "^1.8.16",
"@oclif/config": "^1.18.3",
Expand Down

0 comments on commit 2fe30ab

Please sign in to comment.