generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 18
/
cancel.ts
74 lines (66 loc) · 2.46 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
/*
* 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 * as os from 'os';
import { flags, FlagsConfig } from '@salesforce/command';
import { Messages, SfError } from '@salesforce/core';
import { Duration } from '@salesforce/kit';
import { RequestStatus } from '@salesforce/source-deploy-retrieve';
import { DeployCommand } from '../../../../deployCommand';
import {
DeployCancelCommandResult,
DeployCancelResultFormatter,
} from '../../../../formatters/deployCancelResultFormatter';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-source', 'cancel');
export class Cancel extends DeployCommand {
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessage('examples').split(os.EOL);
public static readonly requiresUsername = true;
public static readonly flagsConfig: FlagsConfig = {
wait: flags.minutes({
char: 'w',
default: Duration.minutes(DeployCommand.DEFAULT_WAIT_MINUTES),
min: Duration.minutes(1),
description: messages.getMessage('flags.wait'),
longDescription: messages.getMessage('flagsLong.wait'),
}),
jobid: flags.id({
char: 'i',
description: messages.getMessage('flags.jobid'),
validate: DeployCommand.isValidDeployId,
}),
};
public async run(): Promise<DeployCancelCommandResult> {
await this.cancel();
this.resolveSuccess();
return this.formatResult();
}
protected async cancel(): Promise<void> {
const deployId = this.resolveDeployId(this.getFlag<string>('jobid'));
try {
const deploy = this.createDeploy(deployId);
await deploy.cancel();
this.deployResult = await this.poll(deployId);
} catch (e) {
const err = e as Error;
throw new SfError(messages.getMessage('CancelFailed', [err.message]), 'CancelFailed');
}
}
protected resolveSuccess(): void {
const status = this.deployResult.response.status;
if (status !== RequestStatus.Canceled) {
this.setExitCode(1);
}
}
protected formatResult(): DeployCancelCommandResult {
const formatter = new DeployCancelResultFormatter(this.logger, this.ux, this.deployResult);
if (!this.isJsonOutput()) {
formatter.display();
}
return formatter.getJson();
}
}