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

44.44% Statements 16/36
100% Branches 1/1
0% Functions 0/1
44.44% Lines 16/36

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 37193x 193x 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 { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import { humanJsonOutputFormatOption } from '../global.options.js';
 
export const profileCommand = defineCommand({
  description: 'Display the profile of the current user',
  since: '0.10.1',
  options: {
    format: humanJsonOutputFormatOption,
  },
  async handler(options) {
    const [activeProfile] = config.profiles;
    if (activeProfile == null) {
      throw new Error(`No profile found, use ${styleText('red', 'clever login')} command`);
    }

    const details = await getProfileDetails({ profile: activeProfile, isActive: true });
    if (!details.isTokenValid) {
      throw new Error(`Your token is invalid or has expired, use ${styleText('red', 'clever login')} command`);
    }

    switch (options.format) {
      case 'json': {
        Logger.printJson(details);
        break;
      }
      case 'human':
      default: {
        Logger.println(formatProfileDetails(details));
      }
    }
  },
});