Skip to content

Commit

Permalink
added support for branches, global fields
Browse files Browse the repository at this point in the history
  • Loading branch information
shafeeqd959 committed Jan 13, 2025
1 parent 51428a6 commit b1c7fc1
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/contentstack-audit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ npm install -g @contentstack/cli-audit
$ csdx COMMAND
running command...
$ csdx (--version|-v)
@contentstack/cli-audit/1.7.4 darwin-arm64 node-v22.8.0
@contentstack/cli-audit/1.7.4 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-bulk-publish/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ npm install -g @contentstack/cli-cm-bulk-publish
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-cm-bulk-publish/1.7.0 darwin-arm64 node-v22.2.0
@contentstack/cli-cm-bulk-publish/1.7.1 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
22 changes: 16 additions & 6 deletions packages/contentstack-import-setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ USAGE
$ csdx cm:stacks:import-setup [-k <value>] [-d <value>] [-a <value>] [--modules <value,value>]
FLAGS
-B, --branch=<value> The name of the target stack's branch
-a, --alias=<value> alias of the management token
-d, --data-dir=<value> path and location where data is stored
-k, --stack-api-key=<value> API key of the target stack
--modules=<option> [optional] specific module name
<options: content-types|entries|both>
--modules=<option>... [optional] specific module name
<options: global-fields|content-types|entries>
DESCRIPTION
Import content from a stack
Expand All @@ -83,7 +84,11 @@ ALIASES
$ csdx cm:import-setup
EXAMPLES
$ csdx cm:stacks:import-setup --stack-api-key <stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup --stack-api-key <target_stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name> -b <branch_name>
```

## `csdx cm:stacks:import-setup [-k <value>] [-d <value>] [-a <value>] [--modules <value,value>]`
Expand All @@ -95,11 +100,12 @@ USAGE
$ csdx cm:stacks:import-setup [-k <value>] [-d <value>] [-a <value>] [--modules <value,value>]
FLAGS
-B, --branch=<value> The name of the target stack's branch
-a, --alias=<value> alias of the management token
-d, --data-dir=<value> path and location where data is stored
-k, --stack-api-key=<value> API key of the target stack
--modules=<option> [optional] specific module name
<options: content-types|entries|both>
--modules=<option>... [optional] specific module name
<options: global-fields|content-types|entries>
DESCRIPTION
Import content from a stack
Expand All @@ -108,7 +114,11 @@ ALIASES
$ csdx cm:import-setup
EXAMPLES
$ csdx cm:stacks:import-setup --stack-api-key <stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup --stack-api-key <target_stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name> -b <branch_name>
```

