From 46c4514f94e17c0107c888e292340209b069f7e1 Mon Sep 17 00:00:00 2001 From: Kaizen Conroy Date: Wed, 8 Jan 2025 23:29:28 -0500 Subject: [PATCH 1/2] chore(cli): aliased commands can be converted to cli args --- packages/aws-cdk/lib/convert-to-cli-args.ts | 4 ++++ packages/aws-cdk/lib/settings.ts | 2 ++ tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts | 7 ++++++- .../cli-args-gen/test/cli-args-function-gen.test.ts | 2 ++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk/lib/convert-to-cli-args.ts b/packages/aws-cdk/lib/convert-to-cli-args.ts index 3fc1b9a2bc541..dd11a124f9a3e 100644 --- a/packages/aws-cdk/lib/convert-to-cli-args.ts +++ b/packages/aws-cdk/lib/convert-to-cli-args.ts @@ -38,6 +38,7 @@ export function convertToCliArgs(args: any): CliArguments { let commandOptions; switch (args._[0] as Command) { case 'list': + case 'ls': commandOptions = { long: args.long, showDependencies: args.showDependencies, @@ -46,6 +47,7 @@ export function convertToCliArgs(args: any): CliArguments { break; case 'synthesize': + case 'synth': commandOptions = { exclusively: args.exclusively, validation: args.validation, @@ -193,6 +195,7 @@ export function convertToCliArgs(args: any): CliArguments { break; case 'acknowledge': + case 'ack': commandOptions = { ID: args.ID, }; @@ -237,6 +240,7 @@ export function convertToCliArgs(args: any): CliArguments { break; case 'docs': + case 'doc': commandOptions = { browser: args.browser, }; diff --git a/packages/aws-cdk/lib/settings.ts b/packages/aws-cdk/lib/settings.ts index c29ae2045c0ac..9c6e680e6c8d8 100644 --- a/packages/aws-cdk/lib/settings.ts +++ b/packages/aws-cdk/lib/settings.ts @@ -36,10 +36,12 @@ export enum Command { ROLLBACK = 'rollback', IMPORT = 'import', ACKNOWLEDGE = 'acknowledge', + ACK = 'ack', NOTICES = 'notices', MIGRATE = 'migrate', CONTEXT = 'context', DOCS = 'docs', + DOC = 'doc', DOCTOR = 'doctor', } diff --git a/tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts b/tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts index cb7e6a6dd22d4..ba9de336c6275 100644 --- a/tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts +++ b/tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts @@ -64,7 +64,7 @@ function buildCommandSwitch(config: CliConfig): string { const commandSwitchExprs = ['let commandOptions;', 'switch (args._[0] as Command) {']; for (const commandName of Object.keys(config.commands)) { commandSwitchExprs.push( - `case '${commandName}':`, + ...buildAliases(commandName, config.commands[commandName].aliases), 'commandOptions = {', ...buildCommandOptions(config.commands[commandName]), ...(config.commands[commandName].arg ? [buildPositionalArguments(config.commands[commandName].arg)] : []), @@ -76,6 +76,11 @@ function buildCommandSwitch(config: CliConfig): string { return commandSwitchExprs.join('\n'); } +function buildAliases(commandName: string, aliases: string[] = []): string[] { + const cases = [commandName, ...aliases]; + return cases.map((c) => `case '${c}':`); +} + function buildCommandOptions(options: CliAction): string[] { const commandOptions: string[] = []; for (const optionName of Object.keys(options.options ?? {})) { diff --git a/tools/@aws-cdk/cli-args-gen/test/cli-args-function-gen.test.ts b/tools/@aws-cdk/cli-args-gen/test/cli-args-function-gen.test.ts index 24b92c7fe291e..0660f7df34468 100644 --- a/tools/@aws-cdk/cli-args-gen/test/cli-args-function-gen.test.ts +++ b/tools/@aws-cdk/cli-args-gen/test/cli-args-function-gen.test.ts @@ -31,6 +31,7 @@ describe('render', () => { variadic: true, }, description: 'Deploy a stack', + aliases: ['d'], options: { all: { type: 'boolean', @@ -62,6 +63,7 @@ describe('render', () => { let commandOptions; switch (args._[0] as Command) { case 'deploy': + case 'd': commandOptions = { all: args.all, STACKS: args.STACKS, From f79b6c52dfc0cc0b637c5b0042c3afe2f5eb448e Mon Sep 17 00:00:00 2001 From: Kaizen Conroy Date: Thu, 9 Jan 2025 12:51:00 -0500 Subject: [PATCH 2/2] inline comment --- tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts b/tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts index ba9de336c6275..9cb34988da8ea 100644 --- a/tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts +++ b/tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts @@ -64,6 +64,8 @@ function buildCommandSwitch(config: CliConfig): string { const commandSwitchExprs = ['let commandOptions;', 'switch (args._[0] as Command) {']; for (const commandName of Object.keys(config.commands)) { commandSwitchExprs.push( + // All aliases of the command should map to the same switch branch + // This ensures that we store options of the command regardless of what alias is specified ...buildAliases(commandName, config.commands[commandName].aliases), 'commandOptions = {', ...buildCommandOptions(config.commands[commandName]),