From c1ba937ff703a20fd966e3fcef178ce9984f2677 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Fri, 9 May 2025 14:51:17 -0400 Subject: [PATCH] Update ts pattern for secret commands --- commands/__tests__/secret.test.ts | 10 ++-- commands/secret.ts | 33 +++++++++---- commands/secret/__tests__/addSecret.test.ts | 2 +- .../secret/__tests__/deleteSecret.test.ts | 2 +- commands/secret/__tests__/listSecret.test.ts | 2 +- .../secret/__tests__/updateSecret.test.ts | 2 +- commands/secret/addSecret.ts | 49 ++++++++++++------- commands/secret/deleteSecret.ts | 46 +++++++++++------ commands/secret/listSecret.ts | 44 +++++++++++------ commands/secret/updateSecret.ts | 47 ++++++++++++------ 10 files changed, 152 insertions(+), 85 deletions(-) diff --git a/commands/__tests__/secret.test.ts b/commands/__tests__/secret.test.ts index 774e3c359..8bd539eb9 100644 --- a/commands/__tests__/secret.test.ts +++ b/commands/__tests__/secret.test.ts @@ -1,9 +1,9 @@ import yargs, { Argv } from 'yargs'; -import * as addSecret from '../secret/addSecret'; -import * as deleteSecret from '../secret/deleteSecret'; -import * as listSecret from '../secret/listSecret'; -import * as updateSecret from '../secret/updateSecret'; -import * as secretCommands from '../secret'; +import addSecret from '../secret/addSecret'; +import deleteSecret from '../secret/deleteSecret'; +import listSecret from '../secret/listSecret'; +import updateSecret from '../secret/updateSecret'; +import secretCommands from '../secret'; jest.mock('yargs'); jest.mock('../secret/addSecret'); diff --git a/commands/secret.ts b/commands/secret.ts index 6fc095dc4..c56d7579b 100644 --- a/commands/secret.ts +++ b/commands/secret.ts @@ -1,17 +1,16 @@ import { Argv } from 'yargs'; -import { addGlobalOptions } from '../lib/commonOpts'; -import * as addSecretCommand from './secret/addSecret'; -import * as listSecretCommand from './secret/listSecret'; -import * as deleteSecretCommand from './secret/deleteSecret'; -import * as updateSecretCommand from './secret/updateSecret'; +import addSecretCommand from './secret/addSecret'; +import listSecretCommand from './secret/listSecret'; +import deleteSecretCommand from './secret/deleteSecret'; +import updateSecretCommand from './secret/updateSecret'; import { i18n } from '../lib/lang'; +import { YargsCommandModuleBucket } from '../types/Yargs'; +import { makeYargsBuilder } from '../lib/yargsUtils'; -export const command = ['secret', 'secrets']; -export const describe = i18n(`commands.secret.describe`); - -export function builder(yargs: Argv): Argv { - addGlobalOptions(yargs); +const command = ['secret', 'secrets']; +const describe = i18n(`commands.secret.describe`); +function secretBuilder(yargs: Argv): Argv { yargs .command(listSecretCommand) .command(addSecretCommand) @@ -20,3 +19,17 @@ export function builder(yargs: Argv): Argv { .demandCommand(1, ''); return yargs; } + +const builder = makeYargsBuilder(secretBuilder, command, describe); + +const secretCommand: YargsCommandModuleBucket = { + command, + describe, + builder, + handler: () => {}, +}; + +export default secretCommand; + +// TODO Remove this legacy export once we've migrated all commands to TS +module.exports = secretCommand; diff --git a/commands/secret/__tests__/addSecret.test.ts b/commands/secret/__tests__/addSecret.test.ts index a2a0e468e..c05cf7dae 100644 --- a/commands/secret/__tests__/addSecret.test.ts +++ b/commands/secret/__tests__/addSecret.test.ts @@ -4,7 +4,7 @@ import { addAccountOptions, addUseEnvironmentOptions, } from '../../../lib/commonOpts'; -import * as addSecretCommand from '../addSecret'; +import addSecretCommand from '../addSecret'; jest.mock('yargs'); jest.mock('../../../lib/commonOpts'); diff --git a/commands/secret/__tests__/deleteSecret.test.ts b/commands/secret/__tests__/deleteSecret.test.ts index 1b19f4d71..b2530a261 100644 --- a/commands/secret/__tests__/deleteSecret.test.ts +++ b/commands/secret/__tests__/deleteSecret.test.ts @@ -4,7 +4,7 @@ import { addAccountOptions, addUseEnvironmentOptions, } from '../../../lib/commonOpts'; -import * as deleteSecretCommand from '../deleteSecret'; +import deleteSecretCommand from '../deleteSecret'; jest.mock('yargs'); jest.mock('../../../lib/commonOpts'); diff --git a/commands/secret/__tests__/listSecret.test.ts b/commands/secret/__tests__/listSecret.test.ts index 6235d2543..b0fa10125 100644 --- a/commands/secret/__tests__/listSecret.test.ts +++ b/commands/secret/__tests__/listSecret.test.ts @@ -4,7 +4,7 @@ import { addAccountOptions, addUseEnvironmentOptions, } from '../../../lib/commonOpts'; -import * as listSecretCommand from '../listSecret'; +import listSecretCommand from '../listSecret'; jest.mock('yargs'); jest.mock('../../../lib/commonOpts'); diff --git a/commands/secret/__tests__/updateSecret.test.ts b/commands/secret/__tests__/updateSecret.test.ts index b550017fb..9f2d24099 100644 --- a/commands/secret/__tests__/updateSecret.test.ts +++ b/commands/secret/__tests__/updateSecret.test.ts @@ -4,7 +4,7 @@ import { addAccountOptions, addUseEnvironmentOptions, } from '../../../lib/commonOpts'; -import * as updateSecretCommand from '../updateSecret'; +import updateSecretCommand from '../updateSecret'; jest.mock('yargs'); jest.mock('../../../lib/commonOpts'); diff --git a/commands/secret/addSecret.ts b/commands/secret/addSecret.ts index 7f1d42cac..437bbbaaf 100644 --- a/commands/secret/addSecret.ts +++ b/commands/secret/addSecret.ts @@ -1,14 +1,8 @@ import { Argv, ArgumentsCamelCase } from 'yargs'; import { logger } from '@hubspot/local-dev-lib/logger'; import { addSecret, fetchSecrets } from '@hubspot/local-dev-lib/api/secrets'; - import { logError, ApiErrorContext } from '../../lib/errorHandlers/index'; import { trackCommandUsage } from '../../lib/usageTracking'; -import { - addConfigOptions, - addAccountOptions, - addUseEnvironmentOptions, -} from '../../lib/commonOpts'; import { uiAccountDescription } from '../../lib/ui'; import { secretValuePrompt, @@ -22,17 +16,19 @@ import { ConfigArgs, AccountArgs, EnvironmentArgs, + YargsCommandModule, } from '../../types/Yargs'; +import { makeYargsBuilder } from '../../lib/yargsUtils'; -export const command = 'add [name]'; -export const describe = i18n(`commands.secret.subcommands.add.describe`); +const command = 'add [name]'; +const describe = i18n(`commands.secret.subcommands.add.describe`); -type CombinedArgs = ConfigArgs & AccountArgs & EnvironmentArgs; -type AddSecretArgs = CommonArgs & CombinedArgs & { name?: string }; +type AddSecretArgs = CommonArgs & + ConfigArgs & + AccountArgs & + EnvironmentArgs & { name?: string }; -export async function handler( - args: ArgumentsCamelCase -): Promise { +async function handler(args: ArgumentsCamelCase): Promise { const { name, derivedAccountId } = args; let secretName = name; @@ -83,11 +79,7 @@ export async function handler( } } -export function builder(yargs: Argv): Argv { - addConfigOptions(yargs); - addAccountOptions(yargs); - addUseEnvironmentOptions(yargs); - +function addSecretBuilder(yargs: Argv): Argv { yargs.positional('name', { describe: i18n(`commands.secret.subcommands.add.positionals.name.describe`), type: 'string', @@ -95,3 +87,24 @@ export function builder(yargs: Argv): Argv { return yargs as Argv; } + +const builder = makeYargsBuilder( + addSecretBuilder, + command, + describe, + { + useGlobalOptions: true, + useConfigOptions: true, + useAccountOptions: true, + useEnvironmentOptions: true, + } +); + +const addSecretCommand: YargsCommandModule = { + command, + describe, + handler, + builder, +}; + +export default addSecretCommand; diff --git a/commands/secret/deleteSecret.ts b/commands/secret/deleteSecret.ts index fc0971a1d..33a634cee 100644 --- a/commands/secret/deleteSecret.ts +++ b/commands/secret/deleteSecret.ts @@ -1,34 +1,31 @@ import { Argv, ArgumentsCamelCase } from 'yargs'; import { logger } from '@hubspot/local-dev-lib/logger'; import { deleteSecret, fetchSecrets } from '@hubspot/local-dev-lib/api/secrets'; - import { secretListPrompt } from '../../lib/prompts/secretPrompt'; import { confirmPrompt } from '../../lib/prompts/promptUtils'; import { EXIT_CODES } from '../../lib/enums/exitCodes'; import { ApiErrorContext, logError } from '../../lib/errorHandlers/index'; import { trackCommandUsage } from '../../lib/usageTracking'; import { uiAccountDescription } from '../../lib/ui'; -import { - addConfigOptions, - addAccountOptions, - addUseEnvironmentOptions, -} from '../../lib/commonOpts'; import { i18n } from '../../lib/lang'; import { CommonArgs, ConfigArgs, AccountArgs, EnvironmentArgs, + YargsCommandModule, } from '../../types/Yargs'; +import { makeYargsBuilder } from '../../lib/yargsUtils'; -export const command = 'delete [name]'; -export const describe = i18n(`commands.secret.subcommands.delete.describe`); +const command = 'delete [name]'; +const describe = i18n(`commands.secret.subcommands.delete.describe`); -type CombinedArgs = ConfigArgs & AccountArgs & EnvironmentArgs; type DeleteSecretArgs = CommonArgs & - CombinedArgs & { name?: string; force?: boolean }; + ConfigArgs & + AccountArgs & + EnvironmentArgs & { name?: string; force?: boolean }; -export async function handler( +async function handler( args: ArgumentsCamelCase ): Promise { const { name, derivedAccountId, force } = args; @@ -99,11 +96,7 @@ export async function handler( } } -export function builder(yargs: Argv): Argv { - addConfigOptions(yargs); - addAccountOptions(yargs); - addUseEnvironmentOptions(yargs); - +function deleteSecretBuilder(yargs: Argv): Argv { yargs .positional('name', { describe: i18n( @@ -118,3 +111,24 @@ export function builder(yargs: Argv): Argv { return yargs as Argv; } + +const builder = makeYargsBuilder( + deleteSecretBuilder, + command, + describe, + { + useGlobalOptions: true, + useConfigOptions: true, + useAccountOptions: true, + useEnvironmentOptions: true, + } +); + +const deleteSecretCommand: YargsCommandModule = { + command, + describe, + handler, + builder, +}; + +export default deleteSecretCommand; diff --git a/commands/secret/listSecret.ts b/commands/secret/listSecret.ts index 61909e85d..d9a491e5e 100644 --- a/commands/secret/listSecret.ts +++ b/commands/secret/listSecret.ts @@ -1,30 +1,25 @@ import { Argv, ArgumentsCamelCase } from 'yargs'; import { logger } from '@hubspot/local-dev-lib/logger'; import { fetchSecrets } from '@hubspot/local-dev-lib/api/secrets'; - import { logError, ApiErrorContext } from '../../lib/errorHandlers/index'; import { trackCommandUsage } from '../../lib/usageTracking'; import { uiAccountDescription } from '../../lib/ui'; -import { - addConfigOptions, - addAccountOptions, - addUseEnvironmentOptions, -} from '../../lib/commonOpts'; import { i18n } from '../../lib/lang'; import { CommonArgs, ConfigArgs, AccountArgs, EnvironmentArgs, + YargsCommandModule, } from '../../types/Yargs'; +import { makeYargsBuilder } from '../../lib/yargsUtils'; -export const command = 'list'; -export const describe = i18n(`commands.secret.subcommands.list.describe`); +const command = 'list'; +const describe = i18n(`commands.secret.subcommands.list.describe`); -type CombinedArgs = ConfigArgs & AccountArgs & EnvironmentArgs; -type ListSecretArgs = CommonArgs & CombinedArgs; +type ListSecretArgs = CommonArgs & ConfigArgs & AccountArgs & EnvironmentArgs; -export async function handler( +async function handler( args: ArgumentsCamelCase ): Promise { const { derivedAccountId } = args; @@ -52,10 +47,27 @@ export async function handler( } } -export function builder(yargs: Argv): Argv { - addConfigOptions(yargs); - addAccountOptions(yargs); - addUseEnvironmentOptions(yargs); - +function listSecretBuilder(yargs: Argv): Argv { return yargs as Argv; } + +const builder = makeYargsBuilder( + listSecretBuilder, + command, + describe, + { + useGlobalOptions: true, + useConfigOptions: true, + useAccountOptions: true, + useEnvironmentOptions: true, + } +); + +const listSecretCommand: YargsCommandModule = { + command, + describe, + handler, + builder, +}; + +export default listSecretCommand; diff --git a/commands/secret/updateSecret.ts b/commands/secret/updateSecret.ts index dd76362c9..cbd82eeeb 100644 --- a/commands/secret/updateSecret.ts +++ b/commands/secret/updateSecret.ts @@ -1,16 +1,10 @@ import { Argv, ArgumentsCamelCase } from 'yargs'; import { updateSecret, fetchSecrets } from '@hubspot/local-dev-lib/api/secrets'; - import { EXIT_CODES } from '../../lib/enums/exitCodes'; import { logger } from '@hubspot/local-dev-lib/logger'; import { ApiErrorContext, logError } from '../../lib/errorHandlers/index'; import { trackCommandUsage } from '../../lib/usageTracking'; import { uiAccountDescription } from '../../lib/ui'; -import { - addConfigOptions, - addAccountOptions, - addUseEnvironmentOptions, -} from '../../lib/commonOpts'; import { secretValuePrompt, secretListPrompt, @@ -21,15 +15,19 @@ import { ConfigArgs, AccountArgs, EnvironmentArgs, + YargsCommandModule, } from '../../types/Yargs'; +import { makeYargsBuilder } from '../../lib/yargsUtils'; -export const command = 'update [name]'; -export const describe = i18n(`commands.secret.subcommands.update.describe`); +const command = 'update [name]'; +const describe = i18n(`commands.secret.subcommands.update.describe`); -type CombinedArgs = ConfigArgs & AccountArgs & EnvironmentArgs; -type UpdateSecretArgs = CommonArgs & CombinedArgs & { name?: string }; +type UpdateSecretArgs = CommonArgs & + ConfigArgs & + AccountArgs & + EnvironmentArgs & { name?: string }; -export async function handler( +async function handler( args: ArgumentsCamelCase ): Promise { const { name, derivedAccountId } = args; @@ -87,11 +85,7 @@ export async function handler( } } -export function builder(yargs: Argv): Argv { - addConfigOptions(yargs); - addAccountOptions(yargs); - addUseEnvironmentOptions(yargs); - +function updateSecretBuilder(yargs: Argv): Argv { yargs.positional('name', { describe: i18n( `commands.secret.subcommands.update.positionals.name.describe` @@ -101,3 +95,24 @@ export function builder(yargs: Argv): Argv { return yargs as Argv; } + +const builder = makeYargsBuilder( + updateSecretBuilder, + command, + describe, + { + useGlobalOptions: true, + useConfigOptions: true, + useAccountOptions: true, + useEnvironmentOptions: true, + } +); + +const updateSecretCommand: YargsCommandModule = { + command, + describe, + handler, + builder, +}; + +export default updateSecretCommand;