/
bootstrap.ts
197 lines (180 loc) · 6.7 KB
/
bootstrap.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { Command } from '@contentstack/cli-command';
import { resolve } from 'path';
import Bootstrap, { BootstrapOptions, SeedParams } from '../../bootstrap';
import {
inquireCloneDirectory,
inquireApp,
inquireAppType,
inquireLivePreviewSupport,
} from '../../bootstrap/interactive';
import {
printFlagDeprecation,
managementSDKClient,
flags,
isAuthenticated,
FlagInput,
configHandler,
} from '@contentstack/cli-utilities';
import config, { getAppLevelConfigByName, AppConfig } from '../../config';
import messageHandler from '../../messages';
export default class BootstrapCommand extends Command {
private bootstrapManagementAPIClient: any;
static description = 'Bootstrap contentstack apps';
static examples = [
'$ csdx cm:bootstrap',
'$ csdx cm:bootstrap --project-dir <path/to/setup/the/app>',
'$ csdx cm:bootstrap --app-name "reactjs-starter" --project-dir <path/to/setup/the/app>',
'$ csdx cm:bootstrap --app-name "reactjs-starter" --project-dir <path/to/setup/the/app> --stack-api-key "stack-api-key"',
'$ csdx cm:bootstrap --app-name "reactjs-starter" --project-dir <path/to/setup/the/app> --org "your-org-uid" --stack-name "stack-name"',
];
static flags: FlagInput = {
'app-name': flags.string({
description:
'App name, reactjs-starter, nextjs-starter, gatsby-starter, angular-starter, nuxt-starter, vue-starter, stencil-starter',
multiple: false,
required: false,
}),
'project-dir': flags.string({
description:
'Directory to setup the project. If directory name has a space then provide the path as a string or escap the space using back slash eg: "../../test space" or ../../test\\ space',
multiple: false,
required: false,
}),
'app-type': flags.string({
description: 'Sample or Starter app',
multiple: false,
required: false,
hidden: true,
}),
'stack-api-key': flags.string({
char: 'k',
description: 'Provide stack API key to seed content',
multiple: false,
required: false,
exclusive: ['org', 'stack-name'],
}),
org: flags.string({
description: 'Provide organization UID to create a new stack',
multiple: false,
required: false,
exclusive: ['stack-api-key'],
}),
'stack-name': flags.string({
char: 'n',
description: 'Name of a new stack that will be created.',
multiple: false,
required: false,
exclusive: ['stack-api-key'],
}),
yes: flags.string({
description: '[Optional] Skip stack confirmation',
char: 'y',
required: false,
}),
// To be deprecated
appName: flags.string({
char: 'a',
description: 'App name, reactjs-starter, nextjs-starter, gatsby-starter, angular-starter, nuxt-starter',
multiple: false,
required: false,
hidden: true,
parse: printFlagDeprecation(['-a', '--appName'], ['--app-name']),
}),
directory: flags.string({
char: 'd',
description:
'Directory to set up the project. If directory name has a space then provide the path as a string or escape the space using backslash eg: "../../test space" or ../../test\\ space',
multiple: false,
required: false,
hidden: true,
parse: printFlagDeprecation(['-d', '--directory'], ['--project-dir']),
}),
appType: flags.string({
char: 's',
description: 'Sample or Starter app',
multiple: false,
required: false,
hidden: true,
parse: printFlagDeprecation(['-s', '--appType'], ['--app-type']),
}),
alias: flags.string({
char: 'a',
description: 'Alias of the management token',
}),
};
async run() {
const { flags: bootstrapCommandFlags } = await this.parse(BootstrapCommand);
const managementTokenAlias = bootstrapCommandFlags.alias;
try {
if (!isAuthenticated() && !managementTokenAlias) {
this.error(messageHandler.parse('CLI_BOOTSTRAP_LOGIN_FAILED'), {
exit: 2,
suggestions: ['https://www.contentstack.com/docs/developers/cli/authentication/'],
});
}
this.bootstrapManagementAPIClient = await managementSDKClient({
host: this.cmaHost,
});
// inquire user inputs
let appType =
(bootstrapCommandFlags.appType as string) || (bootstrapCommandFlags['app-type'] as string) || 'starterapp';
if (!appType) {
appType = await inquireAppType();
}
const selectedAppName =
(bootstrapCommandFlags.appName as string) || (bootstrapCommandFlags['app-name'] as string);
let selectedApp;
if (!selectedAppName) {
if (appType === 'sampleapp') {
selectedApp = await inquireApp(config.sampleApps);
} else if (appType === 'starterapp') {
selectedApp = await inquireApp(config.starterApps);
} else {
this.error('Invalid app type provided ' + appType, { exit: 1 });
}
}
if (!selectedAppName && !selectedApp) {
this.error(messageHandler.parse('CLI_BOOTSTRAP_INVALID_APP_NAME'), {
exit: 1,
});
}
const yes = bootstrapCommandFlags.yes as string;
const appConfig: AppConfig = getAppLevelConfigByName(selectedAppName || selectedApp.configKey);
let cloneDirectory =
(bootstrapCommandFlags.directory as string) || (bootstrapCommandFlags['project-dir'] as string);
if (!cloneDirectory) {
cloneDirectory = await inquireCloneDirectory();
}
cloneDirectory = resolve(cloneDirectory);
const livePreviewEnabled = bootstrapCommandFlags.yes ? true : await inquireLivePreviewSupport();
const seedParams: SeedParams = {};
const stackAPIKey = bootstrapCommandFlags['stack-api-key'];
const org = bootstrapCommandFlags.org;
const stackName = bootstrapCommandFlags['stack-name'];
if (stackAPIKey) seedParams.stackAPIKey = stackAPIKey;
if (org) seedParams.org = org;
if (stackName) seedParams.stackName = stackName;
if (yes) seedParams.yes = yes;
if (managementTokenAlias) {
seedParams.managementTokenAlias = managementTokenAlias;
const listOfTokens = configHandler.get('tokens');
const managementToken = listOfTokens[managementTokenAlias].token;
seedParams.managementToken = managementToken;
}
// initiate bootstrsourceap
const options: BootstrapOptions = {
appConfig,
seedParams,
cloneDirectory,
managementAPIClient: this.bootstrapManagementAPIClient,
region: this.region,
appType,
livePreviewEnabled,
};
const bootstrap = new Bootstrap(options);
await bootstrap.run();
} catch (error: any) {
this.error(error, { exit: 1, suggestions: error.suggestions });
}
}
}