From f98a78ebf267613a59035349c4cadb3a4e66cc23 Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Tue, 1 Aug 2023 16:39:13 +0530 Subject: [PATCH 1/3] feat: stack export rewrite --- .../contentstack-export/src/config/index.ts | 3 +- .../src/export/modules-js/stack.js | 4 +- .../src/export/modules/stack.ts | 93 +++++++++++++++++++ .../src/types/export-config.ts | 1 + .../contentstack-export/src/types/index.ts | 7 ++ 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 packages/contentstack-export/src/export/modules/stack.ts diff --git a/packages/contentstack-export/src/config/index.ts b/packages/contentstack-export/src/config/index.ts index 5fd9fe6090..f423abef4f 100644 --- a/packages/contentstack-export/src/config/index.ts +++ b/packages/contentstack-export/src/config/index.ts @@ -380,7 +380,8 @@ const config: DefaultConfig = { 'global-fields', 'content-types', 'entries', - 'workflows' + 'workflows', + 'stack' ], apis: { userSession: '/user-session/', diff --git a/packages/contentstack-export/src/export/modules-js/stack.js b/packages/contentstack-export/src/export/modules-js/stack.js index 157b99b269..4f4e8329c4 100644 --- a/packages/contentstack-export/src/export/modules-js/stack.js +++ b/packages/contentstack-export/src/export/modules-js/stack.js @@ -49,8 +49,8 @@ class ExportStack { return self.getLocales(apiDetails); } else if (self.config.preserveStackVersion) { log(self.config, 'Exporting stack details', 'success'); - let stackFolderPath = path.resolve(self.config.data, stackConfig.dirName); - let stackContentsFile = path.resolve(stackFolderPath, stackConfig.fileName); + let stackFolderPath = path.resolve(self.config.data, this.stackConfig.dirName); + let stackContentsFile = path.resolve(stackFolderPath, this.stackConfig.fileName); mkdirp.sync(stackFolderPath); diff --git a/packages/contentstack-export/src/export/modules/stack.ts b/packages/contentstack-export/src/export/modules/stack.ts new file mode 100644 index 0000000000..3a50374158 --- /dev/null +++ b/packages/contentstack-export/src/export/modules/stack.ts @@ -0,0 +1,93 @@ +import find from 'lodash/find'; +import { resolve as pResolve } from 'node:path'; +import { isAuthenticated } from '@contentstack/cli-utilities'; + +import config from '../../config'; +import BaseClass from './base-class'; +import { log, formatError, fsUtil } from '../../utils'; +import { StackConfig, ModuleClassParams } from '../../types'; + +export default class ExportStack extends BaseClass { + private stackConfig: StackConfig; + private stackFolderPath: string; + private qs: { + include_count: boolean; + skip?: number; + }; + + constructor({ exportConfig, stackAPIClient }: ModuleClassParams) { + super({ exportConfig, stackAPIClient }); + this.stackConfig = config.modules.stack; + this.qs = { include_count: true }; + this.stackFolderPath = pResolve(this.exportConfig.data, this.stackConfig.dirName); + } + + async start(): Promise { + if (isAuthenticated) { + const stackData = await this.getStack(); + if (stackData?.org_uid) { + this.exportConfig.org_uid = stackData.org_uid; + this.exportConfig.sourceStackName = stackData.name; + } + } + if (!this.exportConfig.preserveStackVersion && !this.exportConfig.hasOwnProperty('master_locale')) { + return this.getLocales(); + } else if (this.exportConfig.preserveStackVersion) { + return this.exportStack(); + } + } + + async getStack() { + return await this.stack.fetch().catch((error: any) => { + log(this.exportConfig, `Failed to export stack. ${formatError(error)}`, 'error'); + }); + } + + async getLocales(skip: number = 0) { + if (skip) { + this.qs.skip = skip; + } + return await this.stack + .locale() + .query(this.qs) + .find() + .then(async (data: any) => { + const { items, count } = data; + if (items?.length) { + skip += this.stackConfig.limit || 100; + const masterLocalObj = find(items, (locale: any) => { + if (locale.fallback_locale === null) { + return locale; + } + }); + if (masterLocalObj) { + return masterLocalObj; + } else if (skip >= count) { + log(this.exportConfig, 'Master locale not found', 'error'); + return; + } else { + return await this.getLocales(skip); + } + } + }) + .catch((error: any) => { + log(this.exportConfig, `Failed to export locales. ${formatError(error)}`, 'error'); + log(this.exportConfig, error, 'error'); + }); + } + + async exportStack() { + log(this.exportConfig, 'Exporting stack details', 'success'); + await fsUtil.makeDirectory(this.stackFolderPath); + return this.stack + .fetch() + .then((resp: any) => { + fsUtil.writeFile(pResolve(this.stackFolderPath, this.stackConfig.fileName), resp); + log(this.exportConfig, 'Exported stack details successfully!', 'success'); + return resp; + }) + .catch((error: any) => { + log(this.exportConfig, `Failed to export stack. ${formatError(error)}`, 'error'); + }); + } +} diff --git a/packages/contentstack-export/src/types/export-config.ts b/packages/contentstack-export/src/types/export-config.ts index 5aa1e7de24..a56db980e6 100644 --- a/packages/contentstack-export/src/types/export-config.ts +++ b/packages/contentstack-export/src/types/export-config.ts @@ -27,6 +27,7 @@ export default interface ExportConfig extends DefaultConfig { access_token?: string; org_uid?: string; source_stack?: string; + sourceStackName?:string; } type branch = { diff --git a/packages/contentstack-export/src/types/index.ts b/packages/contentstack-export/src/types/index.ts index 3839a6de98..ccf95c0918 100644 --- a/packages/contentstack-export/src/types/index.ts +++ b/packages/contentstack-export/src/types/index.ts @@ -114,5 +114,12 @@ export interface CustomRoleConfig{ dependencies?: Modules[]; } +export interface StackConfig{ + dirName: string; + fileName: string; + dependencies?: Modules[]; + limit?: number; +} + export { default as DefaultConfig } from './default-config'; export { default as ExportConfig } from './export-config'; From c9e413e48202b104e1bec865ddd2d6fbd8405d30 Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Tue, 1 Aug 2023 16:40:02 +0530 Subject: [PATCH 2/3] refactor: remove management dependency from clone --- package-lock.json | 2 -- packages/contentstack-clone/package.json | 1 - pnpm-lock.yaml | 25 ------------------------ 3 files changed, 28 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7880b30e6e..0f2fdedd08 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20605,7 +20605,6 @@ "@contentstack/cli-cm-import": "^1.7.0", "@contentstack/cli-command": "^1.2.11", "@contentstack/cli-utilities": "^1.5.1", - "@contentstack/management": "~1.10.0", "async": "^3.2.4", "chalk": "^4.1.0", "child_process": "^1.0.2", @@ -23953,7 +23952,6 @@ "@contentstack/cli-cm-import": "^1.7.0", "@contentstack/cli-command": "^1.2.11", "@contentstack/cli-utilities": "^1.5.1", - "@contentstack/management": "~1.10.0", "@oclif/test": "^1.2.7", "async": "^3.2.4", "chai": "^4.2.0", diff --git a/packages/contentstack-clone/package.json b/packages/contentstack-clone/package.json index 4a02b71da2..914f824c69 100644 --- a/packages/contentstack-clone/package.json +++ b/packages/contentstack-clone/package.json @@ -9,7 +9,6 @@ "@contentstack/cli-cm-import": "^1.7.0", "@contentstack/cli-command": "^1.2.11", "@contentstack/cli-utilities": "^1.5.1", - "@contentstack/management": "~1.10.0", "@colors/colors": "^1.5.0", "async": "^3.2.4", "chalk": "^4.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4aaf00300f..5836ea4149 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -363,7 +363,6 @@ importers: '@contentstack/cli-cm-import': ^1.7.0 '@contentstack/cli-command': ^1.2.11 '@contentstack/cli-utilities': ^1.5.1 - '@contentstack/management': ~1.10.0 '@oclif/test': ^1.2.7 async: ^3.2.4 chai: ^4.2.0 @@ -389,7 +388,6 @@ importers: '@contentstack/cli-cm-import': link:../contentstack-import '@contentstack/cli-command': link:../contentstack-command '@contentstack/cli-utilities': link:../contentstack-utilities - '@contentstack/management': 1.10.0 async: 3.2.4 chalk: 4.1.2 child_process: 1.0.2 @@ -1548,19 +1546,6 @@ packages: uuid: 8.3.2 dev: false - /@contentstack/management/1.10.0: - resolution: - { integrity: sha512-wnmVS19n3cZeh6T2PbBXGCr4irktunVmuECTyPF5++Rsc6GDO+FoQGCZdvzHpRZE0GLiPFIID50z2TZuNArEOg== } - engines: { node: '>=8.0.0' } - dependencies: - axios: 0.27.2 - form-data: 3.0.1 - lodash: 4.17.21 - qs: 6.11.2 - transitivePeerDependencies: - - debug - dev: false - /@contentstack/management/1.10.0_debug@4.3.4: resolution: { integrity: sha512-wnmVS19n3cZeh6T2PbBXGCr4irktunVmuECTyPF5++Rsc6GDO+FoQGCZdvzHpRZE0GLiPFIID50z2TZuNArEOg== } @@ -4543,16 +4528,6 @@ packages: xml2js: 0.5.0 dev: true - /axios/0.27.2: - resolution: - { integrity: sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ== } - dependencies: - follow-redirects: 1.15.2 - form-data: 4.0.0 - transitivePeerDependencies: - - debug - dev: false - /axios/0.27.2_debug@4.3.4: resolution: { integrity: sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ== } From cd7740d6876796614353400ed76be9eba5573913 Mon Sep 17 00:00:00 2001 From: Aman Kumar Date: Tue, 1 Aug 2023 17:01:00 +0530 Subject: [PATCH 3/3] refactor: stack export --- packages/contentstack-export/src/export/modules/stack.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/contentstack-export/src/export/modules/stack.ts b/packages/contentstack-export/src/export/modules/stack.ts index 3a50374158..c98a87bc8a 100644 --- a/packages/contentstack-export/src/export/modules/stack.ts +++ b/packages/contentstack-export/src/export/modules/stack.ts @@ -31,13 +31,14 @@ export default class ExportStack extends BaseClass { } } if (!this.exportConfig.preserveStackVersion && !this.exportConfig.hasOwnProperty('master_locale')) { + //fetch master locale details return this.getLocales(); } else if (this.exportConfig.preserveStackVersion) { return this.exportStack(); } } - async getStack() { + async getStack(): Promise { return await this.stack.fetch().catch((error: any) => { log(this.exportConfig, `Failed to export stack. ${formatError(error)}`, 'error'); }); @@ -76,7 +77,7 @@ export default class ExportStack extends BaseClass { }); } - async exportStack() { + async exportStack(): Promise { log(this.exportConfig, 'Exporting stack details', 'success'); await fsUtil.makeDirectory(this.stackFolderPath); return this.stack