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

47.45% Statements 28/59
100% Branches 1/1
0% Functions 0/2
47.45% Lines 28/59

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 60193x 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 AppConfig from '../../models/app_configuration.js';
 
function formatApps(apps, onlyAliases, json) {
  if (json) {
    if (onlyAliases) {
      apps = apps.map((a) => a.alias);
    }
    return JSON.stringify(apps, null, 2);
  } else {
    if (onlyAliases) {
      return apps.map((a) => a.alias).join('\n');
    } else {
      return apps
        .map((app) => {
          const sshUrl = app.git_ssh_url ?? app.deploy_url.replace('https://', 'git+ssh://git@');
          return [
            `Application ${app.name}`,
            `  alias: ${styleText('bold', app.alias)}`,
            `  ID: ${app.app_id}`,
            `  deployment URL: ${app.deploy_url}`,
            `  git+ssh URL: ${sshUrl}`,
          ].join('\n');
        })
        .join('\n\n');
    }
  }
}
 
export const applicationsCommand = defineCommand({
  description: 'List linked applications',
  since: '0.3.0',
  options: {
    onlyAliases: defineOption({
      name: 'only-aliases',
      schema: z.boolean().default(false),
      description: 'List only application aliases',
    }),
    json: defineOption({
      name: 'json',
      schema: z.boolean().default(false),
      description: 'Show result in JSON format',
      aliases: ['j'],
    }),
  },
  args: [],
  async handler(options) {
    const { onlyAliases, json } = options;

    const { apps } = await AppConfig.loadApplicationConf();

    const formattedApps = formatApps(apps, onlyAliases, json);
    Logger.println(formattedApps);
  },
});