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

Stack module export rewrite #957

Merged
merged 3 commits into from
Aug 1, 2023
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
2 changes: 0 additions & 2 deletions package-lock.json

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

1 change: 0 additions & 1 deletion packages/contentstack-clone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/contentstack-export/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ const config: DefaultConfig = {
'global-fields',
'content-types',
'entries',
'workflows'
'workflows',
'stack'
],
apis: {
userSession: '/user-session/',
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-export/src/export/modules-js/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
94 changes: 94 additions & 0 deletions packages/contentstack-export/src/export/modules/stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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<void> {
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')) {
//fetch master locale details
return this.getLocales();
} else if (this.exportConfig.preserveStackVersion) {
return this.exportStack();
}
}

async getStack(): Promise<any> {
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(): Promise<any> {
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');
});
}
}
1 change: 1 addition & 0 deletions packages/contentstack-export/src/types/export-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default interface ExportConfig extends DefaultConfig {
access_token?: string;
org_uid?: string;
source_stack?: string;
sourceStackName?:string;
}

type branch = {
Expand Down
7 changes: 7 additions & 0 deletions packages/contentstack-export/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
25 changes: 0 additions & 25 deletions pnpm-lock.yaml

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