-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcreate.ts
More file actions
127 lines (110 loc) · 4.12 KB
/
create.ts
File metadata and controls
127 lines (110 loc) · 4.12 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
/*
* 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} from '@oclif/core';
import {OdsCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {getApiErrorMessage, type OdsComponents} from '@salesforce/b2c-tooling-sdk';
import {t, withDocs} from '../../../i18n/index.js';
import open from 'open';
type SandboxAliasModel = OdsComponents['schemas']['SandboxAliasModel'];
/**
* Command to create a sandbox alias.
*/
export default class SandboxAliasCreate extends OdsCommand<typeof SandboxAliasCreate> {
static aliases = ['ods:alias:create'];
static args = {
sandboxId: Args.string({
description: 'Sandbox ID (UUID or realm-instance, e.g., abcd-123)',
required: true,
}),
hostname: Args.string({
description: 'Hostname alias to register (e.g., my-store.example.com)',
required: true,
}),
};
static description = withDocs(
t('commands.sandbox.alias.create.description', 'Create a hostname alias for a sandbox'),
'/cli/sandbox.html#b2c-sandbox-alias-create',
);
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> zzzv-123 my-store.example.com',
'<%= config.bin %> <%= command.id %> zzzv-123 secure-store.example.com --unique',
'<%= config.bin %> <%= command.id %> zzzv-123 secure-store.example.com --unique --letsencrypt',
'<%= config.bin %> <%= command.id %> zzzv-123 my-store.example.com --json',
];
static flags = {
unique: Flags.boolean({
char: 'u',
description: "Make the alias unique (required for Let's Encrypt certificates)",
default: false,
}),
letsencrypt: Flags.boolean({
description: "Request a Let's Encrypt certificate for this alias (requires --unique)",
default: false,
dependsOn: ['unique'],
}),
'no-open': Flags.boolean({
description: 'Do not open registration URL in browser (for non-unique aliases)',
default: false,
}),
};
async run(): Promise<SandboxAliasModel> {
const {sandboxId, hostname} = this.args;
const {unique, letsencrypt, 'no-open': noOpen} = this.flags;
const resolvedSandboxId = await this.resolveSandboxId(sandboxId);
this.log(
t('commands.sandbox.alias.create.creating', 'Creating alias {{hostname}} for sandbox {{sandboxId}}...', {
hostname,
sandboxId,
}),
);
const result = await this.odsClient.POST('/sandboxes/{sandboxId}/aliases', {
params: {
path: {sandboxId: resolvedSandboxId},
},
body: {
name: hostname,
unique,
requestLetsEncryptCertificate: letsencrypt,
},
});
if (!result.data?.data) {
const message = getApiErrorMessage(result.error, result.response);
this.error(
t('commands.sandbox.alias.create.error', 'Failed to create alias: {{message}}', {
message,
}),
);
}
const alias = result.data.data as SandboxAliasModel;
if (!this.jsonEnabled()) {
this.log(t('commands.sandbox.alias.create.success', 'Alias created successfully'));
this.log('');
this.log(`ID: ${alias.id}`);
this.log(`Hostname: ${alias.name}`);
this.log(`Status: ${alias.status}`);
if (unique && alias.domainVerificationRecord) {
this.log('');
this.log(t('commands.sandbox.alias.create.verification', '⚠️ DNS Verification Required:'));
this.log(
t(
'commands.sandbox.alias.create.verification_instructions',
'Add this TXT record to your DNS configuration:',
),
);
this.log(` ${alias.domainVerificationRecord}`);
this.log('');
this.log(t('commands.sandbox.alias.create.verification_wait', 'The alias will activate after DNS propagation'));
}
if (!unique && alias.registration && !noOpen) {
this.log('');
this.log(t('commands.sandbox.alias.create.registration', 'Opening alias registration in browser...'));
await open(alias.registration);
}
}
return alias;
}
}