generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
scratch.ts
73 lines (67 loc) · 2.78 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
/*
* 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 { AuthInfo, AuthRemover, Messages, Org } from '@salesforce/core';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import { ensureString } from '@salesforce/ts-types';
import { orgThatMightBeDeleted } from '../../../shared/flags.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-org', 'delete_scratch');
export type ScratchDeleteResponse = {
orgId: string;
username: string;
};
export default class DeleteScratch extends SfCommand<ScratchDeleteResponse> {
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:delete:scratch'];
public static readonly deprecateAliases = true;
public static readonly flags = {
'target-org': orgThatMightBeDeleted({
summary: messages.getMessage('flags.target-org.summary'),
required: true,
}),
'no-prompt': Flags.boolean({
char: 'p',
summary: messages.getMessage('flags.no-prompt.summary'),
}),
};
public async run(): Promise<ScratchDeleteResponse> {
const flags = (await this.parse(DeleteScratch)).flags;
const resolvedUsername = flags['target-org'];
const { orgId, isScratch } = (await AuthInfo.create({ username: resolvedUsername })).getFields();
if (!isScratch) {
throw messages.createError('error.unknownScratch', [resolvedUsername]);
}
if (
flags['no-prompt'] ||
(await this.confirm({ message: messages.getMessage('prompt.confirm', [resolvedUsername]) }))
) {
try {
const org = await Org.create({ aliasOrUsername: resolvedUsername });
await org.delete();
return { username: org.getUsername() as string, orgId: org.getOrgId() };
} catch (e) {
if (e instanceof Error && e.name === 'DomainNotFoundError') {
// the org has expired, so remote operations won't work
// let's clean up the files locally
const authRemover = await AuthRemover.create();
await authRemover.removeAuth(resolvedUsername);
this.logSuccess(messages.getMessage('success', [resolvedUsername]));
} else if (e instanceof Error && e.name === 'ScratchOrgNotFound') {
this.logSuccess(messages.getMessage('success.Idempotent', [resolvedUsername]));
} else {
throw e;
}
}
}
return {
username: resolvedUsername,
orgId: ensureString(orgId),
};
}
}