Skip to content

Commit

Permalink
fix: logger, custom attribute ref & error
Browse files Browse the repository at this point in the history
  • Loading branch information
aman19K committed Sep 27, 2024
1 parent 5a2e734 commit 2a292a8
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export default class ExportMarketplaceApps {
*/
async getAppConfigurations(index: number, appInstallation: any) {
const appName = appInstallation?.manifest?.name;
log(this.exportConfig, `Exporting ${appName} app and it's config.`, 'info');
const appUid = appInstallation?.manifest?.uid;
const app = appName || appUid;
log(this.exportConfig, `Exporting ${app} app and it's config.`, 'info');

await this.appSdk
.marketplace(this.exportConfig.org_uid)
Expand All @@ -158,17 +160,17 @@ export default class ExportMarketplaceApps {

if (!isEmpty(data?.server_configuration)) {
this.installedApps[index]['server_configuration'] = this.nodeCrypto.encrypt(data.server_configuration);
log(this.exportConfig, `Exported ${appName} app and it's config.`, 'success');
log(this.exportConfig, `Exported ${app} app and it's config.`, 'success');
} else {
log(this.exportConfig, `Exported ${appName} app`, 'success');
log(this.exportConfig, `Exported ${app} app`, 'success');
}
} else if (error) {
log(this.exportConfig, `Error on exporting ${appName} app and it's config.`, 'error');
log(this.exportConfig, `Error on exporting ${app} app and it's config.`, 'error');
log(this.exportConfig, error, 'error');
}
})
.catch((error: any) => {
log(this.exportConfig, `Failed to export ${appName} app config ${formatError(error)}`, 'error');
log(this.exportConfig, `Failed to export ${app} app config ${formatError(error)}`, 'error');
log(this.exportConfig, error, 'error');
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AnyProperty,
} from '@contentstack/cli-variants';

import { log, formatError } from '../../utils';
import { log } from '../../utils';
import { ModuleClassParams, ExportConfig } from '../../types';

export default class ExportPersonalize {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ export default class ImportMarketplaceApps {
);

if (installation.installation_uid) {
const appName = this.appNameMapping[app.manifest.name] ?? app.manifest.name;
const appName = this.appNameMapping[app.manifest.name] || app.manifest.name || app.manifest.uid;
log(this.importConfig, `${appName} app installed successfully.!`, 'success');
await makeRedirectUrlCall(installation, appName, this.importConfig);
this.installationUidMapping[app.uid] = installation.installation_uid;
Expand All @@ -448,7 +448,8 @@ export default class ImportMarketplaceApps {
await confirmToCloseProcess(installation, this.importConfig);
}
} else if (!isEmpty(configuration) || !isEmpty(server_configuration)) {
log(this.importConfig, `${app.manifest.name} is already installed`, 'success');
const appName = app.manifest.name || app.manifest.uid;
log(this.importConfig, `${appName} is already installed`, 'success');
updateParam = await ifAppAlreadyExist(app, currentStackApp, this.importConfig);
}

Expand Down Expand Up @@ -481,7 +482,8 @@ export default class ImportMarketplaceApps {
trace(data, 'error', true);
log(this.importConfig, formatError(data.message), 'success');
} else {
log(this.importConfig, `${app.manifest.name} app config updated successfully.!`, 'success');
const appName = app.manifest.name || app.manifest.uid;
log(this.importConfig, `${appName} app config updated successfully.!`, 'success');
}
})
.catch((error: any) => {
Expand Down
16 changes: 11 additions & 5 deletions packages/contentstack-import/src/utils/entries-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,17 @@ export const restoreJsonRteEntryRefs = (
});
} else {
entry[element.uid] = restoreReferenceInJsonRTE(sourceStackEntry[element.uid], uidMapper);
entry[element.uid].children = entry[element.uid].children.map((child: any) => {
child = setDirtyTrue(child);
child = resolveAssetRefsInEntryRefsForJsonRte(child, mappedAssetUids, mappedAssetUrls);
return child;
});
if (entry[element.uid].children && entry[element.uid].children.length > 0) {
entry[element.uid].children = entry[element.uid].children.map((child: any) => {
child = setDirtyTrue(child);
child = resolveAssetRefsInEntryRefsForJsonRte(child, mappedAssetUids, mappedAssetUrls);
return child;
});
} else {
entry[element.uid].children = [
{ type: 'p', attrs: { style: {}, 'redactor-attributes': {}, dir: 'ltr' } },
];
}
}
}
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/import/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class Project extends PersonalizationAdapter<ImportConfig> {
description: project.description,
connectedStackApiKey: this.config.apiKey,
}).catch(async (error) => {
if (error === 'personalization.PROJECTS.DUPLICATE_NAME' || error === 'personalize.PROJECTS.DUPLICATE_NAME') {
if (error.includes('personalization.PROJECTS.DUPLICATE_NAME') || error.includes('personalize.PROJECTS.DUPLICATE_NAME')) {
const projectName = await askProjectName('Copy Of ' + (newName || project.name));
return await createProject(projectName);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-variants/src/import/variant-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
*/
async publishVariantEntries(batch: VariantEntryStruct[], entryUid: string, content_type: string) {
const allPromise = [];
log(this.config, `Publishing variant entries for entry uid '${entryUid}' of Content Type '${content_type}'`, 'info');
for (let [, variantEntry] of entries(batch)) {
const variantEntryUID = variantEntry.uid;
const oldVariantUid = variantEntry._variant._uid || '';
Expand Down Expand Up @@ -446,11 +447,9 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
entry: {
environments,
locales,
publish_with_base_entry: false,
variants: [{ uid: newVariantUid, version: 1 }],
},
locale: variantEntry.locale,
version: 1,
};

const promise = this.variantInstance.publishVariantEntry(
Expand All @@ -470,6 +469,7 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Imp
allPromise.push(promise);
}
await Promise.allSettled(allPromise);
log(this.config, `Published variant entries for entry uid '${entryUid}' of Content Type '${content_type}'`, 'info');
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/contentstack-variants/src/types/variant-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export type PublishVariantEntryDto = {
entry: {
environments: string[];
locales: string[];
publish_with_base_entry: boolean;
variants: {
uid: string;
version?: number;
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-variants/src/utils/attributes-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const lookUpAttributes = (attributeRules: Record<string, any>[], attribut
// Check if attribute reference exists in attributesUid
const attributeRef = rule.attribute?.ref;
const attributeType = rule.attribute['__type'];
// check if type is UserAttributeReference
if (attributeType === 'UserAttributeReference') {
// check if type is CustomAttributeReference
if (attributeType === 'CustomAttributeReference') {
if (attributeRef && attributesUid.hasOwnProperty(attributeRef) && attributesUid[attributeRef]) {
rule.attribute.ref = attributesUid[attributeRef];
} else {
Expand Down
12 changes: 6 additions & 6 deletions packages/contentstack-variants/src/utils/error-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ export function formatErrors(errors: any): string {
for (const errorKey in errors) {
const errorValue = errors[errorKey];
if (Array.isArray(errorValue)) {
errorMessages.push(...errorValue.map((error: any) => formatError(error)));
errorMessages.push(...errorValue.map((error: any) => formatError(errorKey, error)));
} else {
errorMessages.push(formatError(errorValue));
errorMessages.push(formatError(errorKey, errorValue));
}
}

return errorMessages.join(' ');
}

function formatError(error: any): string {
if (typeof error === 'object') {
return Object.values(error).join(' ');
function formatError(errorKey: string, error: any): string {
if (typeof error === 'object' && error !== null) {
return `${errorKey}: ${Object.values(error).join(' ')}`;
}
return error;
return `${errorKey}: ${error}`;
}
9 changes: 5 additions & 4 deletions packages/contentstack-variants/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ let errorLogger: winston.Logger;
let successTransport;
let errorTransport;

function init(_logPath: string) {
function init(_logPath: string, module: string) {
if (!logger || !errorLogger) {
const logsDir = path.resolve(sanitizePath(_logPath), 'logs', 'export');
const logsDir = path.resolve(sanitizePath(_logPath), 'logs', module);
// Create dir if doesn't already exist
mkdirp.sync(logsDir);

Expand Down Expand Up @@ -131,11 +131,12 @@ function init(_logPath: string) {
export const log = (config: ExportConfig | ImportConfig, message: any, type: 'info' | 'error' | 'success') => {
const logsPath = config.data;
// ignoring the type argument, as we are not using it to create a logfile anymore
const module = (config as ImportConfig)['backupDir'] ? 'import' : 'export';
if (type !== 'error') {
// removed type argument from init method
init(logsPath).log(message);
init(logsPath, module).log(message);
} else {
init(logsPath).error(message);
init(logsPath, module).error(message);
}
};

Expand Down

0 comments on commit 2a292a8

Please sign in to comment.