All files / src/commands/addon addon.providers.show.command.js

18.62% Statements 19/102
100% Branches 1/1
0% Functions 0/1
18.62% Lines 19/102

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 92 93 94 95 96 97 98 99 100 101 102 103193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                                                                                                                                       193x 193x  
import { defineCommand } from '../../lib/define-command.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import * as Addon from '../../models/addon.js';
import { getOwnerIdFromOrgIdOrName } from '../../models/ids-resolver.js';
import { humanJsonOutputFormatOption, orgaIdOrNameOption } from '../global.options.js';
import { addonProviderArg } from './addon.args.js';
 
export const addonProvidersShowCommand = defineCommand({
  description: 'Show information about an add-on provider',
  since: '0.2.3',
  options: {
    org: orgaIdOrNameOption,
    format: humanJsonOutputFormatOption,
  },
  args: [addonProviderArg],
  async handler(options, providerName) {
    const { org: orgaIdOrName, format } = options;

    const ownerId = await getOwnerIdFromOrgIdOrName(orgaIdOrName);
    const provider = await Addon.getProvider(providerName, ownerId);
    const providerInfos = await Addon.getProviderInfos(providerName);

    const formattedPlans = [...provider.plans]
      .sort((a, b) => a.price - b.price)
      .map((plan) => {
        const formattedFeatures = plan.features
          .map((feature) => ({
            name: feature.name,
            value: feature.value,
          }))
          .sort((a, b) => a.name.localeCompare(b.name));

        const formattedPlan = {
          id: plan.id,
          name: plan.name,
          slug: plan.slug,
          features: formattedFeatures,
        };

        if (providerInfos != null) {
          const planType = plan.features.find(({ name }) => name.toLowerCase() === 'type');
          if (planType != null && planType.value.toLowerCase() === 'dedicated') {
            const planVersions = Object.keys(providerInfos.dedicated);
            formattedPlan.versions = planVersions.map((version) => {
              return {
                version,
                isDefault: version === providerInfos.defaultDedicatedVersion,
                features: providerInfos.dedicated[version].features.map((feature) => ({
                  name: feature.name,
                  enabledByDefault: feature.enabled,
                })),
              };
            });
          }
        }

        return formattedPlan;
      });

    const formattedProvider = {
      id: provider.id,
      name: provider.name,
      shortDesc: provider.shortDesc,
      regions: provider.regions,
      plans: formattedPlans,
    };

    switch (format) {
      case 'json': {
        Logger.printJson(formattedProvider);
        break;
      }
      case 'human':
      default: {
        Logger.println(styleText('bold', formattedProvider.id));
        Logger.println(`${formattedProvider.name}: ${formattedProvider.shortDesc}`);
        Logger.println();
        Logger.println(`Available regions: ${formattedProvider.regions.join(', ')}`);
        Logger.println();
        Logger.println('Available plans');

        formattedProvider.plans.forEach((plan) => {
          Logger.println(`Plan ${styleText('bold', plan.slug)}`);
          plan.features.forEach(({ name, value }) => Logger.println(`  ${name}: ${value}`));

          if (plan.versions != null) {
            Logger.println(
              `  Available versions: ${plan.versions.map(({ version, isDefault }) => (isDefault ? `${version} (default)` : version)).join(', ')}`,
            );
            plan.versions.forEach(({ version, features }) => {
              Logger.println(`  Options for version ${version}:`);
              features.forEach(({ name, enabledByDefault }) => {
                Logger.println(`    ${name}: default=${enabledByDefault}`);
              });
            });
          }
        });
      }
    }
  },
});