Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overwrite feature extension and global field #1057

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion packages/contentstack-import/src/commands/cm/stacks/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ export default class ImportCommand extends Command {
required: false,
description: '[optional] Override marketplace prompts',
}),
'replace-existing': flags.boolean({
required: false,
default: false,
description: '[optional] replace existing modules',
}),
'skip-existing': flags.boolean({
required: false,
default: false,
description: '[optional] removes the module exist warning messages',
}),
};

static aliases: string[] = ['cm:import'];
Expand All @@ -115,7 +125,11 @@ export default class ImportCommand extends Command {
const moduleImporter = new ModuleImporter(managementAPIClient, importConfig);
await moduleImporter.start();
log(importConfig, `The content has been imported to the stack ${importConfig.apiKey} successfully!`, 'success');
log(importConfig, `The log has been stored at '${path.join(importConfig.backupDir, 'logs', 'import')}'`, 'success');
log(
importConfig,
`The log has been stored at '${path.join(importConfig.backupDir, 'logs', 'import')}'`,
'success',
);
} catch (error) {
log({ data: backupDir } as ImportConfig, `Failed to import stack content - ${formatError(error)}`, 'error');
log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type ApiModuleType =
| 'publish-assets'
| 'create-assets-folder'
| 'create-extensions'
| 'update-extensions'
| 'create-locale'
| 'update-locale'
| 'create-gfs'
Expand Down
122 changes: 103 additions & 19 deletions packages/contentstack-import/src/import/modules/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import isEmpty from 'lodash/isEmpty';
import values from 'lodash/values';
import cloneDeep from 'lodash/cloneDeep';
import { join, resolve } from 'node:path';
import { ExtensionData } from '@contentstack/management/types/stack/extension';

import { log, formatError, fsUtil, fileHelper } from '../../utils';
import BaseClass, { ApiOptions } from './base-class';
Expand All @@ -17,6 +19,7 @@ export default class ImportExtensions extends BaseClass {
private extUidMapper: Record<string, unknown>;
private extSuccess: Record<string, unknown>[];
private extFailed: Record<string, unknown>[];
private existingExtensions: Record<string, unknown>[];

constructor({ importConfig, stackAPIClient }: ModuleClassParams) {
super({ importConfig, stackAPIClient });
Expand All @@ -28,6 +31,7 @@ export default class ImportExtensions extends BaseClass {
this.extFailsPath = join(this.mapperDirPath, 'fails.json');
this.extFailed = [];
this.extSuccess = [];
this.existingExtensions = [];
this.extUidMapper = {};
}

Expand All @@ -40,7 +44,10 @@ export default class ImportExtensions extends BaseClass {

//Step1 check folder exists or not
if (fileHelper.fileExistsSync(this.extensionsFolderPath)) {
this.extensions = fsUtil.readFile(join(this.extensionsFolderPath, 'extensions.json'), true) as Record<string,unknown>;
this.extensions = fsUtil.readFile(join(this.extensionsFolderPath, 'extensions.json'), true) as Record<
string,
unknown
>;
} else {
log(this.importConfig, `No such file or directory - '${this.extensionsFolderPath}'`, 'error');
return;
Expand All @@ -53,6 +60,13 @@ export default class ImportExtensions extends BaseClass {

await this.importExtensions();

// Note: if any extensions present, then update it
if (this.importConfig.replaceExisting && this.existingExtensions.length > 0) {
await this.replaceExtensions().catch((error: Error) => {
log(this.importConfig, `Error while replacing extensions ${formatError(error)}`, 'error');
});
}

if (this.extSuccess?.length) {
fsUtil.writeFile(this.extSuccessPath, this.extSuccess);
}
Expand Down Expand Up @@ -80,10 +94,14 @@ export default class ImportExtensions extends BaseClass {
};

const onReject = ({ error, apiData }: any) => {
const err = error?.message ? JSON.parse(error.message) : error;
const { title } = apiData;
if (err?.errors?.title) {
log(this.importConfig, `Extension '${title}' already exists`, 'info');
if (error?.errors?.title) {
if (this.importConfig.replaceExisting) {
this.existingExtensions.push(apiData);
}
if (!this.importConfig.skipExisting) {
log(this.importConfig, `Extension '${title}' already exists`, 'info');
}
} else {
this.extFailed.push(apiData);
log(this.importConfig, `Extension '${title}' failed to be import ${formatError(error)}`, 'error');
Expand All @@ -96,7 +114,6 @@ export default class ImportExtensions extends BaseClass {
apiContent,
processName: 'import extensions',
apiParams: {
serializeData: this.serializeExtensions.bind(this),
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'create-extensions',
Expand All @@ -109,19 +126,86 @@ export default class ImportExtensions extends BaseClass {
);
}

/**
* @method serializeExtensions
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializeExtensions(apiOptions: ApiOptions): ApiOptions {
const { apiData: extension } = apiOptions;
if (this.extUidMapper.hasOwnProperty(extension.uid)) {
log(this.importConfig, `Extension '${extension.title}' already exists. Skipping it to avoid duplicates!`, 'info');
apiOptions.entity = undefined;
} else {
apiOptions.apiData = extension;
}
return apiOptions;
async replaceExtensions(): Promise<any> {
const onSuccess = ({ response, apiData: { uid, title } = { uid: null, title: '' } }: any) => {
this.extSuccess.push(response);
this.extUidMapper[uid] = response.uid;
log(this.importConfig, `Extension '${title}' replaced successfully`, 'success');
fsUtil.writeFile(this.extUidMapperPath, this.extUidMapper);
};

const onReject = ({ error, apiData }: any) => {
this.extFailed.push(apiData);
log(this.importConfig, `Extension '${apiData.title}' failed to replace ${formatError(error)}`, 'error');
log(this.importConfig, error, 'error');
};

await this.makeConcurrentCall(
{
apiContent: this.existingExtensions,
processName: 'Replace extensions',
apiParams: {
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'update-extensions',
includeParamOnCompletion: true,
},
concurrencyLimit: this.importConfig.concurrency || this.importConfig.fetchConcurrency || 1,
},
this.replaceExtensionHandler.bind(this),
);
}

async replaceExtensionHandler({
apiParams,
element: extension,
isLastRequest,
}: {
apiParams: ApiOptions;
element: Record<string, string>;
isLastRequest: boolean;
}) {
return new Promise(async (resolve, reject) => {
const { items: [extensionsInStack] = [] }: any = await this.stack
.extension()
.query({ query: { title: extension.title } })
.findOne()
.catch((error) => {
apiParams.reject({
error,
apiData: extension,
});
reject(true);
});
if (extensionsInStack) {
const extensionPayload = this.stack.extension(extension.uid);
Object.assign(extensionPayload, cloneDeep(extensionsInStack), {
uid: extensionsInStack.uid,
urlPath: extensionsInStack.urlPath,
});
return extensionPayload
.update()
.then((response) => {
apiParams.resolve({
response,
apiData: extension,
});
resolve(true);
})
.catch((error) => {
apiParams.reject({
error,
apiData: extension,
});
reject(true);
});
} else {
apiParams.reject({
error: new Error(`Extension with title ${extension.title} not found in the stack`),
apiData: extension,
});
reject(true);
}
});
}
}
79 changes: 73 additions & 6 deletions packages/contentstack-import/src/import/modules/global-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import * as path from 'path';
import { values, isEmpty, filter, pick } from 'lodash';
import { isEmpty, cloneDeep } from 'lodash';
import { cliux } from '@contentstack/cli-utilities';
import { GlobalFieldData } from '@contentstack/management/types/stack/globalField';
import { GlobalField, GlobalFieldData } from '@contentstack/management/types/stack/globalField';
import { fsUtil, log, formatError, fileHelper, lookupExtension, removeReferenceFields } from '../../utils';
import { ImportConfig, ModuleClassParams } from '../../types';
import BaseClass, { ApiOptions } from './base-class';
Expand All @@ -30,6 +30,7 @@ export default class ImportGlobalFields extends BaseClass {
private marketplaceAppMapperPath: string;
private reqConcurrency: number;
private installedExtensions: Record<string, unknown>;
private existingGFs: Record<string, any>[];
private gFsConfig: {
dirName: string;
fileName: string;
Expand All @@ -47,6 +48,7 @@ export default class ImportGlobalFields extends BaseClass {
this.createdGFs = [];
this.failedGFs = [];
this.pendingGFs = [];
this.existingGFs = [];
this.reqConcurrency = this.gFsConfig.writeConcurrency || this.config.writeConcurrency;
this.gFsMapperPath = path.resolve(this.config.data, 'mapper', 'global_fields');
this.gFsFolderPath = path.resolve(this.config.data, this.gFsConfig.dirName);
Expand All @@ -73,6 +75,13 @@ export default class ImportGlobalFields extends BaseClass {

await this.importGFs();
fsUtil.writeFile(this.gFsPendingPath, this.pendingGFs);

if (this.importConfig.replaceExisting && this.existingGFs.length > 0) {
await this.replaceGFs().catch((error: Error) => {
log(this.importConfig, `Error while replacing global fields ${formatError(error)}`, 'error');
});
}

log(this.config, 'Global fields import has been completed!', 'info');
}

Expand All @@ -83,10 +92,23 @@ export default class ImportGlobalFields extends BaseClass {
fsUtil.writeFile(this.gFsUidMapperPath, this.gFsUidMapper);
log(this.config, 'Global field ' + uid + ' created successfully', 'success');
};
const onReject = ({ error, apiData: { uid } = undefined }: any) => {
log(this.importConfig, `Global fields '${uid}' failed to import`, 'error');
log(this.importConfig, formatError(error), 'error');
this.failedGFs.push({ uid });
const onReject = ({ error, apiData: globalField = undefined }: any) => {
const uid = globalField.uid;
if (error?.errors?.title) {
if (this.importConfig.replaceExisting) {
// Note: skipping the pending GFs from update, reason: pending GFs gets updated at the CTs import
if (this.pendingGFs.indexOf(uid) === -1) {
this.existingGFs.push(globalField);
}
}
if (!this.importConfig.skipExisting) {
log(this.importConfig, `Global fields '${uid}' already exist`, 'info');
}
} else {
log(this.importConfig, `Global fields '${uid}' failed to import`, 'error');
log(this.importConfig, formatError(error), 'error');
this.failedGFs.push({ uid });
}
};

return await this.makeConcurrentCall(
Expand Down Expand Up @@ -140,4 +162,49 @@ export default class ImportGlobalFields extends BaseClass {
});
});
}

async replaceGFs(): Promise<any> {
const onSuccess = ({ response: globalField, apiData: { uid } = { uid: null } }: any) => {
this.createdGFs.push(globalField);
this.gFsUidMapper[uid] = globalField;
fsUtil.writeFile(this.gFsUidMapperPath, this.gFsUidMapper);
log(this.config, 'Global field ' + uid + ' replaced successfully', 'success');
};

const onReject = ({ error, apiData: { uid } }: any) => {
log(this.importConfig, `Global fields '${uid}' failed to replace`, 'error');
log(this.importConfig, formatError(error), 'error');
this.failedGFs.push({ uid });
};

await this.makeConcurrentCall(
{
apiContent: this.existingGFs,
processName: 'Replace global fields',
apiParams: {
serializeData: this.serializeReplaceGFs.bind(this),
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'update-gfs',
includeParamOnCompletion: true,
},
concurrencyLimit: this.importConfig.concurrency || this.importConfig.fetchConcurrency || 1,
},
undefined,
false,
);
}

/**
* @method serializeUpdateGFs
* @param {ApiOptions} apiOptions ApiOptions
* @returns {ApiOptions} ApiOptions
*/
serializeReplaceGFs(apiOptions: ApiOptions): ApiOptions {
const { apiData: globalField } = apiOptions;
const globalFieldPayload = this.stack.globalField(globalField.uid);
Object.assign(globalFieldPayload, cloneDeep(globalField));
apiOptions.apiData = globalFieldPayload;
return apiOptions;
}
}
2 changes: 2 additions & 0 deletions packages/contentstack-import/src/types/import-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export default interface ImportConfig extends DefaultConfig, ExternalConfig {
destinationStackName?: string;
org_uid?: string;
contentVersion: number;
replaceExisting?: boolean;
skipExisting?: boolean;
}

type branch = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
// Note to support old modules
config.target_stack = config.apiKey;

config.replaceExisting = importCmdFlags['replace-existing'];
config.skipExisting = importCmdFlags['skip-existing'];

return config;
};

Expand Down