Skip to content

Commit d6013a7

Browse files
authored
chore(cli): aliased commands can be converted to cli args (#32806)
This PR does not currently change CLI functionality. The function `convertToCliArgs` is not used in the CLI yet which is why this PR is not fixing a regression. It will eventually be used to strongly-type cli arguments. Previously, aliased commands, like `cdk ack` instead of `cdk acknowledge` would fall through the cracks of the generated convert function. The switch statement was only switching on command names so we would not store any options associated with an aliased command. Specifically, `cdk synth --exclusively` would _not_ store the `exclusively` flag in the ensuing `CliArguments` object because `synth` is an alias. Now we do. This is an additional step forward to being able to use `CliArguments` in `cli.ts` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent bb59b5a commit d6013a7

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

packages/aws-cdk/lib/convert-to-cli-args.ts

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function convertToCliArgs(args: any): CliArguments {
3838
let commandOptions;
3939
switch (args._[0] as Command) {
4040
case 'list':
41+
case 'ls':
4142
commandOptions = {
4243
long: args.long,
4344
showDependencies: args.showDependencies,
@@ -46,6 +47,7 @@ export function convertToCliArgs(args: any): CliArguments {
4647
break;
4748

4849
case 'synthesize':
50+
case 'synth':
4951
commandOptions = {
5052
exclusively: args.exclusively,
5153
validation: args.validation,
@@ -193,6 +195,7 @@ export function convertToCliArgs(args: any): CliArguments {
193195
break;
194196

195197
case 'acknowledge':
198+
case 'ack':
196199
commandOptions = {
197200
ID: args.ID,
198201
};
@@ -237,6 +240,7 @@ export function convertToCliArgs(args: any): CliArguments {
237240
break;
238241

239242
case 'docs':
243+
case 'doc':
240244
commandOptions = {
241245
browser: args.browser,
242246
};

packages/aws-cdk/lib/settings.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ export enum Command {
3636
ROLLBACK = 'rollback',
3737
IMPORT = 'import',
3838
ACKNOWLEDGE = 'acknowledge',
39+
ACK = 'ack',
3940
NOTICES = 'notices',
4041
MIGRATE = 'migrate',
4142
CONTEXT = 'context',
4243
DOCS = 'docs',
44+
DOC = 'doc',
4345
DOCTOR = 'doctor',
4446
}
4547

tools/@aws-cdk/cli-args-gen/lib/cli-args-function-gen.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ function buildCommandSwitch(config: CliConfig): string {
6464
const commandSwitchExprs = ['let commandOptions;', 'switch (args._[0] as Command) {'];
6565
for (const commandName of Object.keys(config.commands)) {
6666
commandSwitchExprs.push(
67-
`case '${commandName}':`,
67+
// All aliases of the command should map to the same switch branch
68+
// This ensures that we store options of the command regardless of what alias is specified
69+
...buildAliases(commandName, config.commands[commandName].aliases),
6870
'commandOptions = {',
6971
...buildCommandOptions(config.commands[commandName]),
7072
...(config.commands[commandName].arg ? [buildPositionalArguments(config.commands[commandName].arg)] : []),
@@ -76,6 +78,11 @@ function buildCommandSwitch(config: CliConfig): string {
7678
return commandSwitchExprs.join('\n');
7779
}
7880

81+
function buildAliases(commandName: string, aliases: string[] = []): string[] {
82+
const cases = [commandName, ...aliases];
83+
return cases.map((c) => `case '${c}':`);
84+
}
85+
7986
function buildCommandOptions(options: CliAction): string[] {
8087
const commandOptions: string[] = [];
8188
for (const optionName of Object.keys(options.options ?? {})) {

tools/@aws-cdk/cli-args-gen/test/cli-args-function-gen.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('render', () => {
3131
variadic: true,
3232
},
3333
description: 'Deploy a stack',
34+
aliases: ['d'],
3435
options: {
3536
all: {
3637
type: 'boolean',
@@ -62,6 +63,7 @@ describe('render', () => {
6263
let commandOptions;
6364
switch (args._[0] as Command) {
6465
case 'deploy':
66+
case 'd':
6567
commandOptions = {
6668
all: args.all,
6769
STACKS: args.STACKS,

0 commit comments

Comments
 (0)