_See code: [src/commands/cm/stacks/import-setup.ts](https://github.com/contentstack/cli/blob/main/packages/contentstack-import-setup/src/commands/cm/stacks/import-setup.ts)_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export default class ImportSetupCommand extends Command {
static description = messageHandler.parse('Import content from a stack');

static examples: string[] = [
`csdx cm:stacks:import-setup --stack-api-key <stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>`,
`csdx cm:stacks:import-setup --stack-api-key <target_stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>`,
`csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name>`,
`csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name> -b <branch_name>`,
];

static flags: FlagInput = {
Expand All @@ -36,8 +38,14 @@ export default class ImportSetupCommand extends Command {
description: 'alias of the management token',
}),
modules: flags.string({
options: ['content-types', 'entries', 'both'], // only allow the value to be from a discrete set
options: ['global-fields', 'content-types', 'entries'], // only allow the value to be from a discrete set
description: '[optional] specific module name',
multiple: true,
}),
branch: flags.string({
char: 'B',
description: "The name of the target stack's branch",
parse: printFlagDeprecation(['-B'], ['--branch']),
}),
};

Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-import-setup/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const config: DefaultConfig = {
'global-fields': {
dirName: 'global_fields',
fileName: 'globalfields.json',
dependencies: ['marketplace-apps'],
dependencies: ['extensions', 'marketplace-apps'],
},
'marketplace-apps': {
dirName: 'marketplace_apps',
Expand Down
5 changes: 5 additions & 0 deletions packages/contentstack-import-setup/src/import/import-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Module from 'module';
import { ImportConfig, Modules } from '../types';
import { backupHandler, log } from '../utils';
import { ContentstackClient, formatError } from '@contentstack/cli-utilities';
import { validateBranch } from '../utils';

export default class ImportSetup {
protected config: ImportConfig;
Expand Down Expand Up @@ -91,6 +92,10 @@ export default class ImportSetup {
if (backupDir) {
this.config.backupDir = backupDir;
}
if (this.config.branchName) {
await validateBranch(this.stackAPIClient, this.config, this.config.branchName);
}

await this.generateDependencyTree();
await this.runModuleImports();
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as chalk from 'chalk';
import { log, fsUtil } from '../../utils';
import { join } from 'path';
import { ImportConfig, ModuleClassParams } from '../../types';
import BaseImportSetup from './base-setup';

export default class GlobalFieldsImportSetup extends BaseImportSetup {
constructor(options: ModuleClassParams) {
super(options);
}

async start() {
try {
await this.setupDependencies();
log(this.config, `Generate required setup files for global fields`, 'success');
} catch (error) {
log(this.config, `Error generating ${error.message}`, 'error');
}
}
}
25 changes: 25 additions & 0 deletions packages/contentstack-import-setup/src/utils/common-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import chalk from 'chalk';
import { log } from '../utils';
import { ImportConfig } from 'src/types';

export const validateBranch = async (stackAPIClient: any, config: ImportConfig, branch: any) => {
return new Promise(async (resolve, reject) => {
try {
const data = await stackAPIClient.branch(branch).fetch();
if (data && typeof data === 'object') {
if (data.error_message) {
log(config, chalk.red(data.error_message), 'error');
log(config, chalk.red('No branch found with the name ' + branch), 'error');
reject({ message: 'No branch found with the name ' + branch, error: data.error_message });
} else {
resolve(data);
}
} else {
reject({ message: 'No branch found with the name ' + branch, error: {} });
}
} catch (error) {
log(config, chalk.red('No branch found with the name ' + branch), 'error');
reject({ message: 'No branch found with the name ' + branch, error });
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,9 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {

if (importCmdFlags['branch']) {
config.branchName = importCmdFlags['branch'];
config.branchDir = path.join(sanitizePath(config.contentDir), sanitizePath(config.branchName));
}

const selectedModule = importCmdFlags['modules'] || (await askSelectedModules());
if (selectedModule === 'both') {
config.selectedModules = ['content-types', 'entries'];
} else {
config.selectedModules = [selectedModule];
}

config.selectedModules = importCmdFlags['modules'] || [await askSelectedModules()];
if (importCmdFlags['backup-dir']) {
config.useBackedupDir = importCmdFlags['backup-dir'];
}
Expand Down
1 change: 1 addition & 0 deletions packages/contentstack-import-setup/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { fsUtil } from './file-helper';
export { default as backupHandler } from './backup-handler';
export { log, unlinkFileLogger } from './logger';
export * from './log';
export * from './common-helper';
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export const askSelectedModules = async (): Promise<string> => {
name: 'selectedModule',
message: 'Please select a module to generate the mapper files',
choices: [
{ name: 'Global Fields', value: 'global-fields' },
{ name: 'Content types', value: 'content-types' },
{ name: 'Entries', value: 'entries' },
{ name: 'Both', value: 'both' },
],
});
};
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-launch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ npm install -g @contentstack/cli-launch
$ csdx COMMAND
running command...
$ csdx (--version|-v)
@contentstack/cli-launch/1.3.1 darwin-arm64 node-v22.8.0
@contentstack/cli-launch/1.3.1 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down
24 changes: 17 additions & 7 deletions packages/contentstack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ npm install -g @contentstack/cli
$ csdx COMMAND
running command...
$ csdx (--version|-v)
@contentstack/cli/1.32.2 darwin-arm64 node-v22.8.0
@contentstack/cli/1.32.2 darwin-arm64 node-v22.2.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down Expand Up @@ -2381,11 +2381,12 @@ USAGE
$ csdx cm:import-setup cm:stacks:import-setup [-k <value>] [-d <value>] [-a <value>] [--modules <value,value>]
FLAGS
-B, --branch=<value> The name of the target stack's branch
-a, --alias=<value> alias of the management token
-d, --data-dir=<value> path and location where data is stored
-k, --stack-api-key=<value> API key of the target stack
--modules=<option> [optional] specific module name
<options: content-types|entries|both>
--modules=<option>... [optional] specific module name
<options: global-fields|content-types|entries>
DESCRIPTION
Import content from a stack
Expand All @@ -2394,7 +2395,11 @@ ALIASES
$ csdx cm:import-setup
EXAMPLES
$ csdx cm:stacks:import-setup --stack-api-key <stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup --stack-api-key <target_stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name> -b <branch_name>
```

## `csdx cm:migrate-rte`
Expand Down Expand Up @@ -2864,11 +2869,12 @@ USAGE
$ csdx cm:stacks:import-setup [-k <value>] [-d <value>] [-a <value>] [--modules <value,value>]
FLAGS
-B, --branch=<value> The name of the target stack's branch
-a, --alias=<value> alias of the management token
-d, --data-dir=<value> path and location where data is stored
-k, --stack-api-key=<value> API key of the target stack
--modules=<option> [optional] specific module name
<options: content-types|entries|both>
--modules=<option>... [optional] specific module name
<options: global-fields|content-types|entries>
DESCRIPTION
Import content from a stack
Expand All @@ -2877,7 +2883,11 @@ ALIASES
$ csdx cm:import-setup
EXAMPLES
$ csdx cm:stacks:import-setup --stack-api-key <stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup --stack-api-key <target_stack_api_key> --data-dir <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name>
$ csdx cm:stacks:import-setup -k <target_stack_api_key> -d <path/of/export/destination/dir> --modules <module_name, module_name> -b <branch_name>
```

_See code: [@contentstack/cli-cm-import-setup](https://github.com/contentstack/cli/blob/main/packages/contentstack-import-setup/src/commands/cm/stacks/import-setup.ts)_
Expand Down

0 comments on commit b1c7fc1

Please sign in to comment.