All files / src/commands/restart restart.command.js

53.84% Statements 42/78
100% Branches 1/1
0% Functions 0/1
53.84% Lines 42/78

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 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 79193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                                         193x 193x  
import { z } from 'zod';
import { defineCommand } from '../../lib/define-command.js';
import { defineOption } from '../../lib/define-option.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import * as Application from '../../models/application.js';
import * as ExitStrategy from '../../models/exit-strategy-option.js';
import { Git } from '../../models/git.js';
import * as Log from '../../models/log.js';
import {
  aliasOption,
  appIdOrNameOption,
  exitOnDeployOption,
  followDeployLogsOption,
  quietOption,
} from '../global.options.js';
 
export const restartCommand = defineCommand({
  description: 'Start or restart an application',
  since: '0.4.0',
  options: {
    commit: defineOption({
      name: 'commit',
      schema: z.string().optional(),
      description: 'Restart the application with a specific commit ID',
      placeholder: 'commit-id',
    }),
    withoutCache: defineOption({
      name: 'without-cache',
      schema: z.boolean().default(false),
      description: 'Restart the application without using cache',
    }),
    alias: aliasOption,
    app: appIdOrNameOption,
    quiet: quietOption,
    follow: followDeployLogsOption,
    exitOnDeploy: exitOnDeployOption,
  },
  args: [],
  async handler(options) {
    const { alias, app: appIdOrName, quiet, commit, withoutCache, follow, exitOnDeploy } = options;

    const exitStrategy = ExitStrategy.get(follow, exitOnDeploy);

    const { ownerId, appId } = await Application.resolveId(appIdOrName, alias);
    const git = await Git.get();
    const fullCommitId = await git.resolveFullCommitId(commit);
    const app = await Application.get(ownerId, appId);

    if (app == null) {
      throw new Error("The application doesn't exist");
    }

    const remoteCommitId = app.commitId;

    const commitId = fullCommitId || remoteCommitId;
    if (commitId != null) {
      const cacheSuffix = withoutCache ? ' without using cache' : '';
      Logger.println(
        `🔄 Restarting ${styleText('bold', app.name)}${cacheSuffix} ${styleText('grey', `(${commitId})`)}`,
      );
    }

    // This should be handled by the API when a deployment ID is set but we'll do this for now
    const redeployDate = new Date();

    const redeploy = await Application.redeploy(ownerId, appId, fullCommitId, withoutCache);

    return Log.watchDeploymentAndDisplayLogs({
      ownerId,
      appId,
      deploymentId: redeploy.deploymentId,
      quiet,
      redeployDate,
      exitStrategy,
    });
  },
});