generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscratch.ts
123 lines (112 loc) · 4.3 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
/*
* Copyright (c) 2020, 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 { strict as assert } from 'node:assert';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import {
Lifecycle,
Messages,
ScratchOrgCache,
ScratchOrgLifecycleEvent,
scratchOrgLifecycleEventName,
scratchOrgLifecycleStages,
scratchOrgResume,
SfError,
} from '@salesforce/core';
import terminalLink from 'terminal-link';
import { MultiStageOutput } from '@oclif/multi-stage-output';
import { capitalCase } from 'change-case';
import { ScratchCreateResponse } from '../../../shared/orgTypes.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-org', 'resume_scratch');
export default class OrgResumeScratch 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:resume:scratch'];
public static readonly deprecateAliases = true;
public static readonly flags = {
'job-id': Flags.salesforceId({
char: 'i',
length: 'both',
summary: messages.getMessage('flags.job-id.summary'),
description: messages.getMessage('flags.job-id.description'),
exactlyOne: ['use-most-recent', 'job-id'],
startsWith: '2SR',
}),
'use-most-recent': Flags.boolean({
char: 'r',
summary: messages.getMessage('flags.use-most-recent.summary'),
exactlyOne: ['use-most-recent', 'job-id'],
}),
};
public async run(): Promise<ScratchCreateResponse> {
const { flags } = await this.parse(OrgResumeScratch);
const cache = await ScratchOrgCache.create();
const lifecycle = Lifecycle.getInstance();
const jobId = flags['use-most-recent'] ? cache.getLatestKey() : flags['job-id'];
if (!jobId && flags['use-most-recent']) throw messages.createError('error.NoRecentJobId');
// oclif doesn't know that the exactlyOne flag will ensure that one of these is set, and there we definitely have a jobID.
assert(jobId);
const cached = cache.get(jobId);
const hubBaseUrl = cached?.hubBaseUrl;
const mso = new MultiStageOutput<ScratchOrgLifecycleEvent & { alias: string | undefined }>({
stages: scratchOrgLifecycleStages.map((stage) => capitalCase(stage)),
title: 'Resuming Scratch Org',
data: { alias: cached?.alias },
jsonEnabled: this.jsonEnabled(),
postStagesBlock: [
{
label: 'Request Id',
type: 'dynamic-key-value',
get: (data) =>
data?.scratchOrgInfo?.Id && terminalLink(data.scratchOrgInfo.Id, `${hubBaseUrl}/${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 scratchOrgResume(jobId);
this.log();
this.logSuccess(messages.getMessage('success'));
return { username, scratchOrgInfo, authFields, warnings, orgId: authFields?.orgId };
} catch (e) {
mso.error();
if (cache.keys() && e instanceof Error && e.name === 'CacheMissError') {
// we have something in the cache, but it didn't match what the user passed in
throw messages.createError('error.jobIdMismatch', [jobId]);
} else {
throw SfError.wrap(e);
}
}
}
}