generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcancel.ts
114 lines (105 loc) · 4.64 KB
/
cancel.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
/*
* Copyright (c) 2022, 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 { Messages } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { RequestStatus } from '@salesforce/source-deploy-retrieve';
import { cancelDeploy, cancelDeployAsync } from '../../../utils/deploy.js';
import { DeployCache } from '../../../utils/deployCache.js';
import { AsyncDeployCancelResultFormatter } from '../../../formatters/asyncDeployCancelResultFormatter.js';
import { DeployCancelResultFormatter } from '../../../formatters/deployCancelResultFormatter.js';
import { DeployResultJson } from '../../../utils/types.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-deploy-retrieve', 'deploy.metadata.cancel');
export default class DeployMetadataCancel extends SfCommand<DeployResultJson> {
public static readonly description = messages.getMessage('description');
public static readonly summary = messages.getMessage('summary');
public static readonly examples = messages.getMessages('examples');
public static readonly aliases = ['deploy:metadata:cancel'];
public static readonly deprecateAliases = true;
public static readonly flags = {
'target-org': Flags.optionalOrg(),
async: Flags.boolean({
summary: messages.getMessage('flags.async.summary'),
description: messages.getMessage('flags.async.description'),
exclusive: ['wait'],
}),
'job-id': Flags.salesforceId({
char: 'i',
startsWith: '0Af',
length: 'both',
description: messages.getMessage('flags.job-id.description'),
summary: messages.getMessage('flags.job-id.summary'),
exactlyOne: ['use-most-recent', 'job-id'],
}),
'use-most-recent': Flags.boolean({
char: 'r',
description: messages.getMessage('flags.use-most-recent.description'),
summary: messages.getMessage('flags.use-most-recent.summary'),
exactlyOne: ['use-most-recent', 'job-id'],
}),
// we want to allow undefined to use the value from the cache
// eslint-disable-next-line sf-plugin/flag-min-max-default
wait: Flags.duration({
char: 'w',
summary: messages.getMessage('flags.wait.summary'),
description: messages.getMessage('flags.wait.description'),
unit: 'minutes',
helpValue: '<minutes>',
min: 1,
exclusive: ['async'],
}),
};
public async run(): Promise<DeployResultJson> {
const [{ flags }, cache] = await Promise.all([this.parse(DeployMetadataCancel), DeployCache.create()]);
const jobId = cache.resolveLatest(flags['use-most-recent'], flags['job-id']);
// cancel don't care about your tracking conflicts
const deployOpts = {
'target-org': flags['target-org']?.getUsername(),
...cache.maybeGet(jobId),
'ignore-conflicts': true,
};
// we may already know the job finished
if (
deployOpts.status &&
[RequestStatus.Canceled, RequestStatus.Failed, RequestStatus.Succeeded, RequestStatus.SucceededPartial].includes(
deployOpts.status
)
) {
messages.createError('error.CannotCancelDeployPre', [jobId, deployOpts.status]);
}
if (flags.async) {
const asyncResult = await cancelDeployAsync({ 'target-org': deployOpts['target-org'] }, jobId);
const formatter = new AsyncDeployCancelResultFormatter(asyncResult.id);
if (!this.jsonEnabled()) formatter.display();
return formatter.getJson();
} else {
const wait = flags.wait ?? Duration.minutes(deployOpts.wait ?? 33);
const result = await cancelDeploy({ ...deployOpts, wait }, jobId);
const formatter = new DeployCancelResultFormatter(result);
if (!this.jsonEnabled()) formatter.display();
cache.update(result.response.id, { status: result.response.status });
await cache.write();
if ([RequestStatus.Succeeded, RequestStatus.SucceededPartial].includes(result.response.status)) {
throw messages.createError('error.CannotCancelDeploy');
}
return formatter.getJson();
}
}
protected catch(error: SfCommand.Error): Promise<never> {
if (error.name.includes('INVALID_ID_FIELD')) {
const err = messages.createError('error.CannotCancelDeploy');
return super.catch({
...error,
name: err.name,
message: err.message,
...(typeof err.code === 'string' || typeof err.code === 'number' ? { code: err.code } : {}),
});
}
return super.catch(error);
}
}