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

39.56% Statements 36/91
100% Branches 1/1
0% Functions 0/1
39.56% Lines 36/91

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92193x 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 Addon from '../../models/addon.js';
import * as Application from '../../models/application.js';
import { aliasOption, appIdOrNameOption, humanJsonOutputFormatOption } from '../global.options.js';
 
export const serviceCommand = defineCommand({
  description: 'Manage service dependencies',
  since: '0.5.0',
  options: {
    onlyApps: defineOption({
      name: 'only-apps',
      schema: z.boolean().default(false),
      description: 'Only show app dependencies',
    }),
    onlyAddons: defineOption({
      name: 'only-addons',
      schema: z.boolean().default(false),
      description: 'Only show add-on dependencies',
    }),
    showAll: defineOption({
      name: 'show-all',
      schema: z.boolean().default(false),
      description: 'Show all available add-ons and applications',
    }),
    alias: aliasOption,
    app: appIdOrNameOption,
    format: humanJsonOutputFormatOption,
  },
  args: [],
  async handler(options) {
    const { alias, app: appIdOrName, showAll, onlyApps, onlyAddons, format } = options;
    if (onlyApps && onlyAddons) {
      throw new Error('--only-apps and --only-addons are mutually exclusive');
    }

    const { ownerId, appId } = await Application.resolveId(appIdOrName, alias);

    const formattedServices = {};

    if (!onlyAddons) {
      const apps = await Application.listDependencies(ownerId, appId, showAll);
      formattedServices.applications = apps.map((app) => ({
        id: app.id,
        name: app.name,
        isLinked: app.isLinked,
      }));
    }

    if (!onlyApps) {
      const addons = await Addon.list(ownerId, appId, showAll);
      formattedServices.addons = addons.map((addon) => ({
        id: addon.id,
        realId: addon.realId,
        name: addon.name,
        isLinked: addon.isLinked,
      }));
    }

    switch (format) {
      case 'json': {
        Logger.printJson(formattedServices);
        break;
      }
      case 'human':
      default: {
        const { applications = [], addons = [] } = formattedServices;

        if (applications.length === 0 && addons.length === 0) {
          Logger.printInfo(
            `No linked services found, use ${styleText('blue', 'clever service link-app')} or ${styleText('blue', 'clever service link-addon')} to link services to an application`,
          );
          return;
        }

        if (applications.length > 0) {
          Logger.println('Applications:');
          applications.forEach(({ isLinked, name }) => Logger.println(`${isLinked ? '*' : ' '} ${name}`));
        }

        if (addons.length > 0) {
          Logger.println('Addons:');
          addons.forEach(({ isLinked, name, realId }) => Logger.println(`${isLinked ? '*' : ' '} ${name} (${realId})`));
        }
      }
    }
  },
});