|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import * as os from 'os'; |
| 9 | +import { flags, FlagsConfig } from '@salesforce/command'; |
| 10 | +import { Messages, SfdxError } from '@salesforce/core'; |
| 11 | +import { Duration } from '@salesforce/kit'; |
| 12 | +import { RequestStatus } from '@salesforce/source-deploy-retrieve'; |
| 13 | +import { DeployCommand } from '../../../../deployCommand'; |
| 14 | +import { |
| 15 | + DeployCancelCommandResult, |
| 16 | + DeployCancelResultFormatter, |
| 17 | +} from '../../../../formatters/deployCancelResultFormatter'; |
| 18 | + |
| 19 | +Messages.importMessagesDirectory(__dirname); |
| 20 | +const messages = Messages.loadMessages('@salesforce/plugin-source', 'md.cancel'); |
| 21 | + |
| 22 | +export class Cancel extends DeployCommand { |
| 23 | + public static readonly description = messages.getMessage('description'); |
| 24 | + public static readonly examples = messages.getMessage('examples').split(os.EOL); |
| 25 | + public static readonly requiresUsername = true; |
| 26 | + public static readonly flagsConfig: FlagsConfig = { |
| 27 | + wait: flags.minutes({ |
| 28 | + char: 'w', |
| 29 | + default: Duration.minutes(DeployCommand.DEFAULT_WAIT_MINUTES), |
| 30 | + min: Duration.minutes(1), |
| 31 | + description: messages.getMessage('flags.wait'), |
| 32 | + longDescription: messages.getMessage('flags.waitLong'), |
| 33 | + }), |
| 34 | + jobid: flags.id({ |
| 35 | + char: 'i', |
| 36 | + description: messages.getMessage('flags.jobid'), |
| 37 | + validate: (val) => { |
| 38 | + if (val.startsWith('0Af')) { |
| 39 | + return true; |
| 40 | + } else { |
| 41 | + throw SfdxError.create('@salesforce/plugin-source', 'deploy', 'invalidDeployId'); |
| 42 | + } |
| 43 | + }, |
| 44 | + }), |
| 45 | + }; |
| 46 | + // The most important difference between this and source:deploy:cancel |
| 47 | + public isSourceStash = false; |
| 48 | + |
| 49 | + public async run(): Promise<DeployCancelCommandResult> { |
| 50 | + await this.cancel(); |
| 51 | + this.resolveSuccess(); |
| 52 | + return this.formatResult(); |
| 53 | + } |
| 54 | + |
| 55 | + protected async cancel(): Promise<void> { |
| 56 | + const deployId = this.resolveDeployId(this.getFlag<string>('jobid')); |
| 57 | + try { |
| 58 | + const deploy = this.createDeploy(deployId); |
| 59 | + await deploy.cancel(); |
| 60 | + |
| 61 | + this.deployResult = await this.poll(deployId); |
| 62 | + } catch (e) { |
| 63 | + if (e instanceof Error) { |
| 64 | + throw SfdxError.create('@salesforce/plugin-source', 'cancel', 'CancelFailed', [e.message]); |
| 65 | + } else { |
| 66 | + throw SfdxError.wrap(e); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + protected resolveSuccess(): void { |
| 72 | + const status = this.deployResult.response.status; |
| 73 | + if (status !== RequestStatus.Canceled) { |
| 74 | + this.setExitCode(1); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + protected formatResult(): DeployCancelCommandResult { |
| 79 | + const formatter = new DeployCancelResultFormatter(this.logger, this.ux, this.deployResult); |
| 80 | + if (!this.isJsonOutput()) { |
| 81 | + formatter.display(); |
| 82 | + } |
| 83 | + return formatter.getJson(); |
| 84 | + } |
| 85 | +} |
0 commit comments