-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexport.ts
More file actions
178 lines (165 loc) · 6.81 KB
/
export.ts
File metadata and controls
178 lines (165 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Command } from '@contentstack/cli-command';
import {
cliux,
messageHandler,
printFlagDeprecation,
managementSDKClient,
flags,
ContentstackClient,
FlagInput,
pathValidator,
sanitizePath,
configHandler,
log,
handleAndLogError,
getLogPath,
} from '@contentstack/cli-utilities';
import { ModuleExporter } from '../../../export';
import { Context, ExportConfig } from '../../../types';
import { setupExportConfig, writeExportMetaFile } from '../../../utils';
export default class ExportCommand extends Command {
static description: string = messageHandler.parse('Export content from a stack');
static examples: string[] = [
'csdx cm:stacks:export --stack-api-key <stack_api_key> --data-dir <path/of/export/destination/dir>',
'csdx cm:stacks:export --config <path/to/config/dir>',
'csdx cm:stacks:export --alias <management_token_alias>',
'csdx cm:stacks:export --alias <management_token_alias> --data-dir <path/to/export/destination/dir>',
'csdx cm:stacks:export --alias <management_token_alias> --config <path/to/config/file>',
'csdx cm:stacks:export --module <single module name>',
'csdx cm:stacks:export --branch [optional] branch name',
];
static usage: string =
'cm:stacks:export [-c <value>] [-k <value>] [-d <value>] [-a <value>] [--module <value>] [--content-types <value>] [--branch <value>] [--secured-assets]';
static flags: FlagInput = {
config: flags.string({
char: 'c',
description: '[optional] Path of the config',
}),
'stack-uid': flags.string({
char: 's',
description: 'API key of the source stack',
hidden: true,
parse: printFlagDeprecation(['-s', '--stack-uid'], ['-k', '--stack-api-key']),
}),
'stack-api-key': flags.string({
char: 'k',
description: 'API Key of the source stack',
}),
data: flags.string({
description: 'path or location to store the data',
hidden: true,
parse: printFlagDeprecation(['--data'], ['--data-dir']),
}),
'data-dir': flags.string({
char: 'd',
description: 'The path or the location in your file system to store the exported content. For e.g., ./content',
}),
alias: flags.string({
char: 'a',
description: 'The management token alias of the source stack from which you will export content.',
}),
'management-token-alias': flags.string({
description: 'alias of the management token',
hidden: true,
parse: printFlagDeprecation(['--management-token-alias'], ['-a', '--alias']),
}),
'auth-token': flags.boolean({
char: 'A',
description: 'to use auth token',
hidden: true,
parse: printFlagDeprecation(['-A', '--auth-token']),
}),
module: flags.string({
char: 'm',
description:
'[optional] Specific module name. If not specified, the export command will export all the modules to the stack. The available modules are assets, content-types, entries, environments, extensions, marketplace-apps, global-fields, labels, locales, webhooks, workflows, custom-roles, and taxonomies.',
parse: printFlagDeprecation(['-m'], ['--module']),
}),
'content-types': flags.string({
char: 't',
description:
'[optional] The UID of the content type(s) whose content you want to export. In case of multiple content types, specify the IDs separated by spaces.',
multiple: true,
parse: printFlagDeprecation(['-t'], ['--content-types']),
}),
branch: flags.string({
char: 'B',
// default: 'main',
description:
"[optional] The name of the branch where you want to export your content. If you don't mention the branch name, then by default the content will be exported from all the branches of your stack.",
parse: printFlagDeprecation(['-B'], ['--branch']),
exclusive: ['branch-alias'],
}),
'branch-alias': flags.string({
description: '(Optional) The alias of the branch from which you want to export content.',
exclusive: ['branch'],
}),
'secured-assets': flags.boolean({
description: '[optional] Use this flag for assets that are secured.',
}),
yes: flags.boolean({
char: 'y',
required: false,
description: '[optional] Force override all Marketplace prompts.',
}),
query: flags.string({
description: '[optional] Query object (inline JSON or file path) to filter module exports.',
hidden: true,
}),
};
static aliases: string[] = ['cm:export'];
async run(): Promise<void> {
let exportDir: string = pathValidator('logs');
try {
const { flags } = await this.parse(ExportCommand);
const exportConfig = await setupExportConfig(flags);
// Prepare the context object
const context = this.createExportContext(exportConfig.apiKey, exportConfig.authenticationMethod);
exportConfig.context = { ...context };
//log.info(`Using Cli Version: ${this.context?.cliVersion}`, exportConfig.context);
// Assign exportConfig variables
this.assignExportConfig(exportConfig);
exportDir = sanitizePath(exportConfig.cliLogsPath || exportConfig.data || exportConfig.exportDir);
const managementAPIClient: ContentstackClient = await managementSDKClient(exportConfig);
const moduleExporter = new ModuleExporter(managementAPIClient, exportConfig);
await moduleExporter.start();
if (!exportConfig.branches?.length) {
writeExportMetaFile(exportConfig);
}
log.success(
`The content of the stack ${exportConfig.apiKey} has been exported successfully!`,
exportConfig.context,
);
log.info(`The exported content has been stored at '${exportDir}'`, exportConfig.context);
log.success(`The log has been stored at '${getLogPath()}'`, exportConfig.context);
} catch (error) {
handleAndLogError(error);
log.info(`The log has been stored at '${getLogPath()}'`);
}
}
// Create export context object
private createExportContext(apiKey: string, authenticationMethod?: string): Context {
return {
command: this.context?.info?.command || 'cm:stacks:export',
module: '',
userId: configHandler.get('userUid') || '',
email: configHandler.get('email') || '',
sessionId: this.context?.sessionId || '',
apiKey: apiKey || '',
orgId: configHandler.get('oauthOrgUid') || '',
authenticationMethod: authenticationMethod || 'Basic Auth',
};
}
// Assign values to exportConfig
private assignExportConfig(exportConfig: ExportConfig): void {
// Note setting host to create cma client
exportConfig.host = this.cmaHost;
exportConfig.region = this.region;
if (this.developerHubUrl) {
exportConfig.developerHubBaseUrl = this.developerHubUrl;
}
if (this.personalizeUrl) {
exportConfig.modules.personalize.baseURL[exportConfig.region.name] = this.personalizeUrl;
}
}
}