diff --git a/commands/__tests__/accounts.test.ts b/commands/__tests__/accounts.test.ts index 8ea0f6bc6..6983e7eef 100644 --- a/commands/__tests__/accounts.test.ts +++ b/commands/__tests__/accounts.test.ts @@ -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'; diff --git a/commands/account/__tests__/info.test.ts b/commands/account/__tests__/info.test.ts new file mode 100644 index 000000000..359163d9e --- /dev/null +++ b/commands/account/__tests__/info.test.ts @@ -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); + }); + }); +}); diff --git a/commands/account/info.ts b/commands/account/info.ts index 4dd0b5aa9..b175a771e 100644 --- a/commands/account/info.ts +++ b/commands/account/info.ts @@ -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 +): Promise { + 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 { addConfigOptions(yargs); yargs.example([ @@ -44,5 +50,5 @@ exports.builder = yargs => { ['$0 accounts info 1234567', i18n(`${i18nKey}.examples.idBased`)], ]); - return yargs; -}; + return yargs as Argv; +} diff --git a/lib/commonOpts.ts b/lib/commonOpts.ts index 5a6794924..7704e170a 100644 --- a/lib/commonOpts.ts +++ b/lib/commonOpts.ts @@ -1,3 +1,4 @@ +import { Argv, Arguments } from 'yargs'; import { LOG_LEVEL, setLogLevel as setLoggerLogLevel, @@ -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'; @@ -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 { + return yargs.option('config', { alias: 'c', describe: i18n(`${i18nKey}.options.config.describe`), type: 'string', diff --git a/types/Yargs.ts b/types/Yargs.ts new file mode 100644 index 000000000..da9320a94 --- /dev/null +++ b/types/Yargs.ts @@ -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'; +};