-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcreate.ts
More file actions
295 lines (257 loc) · 8.58 KB
/
create.ts
File metadata and controls
295 lines (257 loc) · 8.58 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {Args, Flags, ux} from '@oclif/core';
import cliui from 'cliui';
import {MrtCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {createEnv, waitForEnv, type MrtEnvironment} from '@salesforce/b2c-tooling-sdk/operations/mrt';
import {t} from '../../../i18n/index.js';
/**
* Print environment details in a formatted table.
*/
function printEnvDetails(env: MrtEnvironment, project: string): void {
const ui = cliui({width: process.stdout.columns || 80});
const labelWidth = 18;
ui.div('');
ui.div({text: 'Slug:', width: labelWidth}, {text: env.slug ?? ''});
ui.div({text: 'Name:', width: labelWidth}, {text: env.name});
ui.div({text: 'Project:', width: labelWidth}, {text: project});
ui.div({text: 'State:', width: labelWidth}, {text: env.state ?? 'unknown'});
ui.div({text: 'Production:', width: labelWidth}, {text: env.is_production ? 'Yes' : 'No'});
if (env.ssr_region) {
ui.div({text: 'Region:', width: labelWidth}, {text: env.ssr_region});
}
if (env.hostname) {
ui.div({text: 'Hostname:', width: labelWidth}, {text: env.hostname});
}
if (env.ssr_external_hostname) {
ui.div({text: 'External Host:', width: labelWidth}, {text: env.ssr_external_hostname});
}
if (env.ssr_external_domain) {
ui.div({text: 'External Domain:', width: labelWidth}, {text: env.ssr_external_domain});
}
if (env.allow_cookies) {
ui.div({text: 'Allow Cookies:', width: labelWidth}, {text: 'Yes'});
}
if (env.enable_source_maps) {
ui.div({text: 'Source Maps:', width: labelWidth}, {text: 'Yes'});
}
if (env.log_level) {
ui.div({text: 'Log Level:', width: labelWidth}, {text: env.log_level});
}
if (env.ssr_proxy_configs && env.ssr_proxy_configs.length > 0) {
ui.div({text: 'Proxies:', width: labelWidth}, {text: ''});
for (const proxy of env.ssr_proxy_configs) {
const proxyPath = (proxy as {path?: string}).path ?? '';
ui.div({text: '', width: labelWidth}, {text: ` ${proxyPath} → ${proxy.host}`});
}
}
ux.stdout(ui.toString());
}
/**
* Proxy configuration for SSR.
*/
interface SsrProxyConfig {
host: string;
path: string;
}
/**
* Parse a proxy string in format "path=host" into a proxy config object.
*/
function parseProxyString(proxyStr: string): SsrProxyConfig {
const eqIndex = proxyStr.indexOf('=');
if (eqIndex === -1) {
throw new Error(`Invalid proxy format: "${proxyStr}". Expected format: path=host.example.com`);
}
const path = proxyStr.slice(0, eqIndex);
const host = proxyStr.slice(eqIndex + 1);
if (!path) {
throw new Error(`Invalid proxy format: "${proxyStr}". Path cannot be empty.`);
}
if (!host) {
throw new Error(`Invalid proxy format: "${proxyStr}". Host cannot be empty.`);
}
return {path, host};
}
/**
* Valid AWS regions for MRT environments.
*/
const SSR_REGIONS = [
'us-east-1',
'us-east-2',
'us-west-1',
'us-west-2',
'ap-south-1',
'ap-south-2',
'ap-northeast-2',
'ap-southeast-1',
'ap-southeast-2',
'ap-southeast-3',
'ap-northeast-1',
'ap-northeast-3',
'ca-central-1',
'eu-central-1',
'eu-central-2',
'eu-west-1',
'eu-west-2',
'eu-west-3',
'eu-north-1',
'eu-south-1',
'il-central-1',
'me-central-1',
'sa-east-1',
] as const;
type SsrRegion = (typeof SSR_REGIONS)[number];
/**
* Create a new environment (target) in a Managed Runtime project.
*/
export default class MrtEnvCreate extends MrtCommand<typeof MrtEnvCreate> {
static args = {
slug: Args.string({
description: 'Environment slug/identifier (e.g., staging, production)',
required: true,
}),
};
static description = t('commands.mrt.env.create.description', 'Create a new Managed Runtime environment');
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> staging --project my-storefront',
'<%= config.bin %> <%= command.id %> staging --project my-storefront --name "Staging Environment"',
'<%= config.bin %> <%= command.id %> production --project my-storefront --production',
'<%= config.bin %> <%= command.id %> feature-test -p my-storefront --region eu-west-1',
'<%= config.bin %> <%= command.id %> staging -p my-storefront --proxy api=api.example.com --proxy ocapi=ocapi.example.com',
'<%= config.bin %> <%= command.id %> staging -p my-storefront --wait',
];
static flags = {
...MrtCommand.baseFlags,
name: Flags.string({
char: 'n',
description: 'Display name for the environment (defaults to slug)',
}),
region: Flags.string({
char: 'r',
description: 'AWS region for SSR deployment',
options: SSR_REGIONS as unknown as string[],
}),
production: Flags.boolean({
description: 'Mark as a production environment',
default: false,
}),
hostname: Flags.string({
description: 'Hostname pattern for V8 Tag loading',
}),
'external-hostname': Flags.string({
description: 'Full external hostname (e.g., www.example.com)',
}),
'external-domain': Flags.string({
description: 'External domain for Universal PWA SSR (e.g., example.com)',
}),
'allow-cookies': Flags.boolean({
description: 'Forward HTTP cookies to origin',
default: false,
allowNo: true,
}),
'enable-source-maps': Flags.boolean({
description: 'Enable source map support in the environment',
default: false,
allowNo: true,
}),
proxy: Flags.string({
description: 'Proxy configuration in format path=host (can be specified multiple times)',
multiple: true,
}),
wait: Flags.boolean({
char: 'w',
description: 'Wait for the environment to be ready before returning',
default: false,
}),
};
async run(): Promise<MrtEnvironment> {
this.requireMrtCredentials();
const {slug} = this.args;
const {mrtProject: project} = this.resolvedConfig;
if (!project) {
this.error(
'MRT project is required. Provide --project flag, set SFCC_MRT_PROJECT, or set mrtProject in dw.json.',
);
}
const {
name: nameFlag,
region,
production: isProduction,
hostname,
'external-hostname': externalHostname,
'external-domain': externalDomain,
'allow-cookies': allowCookies,
'enable-source-maps': enableSourceMaps,
proxy: proxyStrings,
wait,
} = this.flags;
// Default name to slug if not provided
const name = nameFlag ?? slug;
// Parse proxy configurations
const proxyConfigs = proxyStrings?.map((p) => parseProxyString(p));
this.log(
t('commands.mrt.env.create.creating', 'Creating environment "{{slug}}" in {{project}}...', {slug, project}),
);
try {
let result = await createEnv(
{
projectSlug: project,
slug,
name,
region: region as SsrRegion | undefined,
isProduction,
hostname,
externalHostname,
externalDomain,
allowCookies: allowCookies || undefined,
enableSourceMaps: enableSourceMaps || undefined,
proxyConfigs,
origin: this.resolvedConfig.mrtOrigin,
},
this.getMrtAuth(),
);
// Wait for environment to be ready if requested
if (wait) {
this.log(t('commands.mrt.env.create.waiting', 'Waiting for environment "{{slug}}" to be ready...', {slug}));
const waitStartTime = Date.now();
result = await waitForEnv(
{
projectSlug: project,
slug,
origin: this.resolvedConfig.mrtOrigin,
onPoll: (env) => {
if (!this.jsonEnabled()) {
const elapsed = Math.round((Date.now() - waitStartTime) / 1000);
this.log(
t('commands.mrt.env.create.state', '[{{elapsed}}s] State: {{state}}', {
elapsed: String(elapsed),
state: env.state ?? 'unknown',
}),
);
}
},
},
this.getMrtAuth(),
);
}
if (this.jsonEnabled()) {
return result;
}
// Human-readable output
this.log(t('commands.mrt.env.create.success', 'Environment created successfully.'));
printEnvDetails(result, project);
return result;
} catch (error) {
if (error instanceof Error) {
this.error(
t('commands.mrt.env.create.failed', 'Failed to create environment: {{message}}', {message: error.message}),
);
}
throw error;
}
}
}