All files / src/commands/profile profile.list.command.js

34.09% Statements 15/44
100% Branches 1/1
0% Functions 0/1
34.09% Lines 15/44

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 45193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                           193x 193x  
import { config } from '../../config/config.js';
import { defineCommand } from '../../lib/define-command.js';
import { formatProfileDetails, getProfileDetails } from '../../lib/profile.js';
import { Logger } from '../../logger.js';
import { humanJsonOutputFormatOption } from '../global.options.js';
 
export const profileListCommand = defineCommand({
  description: 'List all configured profiles',
  since: '4.6.0',
  options: {
    format: humanJsonOutputFormatOption,
  },
  async handler(options) {
    if (config.profiles.length === 0) {
      throw new Error('No profile found. You are not logged in.');
    }

    const profilesDetails = await Promise.all(
      config.profiles.map(async (profile, index) => {
        return getProfileDetails({
          profile,
          isActive: index === 0,
        });
      }),
    );

    switch (options.format) {
      case 'json': {
        Logger.printJson(profilesDetails);
        break;
      }
      case 'human':
      default: {
        Logger.println(
          profilesDetails
            .map((details) => {
              return formatProfileDetails(details);
            })
            .join('\n\n'),
        );
      }
    }
  },
});