generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
scratch.ts
249 lines (235 loc) · 8.6 KB
/
scratch.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
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
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { MultiStageOutput } from '@oclif/multi-stage-output';
import {
Lifecycle,
Messages,
Org,
scratchOrgCreate,
ScratchOrgLifecycleEvent,
scratchOrgLifecycleEventName,
scratchOrgLifecycleStages,
SfError,
} from '@salesforce/core';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import { Duration } from '@salesforce/kit';
import terminalLink from 'terminal-link';
import { capitalCase } from 'change-case';
import { buildScratchOrgRequest } from '../../../shared/scratchOrgRequest.js';
import { ScratchCreateResponse } from '../../../shared/orgTypes.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-org', 'create_scratch');
const definitionFileHelpGroupName = 'Definition File Override';
export default class OrgCreateScratch extends SfCommand<ScratchCreateResponse> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly aliases = ['env:create:scratch'];
public static readonly deprecateAliases = true;
public static readonly flags = {
alias: Flags.string({
char: 'a',
summary: messages.getMessage('flags.alias.summary'),
description: messages.getMessage('flags.alias.description'),
}),
async: Flags.boolean({
summary: messages.getMessage('flags.async.summary'),
description: messages.getMessage('flags.async.description'),
}),
'set-default': Flags.boolean({
char: 'd',
summary: messages.getMessage('flags.set-default.summary'),
}),
'definition-file': Flags.file({
exists: true,
char: 'f',
summary: messages.getMessage('flags.definition-file.summary'),
description: messages.getMessage('flags.definition-file.description'),
}),
'target-dev-hub': Flags.requiredHub({
char: 'v',
summary: messages.getMessage('flags.target-dev-hub.summary'),
description: messages.getMessage('flags.target-dev-hub.description'),
required: true,
}),
'no-ancestors': Flags.boolean({
char: 'c',
summary: messages.getMessage('flags.no-ancestors.summary'),
helpGroup: 'Packaging',
}),
edition: Flags.string({
char: 'e',
summary: messages.getMessage('flags.edition.summary'),
description: messages.getMessage('flags.edition.description'),
options: [
'developer',
'enterprise',
'group',
'professional',
'partner-developer',
'partner-enterprise',
'partner-group',
'partner-professional',
],
// eslint-disable-next-line @typescript-eslint/require-await
parse: async (value: string) => {
// the API expects partner editions in `partner <EDITION>` format.
// so we replace the hyphen here with a space.
if (value.startsWith('partner-')) {
return value.replace('-', ' ');
}
return value;
},
helpGroup: definitionFileHelpGroupName,
}),
'no-namespace': Flags.boolean({
char: 'm',
summary: messages.getMessage('flags.no-namespace.summary'),
helpGroup: 'Packaging',
}),
'duration-days': Flags.duration({
unit: 'days',
default: Duration.days(7),
min: 1,
max: 30,
char: 'y',
helpValue: '<days>',
summary: messages.getMessage('flags.duration-days.summary'),
}),
wait: Flags.duration({
unit: 'minutes',
default: Duration.minutes(5),
min: 2,
char: 'w',
helpValue: '<minutes>',
summary: messages.getMessage('flags.wait.summary'),
description: messages.getMessage('flags.wait.description'),
}),
'api-version': Flags.orgApiVersion(),
'client-id': Flags.string({
char: 'i',
summary: messages.getMessage('flags.client-id.summary'),
}),
'track-source': Flags.boolean({
default: true,
char: 't',
summary: messages.getMessage('flags.track-source.summary'),
description: messages.getMessage('flags.track-source.description'),
allowNo: true,
}),
username: Flags.string({
summary: messages.getMessage('flags.username.summary'),
description: messages.getMessage('flags.username.description'),
helpGroup: definitionFileHelpGroupName,
}),
description: Flags.string({
summary: messages.getMessage('flags.description.summary'),
helpGroup: definitionFileHelpGroupName,
}),
name: Flags.string({
summary: messages.getMessage('flags.name.summary'),
helpGroup: definitionFileHelpGroupName,
}),
release: Flags.string({
summary: messages.getMessage('flags.release.summary'),
description: messages.getMessage('flags.release.description'),
options: ['preview', 'previous'],
helpGroup: definitionFileHelpGroupName,
}),
'admin-email': Flags.string({
summary: messages.getMessage('flags.admin-email.summary'),
helpGroup: definitionFileHelpGroupName,
}),
'source-org': Flags.salesforceId({
summary: messages.getMessage('flags.source-org.summary'),
startsWith: '00D',
length: 15,
helpGroup: definitionFileHelpGroupName,
// salesforceId flag has `i` and that would be a conflict with client-id
char: undefined,
}),
};
public async run(): Promise<ScratchCreateResponse> {
const lifecycle = Lifecycle.getInstance();
const { flags } = await this.parse(OrgCreateScratch);
const baseUrl = flags['target-dev-hub'].getField(Org.Fields.INSTANCE_URL)?.toString();
if (!baseUrl) {
throw new SfError('No instance URL found for the dev hub');
}
const createCommandOptions = await buildScratchOrgRequest(
flags,
flags['client-id'] ? await this.secretPrompt({ message: messages.getMessage('prompt.secret') }) : undefined
);
const mso = new MultiStageOutput<ScratchOrgLifecycleEvent & { alias: string | undefined }>({
stages: (flags.async ? ['prepare request', 'send request', 'done'] : scratchOrgLifecycleStages).map((stage) =>
capitalCase(stage)
),
title: flags.async ? 'Creating Scratch Org (async)' : 'Creating Scratch Org',
data: { alias: flags.alias },
jsonEnabled: this.jsonEnabled(),
postStagesBlock: [
{
label: 'Request Id',
type: 'dynamic-key-value',
get: (data) =>
data?.scratchOrgInfo?.Id && terminalLink(data.scratchOrgInfo.Id, `${baseUrl}/${data.scratchOrgInfo.Id}`),
bold: true,
},
{
label: 'OrgId',
type: 'dynamic-key-value',
get: (data) => data?.scratchOrgInfo?.ScratchOrg,
bold: true,
color: 'cyan',
},
{
label: 'Username',
type: 'dynamic-key-value',
get: (data) => data?.scratchOrgInfo?.SignupUsername,
bold: true,
color: 'cyan',
},
{
label: 'Alias',
type: 'static-key-value',
get: (data) => data?.alias,
},
],
});
lifecycle.on<ScratchOrgLifecycleEvent>(scratchOrgLifecycleEventName, async (data): Promise<void> => {
mso.skipTo(capitalCase(data.stage), data);
if (data.stage === 'done') {
mso.stop();
}
return Promise.resolve();
});
try {
const { username, scratchOrgInfo, authFields, warnings } = await scratchOrgCreate(createCommandOptions);
if (!scratchOrgInfo) {
throw new SfError('The scratch org did not return with any information');
}
if (flags.async) {
mso.skipTo('Done', { scratchOrgInfo });
mso.stop();
this.info(messages.getMessage('action.resume', [this.config.bin, scratchOrgInfo.Id]));
} else {
this.logSuccess(messages.getMessage('success'));
}
return { username, scratchOrgInfo, authFields, warnings, orgId: authFields?.orgId };
} catch (error) {
mso.error();
if (error instanceof SfError && error.name === 'ScratchOrgInfoTimeoutError') {
const scratchOrgInfoId = (error.data as { scratchOrgInfoId: string }).scratchOrgInfoId;
const resumeMessage = messages.getMessage('action.resume', [this.config.bin, scratchOrgInfoId]);
this.info(resumeMessage);
this.error('The scratch org did not complete within your wait time', { code: '69', exit: 69 });
} else {
throw error;
}
}
}
}