generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 12
/
scratch.ts
86 lines (75 loc) · 3.19 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
/*
* 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,
scratchOrgResume,
SfError,
} from '@salesforce/core';
import { ScratchCreateResponse } from '../../../shared/orgTypes.js';
import { buildStatus } from '../../../shared/scratchOrgOutput.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 hubBaseUrl = cache.get(jobId)?.hubBaseUrl;
let lastStatus: string | undefined;
lifecycle.on<ScratchOrgLifecycleEvent>(scratchOrgLifecycleEventName, async (data): Promise<void> => {
lastStatus = buildStatus(data, hubBaseUrl);
this.spinner.status = lastStatus;
return Promise.resolve();
});
this.log();
this.spinner.start('Creating Scratch Org');
try {
const { username, scratchOrgInfo, authFields, warnings } = await scratchOrgResume(jobId);
this.spinner.stop(lastStatus);
this.log();
this.logSuccess(messages.getMessage('success'));
return { username, scratchOrgInfo, authFields, warnings, orgId: authFields?.orgId };
} catch (e) {
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);
}
}
}
}