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

CS-43639-added update the extension after the creation of content types and version bump #1300

Merged
merged 8 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 35 additions & 23 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/contentstack-clone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/rohitmishra209/cli-cm-clone/issues",
"dependencies": {
"@contentstack/cli-cm-export": "~1.10.5",
"@contentstack/cli-cm-import": "~1.13.4",
"@contentstack/cli-cm-import": "~1.13.5",
"@contentstack/cli-command": "~1.2.16",
"@contentstack/cli-utilities": "~1.5.13",
"@colors/colors": "^1.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-import/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-import",
"description": "Contentstack CLI plugin to import content into stack",
"version": "1.13.4",
"version": "1.13.5",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
32 changes: 17 additions & 15 deletions packages/contentstack-import/src/import/modules/base-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ export default abstract class BaseClass {
apiData: includeParamOnCompletion ? apiData : undefined,
});

if (
!apiData ||
(entity === 'publish-entries' && !apiData.entryUid) ||
(entity === 'update-extensions' && !apiData.uid)
) {
return Promise.resolve();
}
switch (entity) {
case 'create-assets-folder':
return this.stack
Expand Down Expand Up @@ -283,6 +290,16 @@ export default abstract class BaseClass {
.create({ extension: omit(apiData, ['uid']) as ExtensionData })
.then(onSuccess)
.catch(onReject);
case 'update-extensions':
return this.stack
.extension(apiData.uid)
.fetch()
.then((extension) => {
extension.scope = apiData.scope;
return extension.update();
})
.then(onSuccess)
.catch(onReject);
case 'create-locale':
return this.stack
.locale()
Expand All @@ -298,14 +315,8 @@ export default abstract class BaseClass {
case 'create-cts':
return this.stack.contentType().create(apiData).then(onSuccess).catch(onReject);
case 'update-cts':
if (!apiData) {
return Promise.resolve();
}
return apiData.update().then(onSuccess).catch(onReject);
case 'update-gfs':
if (!apiData) {
return Promise.resolve();
}
return apiData.update().then(onSuccess).catch(onReject);
case 'create-environments':
return this.stack
Expand Down Expand Up @@ -347,9 +358,6 @@ export default abstract class BaseClass {
.then(onSuccess)
.catch(onReject);
case 'create-entries':
if (!apiData) {
return Promise.resolve();
}
if (additionalInfo[apiData?.uid]?.isLocalized) {
return apiData.update({ locale: additionalInfo.locale }).then(onSuccess).catch(onReject);
}
Expand All @@ -360,14 +368,8 @@ export default abstract class BaseClass {
.then(onSuccess)
.catch(onReject);
case 'update-entries':
if (!apiData) {
return Promise.resolve();
}
return apiData.update({ locale: additionalInfo.locale }).then(onSuccess).catch(onReject);
case 'publish-entries':
if (!apiData || !apiData.entryUid) {
return Promise.resolve();
}
return this.stack
.contentType(additionalInfo.cTUid)
.entry(apiData.entryUid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default class ContentTypesImport extends BaseClass {
};
private taxonomiesPath: string;
public taxonomies: Record<string, unknown>;
private extPendingPath: string;

constructor({ importConfig, stackAPIClient }: ModuleClassParams) {
super({ importConfig, stackAPIClient });
Expand Down Expand Up @@ -78,6 +79,7 @@ export default class ContentTypesImport extends BaseClass {
this.createdGFs = [];
this.pendingGFs = [];
this.taxonomiesPath = path.join(importConfig.data, 'mapper/taxonomies', 'success.json');
this.extPendingPath = path.join(importConfig.data, 'mapper', 'extensions', 'pending_extensions.js');
}

async start(): Promise<any> {
Expand Down Expand Up @@ -106,6 +108,9 @@ export default class ContentTypesImport extends BaseClass {
if (this.fieldRules.length > 0) {
await fsUtil.writeFile(path.join(this.cTsFolderPath, 'field_rules_uid.json'), this.fieldRules);
}
log(this.importConfig, 'Updating the extensions...', 'success');
await this.updatePendingExtensions();
log(this.importConfig, 'Successfully updated the extensions.', 'success');
await this.updatePendingGFs().catch((error) => {
log(this.importConfig, `Error while updating pending global field ${formatError(error)}`, 'error');
});
Expand Down Expand Up @@ -250,4 +255,44 @@ export default class ContentTypesImport extends BaseClass {
apiOptions.apiData = globalFieldPayload;
return apiOptions;
}

async updatePendingExtensions(): Promise<any> {
let apiContent = fsUtil.readFile(this.extPendingPath) as Record<string, any>[];
if (apiContent.length === 0) {
log(this.importConfig, `No extensions found to be updated.`, 'success');
return;
}

const onSuccess = ({ response, apiData: { uid, title } = { uid: null, title: '' } }: any) => {
log(this.importConfig, `Successfully updated the '${response.title}' extension.`, 'success');
};

const onReject = ({ error, apiData }: any) => {
const { uid } = apiData;
if (error?.errors?.title) {
if (!this.importConfig.skipExisting) {
log(this.importConfig, `Extension '${uid}' already exists.`, 'info');
}
} else {
log(this.importConfig, `Failed to update '${uid}' extension due to ${formatError(error)}.`, 'error');
log(this.importConfig, error, 'error');
}
};

return await this.makeConcurrentCall(
{
apiContent,
processName: 'update extensions',
apiParams: {
reject: onReject.bind(this),
resolve: onSuccess.bind(this),
entity: 'update-extensions',
includeParamOnCompletion: true,
},
concurrencyLimit: this.importConfig.concurrency || this.importConfig.fetchConcurrency || 1,
},
undefined,
false,
);
}
}
35 changes: 33 additions & 2 deletions packages/contentstack-import/src/import/modules/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { join } from 'node:path';

import { log, formatError, fsUtil, fileHelper } from '../../utils';
import BaseClass, { ApiOptions } from './base-class';
import { ModuleClassParams, Extensions } from '../../types';
import { ModuleClassParams, Extensions, ExtensionType } from '../../types';

export default class ImportExtensions extends BaseClass {
private mapperDirPath: string;
Expand All @@ -19,6 +19,8 @@ export default class ImportExtensions extends BaseClass {
private extSuccess: Record<string, unknown>[];
private extFailed: Record<string, unknown>[];
private existingExtensions: Record<string, unknown>[];
private extPendingPath: string;
private extensionObject: Record<string, unknown>[];

constructor({ importConfig, stackAPIClient }: ModuleClassParams) {
super({ importConfig, stackAPIClient });
Expand All @@ -28,10 +30,12 @@ export default class ImportExtensions extends BaseClass {
this.extUidMapperPath = join(this.mapperDirPath, 'uid-mapping.json');
this.extSuccessPath = join(this.mapperDirPath, 'success.json');
this.extFailsPath = join(this.mapperDirPath, 'fails.json');
this.extPendingPath = join(this.mapperDirPath, 'pending_extensions.js');
this.extFailed = [];
this.extSuccess = [];
this.existingExtensions = [];
this.extUidMapper = {};
this.extensionObject = [];
}

/**
Expand All @@ -45,7 +49,7 @@ export default class ImportExtensions extends BaseClass {
if (fileHelper.fileExistsSync(this.extensionsFolderPath)) {
this.extensions = fsUtil.readFile(join(this.extensionsFolderPath, 'extensions.json'), true) as Record<
string,
unknown
Record<string, unknown>
>;
} else {
log(this.importConfig, `No such file or directory - '${this.extensionsFolderPath}'`, 'error');
Expand All @@ -57,8 +61,14 @@ export default class ImportExtensions extends BaseClass {
? (fsUtil.readFile(join(this.extUidMapperPath), true) as Record<string, unknown>)
: {};

// Check whether the scope of an extension contains content-types in scope
// Remove the scope and store the scope with uid in pending extensions
this.getContentTypesInScope();

await this.importExtensions();

// Update the uid of the extension
this.updateUidExtension();
// Note: if any extensions present, then update it
if (this.importConfig.replaceExisting && this.existingExtensions.length > 0) {
await this.replaceExtensions().catch((error: Error) => {
Expand Down Expand Up @@ -209,4 +219,25 @@ export default class ImportExtensions extends BaseClass {
}
});
}

getContentTypesInScope() {
const extension = values(this.extensions);
extension.forEach((ext: ExtensionType) => {
let ct: any = ext?.scope?.content_types || [];
if ((ct.length === 1 && ct[0] !== '$all') || ct?.length > 1) {
log(this.importConfig, `Removing the content-types ${ct.join(',')} from the extension ${ext.title} ...`, 'info');
const { uid, scope } = ext;
this.extensionObject.push({ uid, scope });
delete ext.scope;
this.extensions[ext.uid] = ext;
}
});
}

updateUidExtension() {
for (let i in this.extensionObject) {
this.extensionObject[i].uid = this.extUidMapper[this.extensionObject[i].uid as string];
}
fsUtil.writeFile(this.extPendingPath, this.extensionObject);
}
}
8 changes: 7 additions & 1 deletion packages/contentstack-import/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,10 @@ export { default as DefaultConfig } from './default-config';
export { default as ImportConfig } from './import-config';

export * from './entries'
export * from './marketplace-app'
export * from './marketplace-app'

export type ExtensionType = {
uid: string,
scope: Record<string,unknown>,
title: string
}
2 changes: 1 addition & 1 deletion packages/contentstack-seed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
"@contentstack/cli-cm-import": "~1.13.4",
"@contentstack/cli-cm-import": "~1.13.5",
"@contentstack/cli-command": "~1.2.16",
"@contentstack/cli-utilities": "~1.5.13",
"inquirer": "8.2.4",
Expand Down
Loading
Loading