Skip to content

Commit

Permalink
chore: Porting the accounts info command to TS (#1342)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers authored Jan 22, 2025
1 parent 91cf915 commit 04942cf
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
2 changes: 1 addition & 1 deletion commands/__tests__/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import yargs from 'yargs';
import list from '../account/list';
import rename from '../account/rename';
import use from '../account/use';
import info from '../account/info';
import * as info from '../account/info';
import remove from '../account/remove';
import clean from '../account/clean';

Expand Down
35 changes: 35 additions & 0 deletions commands/account/__tests__/info.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import yargs, { Argv } from 'yargs';
import { addConfigOptions } from '../../../lib/commonOpts';

jest.mock('yargs');
jest.mock('../../../lib/commonOpts');

// Import this last so mocks apply
import * as accountInfoCommand from '../info';

describe('commands/account/info', () => {
const yargsMock = yargs as Argv;

describe('command', () => {
it('should have the correct command structure', () => {
expect(accountInfoCommand.command).toEqual('info [account]');
});
});

describe('describe', () => {
it('should provide a description', () => {
expect(accountInfoCommand.describe).toBeDefined();
});
});

describe('builder', () => {
it('should support the correct options', () => {
accountInfoCommand.builder(yargsMock);

expect(yargsMock.example).toHaveBeenCalledTimes(1);

expect(addConfigOptions).toHaveBeenCalledTimes(1);
expect(addConfigOptions).toHaveBeenCalledWith(yargsMock);
});
});
});
42 changes: 24 additions & 18 deletions commands/account/info.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
// @ts-nocheck
const { logger } = require('@hubspot/local-dev-lib/logger');
const { getAccountConfig } = require('@hubspot/local-dev-lib/config');
const { getAccessToken } = require('@hubspot/local-dev-lib/personalAccessKey');
const { addConfigOptions } = require('../../lib/commonOpts');
const { i18n } = require('../../lib/lang');
const { getTableContents } = require('../../lib/ui/table');
import { Argv, ArgumentsCamelCase } from 'yargs';
import { logger } from '@hubspot/local-dev-lib/logger';
import { getAccountConfig } from '@hubspot/local-dev-lib/config';
import { getAccessToken } from '@hubspot/local-dev-lib/personalAccessKey';
import { addConfigOptions } from '../../lib/commonOpts';
import { i18n } from '../../lib/lang';
import { getTableContents } from '../../lib/ui/table';
import { CommonArgs, ConfigArgs } from '../../types/Yargs';

const i18nKey = 'commands.account.subcommands.info';
exports.describe = i18n(`${i18nKey}.describe`);

exports.command = 'info [account]';
export const describe = i18n(`${i18nKey}.describe`);
export const command = 'info [account]';

exports.handler = async options => {
const { derivedAccountId } = options;
type AccountInfoArgs = CommonArgs & ConfigArgs;

export async function handler(
args: ArgumentsCamelCase<AccountInfoArgs>
): Promise<void> {
const { derivedAccountId } = args;
const config = getAccountConfig(derivedAccountId);
// check if the provided account is using a personal access key, if not, show an error
if (config && config.authType === 'personalaccesskey') {
const { name, personalAccessKey, env } = config;
let scopeGroups: string[][] = [];

const response = await getAccessToken(
personalAccessKey,
personalAccessKey!,
env,
derivedAccountId
);

const scopeGroups = response.scopeGroups.map(s => [s]);
scopeGroups = response.scopeGroups.map(s => [s]);

logger.log(i18n(`${i18nKey}.name`, { name }));
logger.log(i18n(`${i18nKey}.name`, { name: name! }));
logger.log(i18n(`${i18nKey}.accountId`, { accountId: derivedAccountId }));
logger.log(i18n(`${i18nKey}.scopeGroups`));
logger.log(getTableContents(scopeGroups, { border: { bodyLeft: ' ' } }));
} else {
logger.log(i18n(`${i18nKey}.errors.notUsingPersonalAccessKey`));
}
};
}

exports.builder = yargs => {
export function builder(yargs: Argv): Argv<AccountInfoArgs> {
addConfigOptions(yargs);

yargs.example([
Expand All @@ -44,5 +50,5 @@ exports.builder = yargs => {
['$0 accounts info 1234567', i18n(`${i18nKey}.examples.idBased`)],
]);

return yargs;
};
return yargs as Argv<AccountInfoArgs>;
}
7 changes: 4 additions & 3 deletions lib/commonOpts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Argv, Arguments } from 'yargs';
import {
LOG_LEVEL,
setLogLevel as setLoggerLogLevel,
Expand All @@ -12,7 +13,7 @@ import {
getAndLoadConfigIfNeeded,
} from '@hubspot/local-dev-lib/config';
import { i18n } from './lang';
import { Argv, Arguments } from 'yargs';
import { ConfigArgs, StringArgType } from '../types/Yargs';

const i18nKey = 'lib.commonOpts';

Expand All @@ -35,8 +36,8 @@ export function addAccountOptions(yargs: Argv): Argv {
});
}

export function addConfigOptions(yargs: Argv): Argv {
return yargs.option('config', {
export function addConfigOptions(yargs: Argv): Argv<ConfigArgs> {
return yargs.option<keyof ConfigArgs, StringArgType>('config', {
alias: 'c',
describe: i18n(`${i18nKey}.options.config.describe`),
type: 'string',
Expand Down
17 changes: 17 additions & 0 deletions types/Yargs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Options } from 'yargs';

export type CommonArgs = {
derivedAccountId: number;
providedAccountId?: number;
d: boolean;
debug: boolean;
};

export type ConfigArgs = {
c?: string;
config?: string;
};

export type StringArgType = Options & {
type: 'string';
};

0 comments on commit 04942cf

Please sign in to comment.