All files / src/commands/cancel-deploy cancel-deploy.command.js

63.33% Statements 19/30
100% Branches 1/1
0% Functions 0/1
63.33% Lines 19/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 31193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                       193x 193x  
import { cancelDeployment, getAllDeployments } from '@clevercloud/client/esm/api/v2/application.js';
import { defineCommand } from '../../lib/define-command.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import * as Application from '../../models/application.js';
import { sendToApi } from '../../models/send-to-api.js';
import { aliasOption, appIdOrNameOption } from '../global.options.js';
 
export const cancelDeployCommand = defineCommand({
  description: 'Cancel an ongoing deployment',
  since: '0.2.0',
  options: {
    alias: aliasOption,
    app: appIdOrNameOption,
  },
  args: [],
  async handler(options) {
    const { alias, app: appIdOrName } = options;
    const { ownerId, appId } = await Application.resolveId(appIdOrName, alias);

    const deployments = await getAllDeployments({ id: ownerId, appId, limit: 1 }).then(sendToApi);

    if (deployments.length === 0 || deployments[0].action !== 'DEPLOY' || deployments[0].state !== 'WIP') {
      throw new Error('There is no ongoing deployment for this application');
    }

    await cancelDeployment({ id: ownerId, appId, deploymentId: deployments[0].id }).then(sendToApi);
    Logger.printSuccess(`Deployment ${styleText(['bold', 'green'], deployments[0].uuid)} successfully cancelled!`);
  },
});