diff --git a/packages/aws-cdk/lib/commands/context.ts b/packages/aws-cdk/lib/commands/context.ts index cd5309bae7360..80019889b964e 100644 --- a/packages/aws-cdk/lib/commands/context.ts +++ b/packages/aws-cdk/lib/commands/context.ts @@ -28,8 +28,6 @@ export function handler(args: yargs.Arguments) { export async function realHandler(options: CommandOptions): Promise { const { configuration, args } = options; - const contextValues = configuration.context.all; - if (args.clear) { configuration.context.clear(); await configuration.saveContext(); @@ -40,9 +38,10 @@ export async function realHandler(options: CommandOptions): Promise { } else { // List -- support '--json' flag if (args.json) { + const contextValues = configuration.context.all; process.stdout.write(JSON.stringify(contextValues, undefined, 2)); } else { - listContext(contextValues); + listContext(configuration.context); } } await version.displayVersionMessage(); @@ -50,7 +49,7 @@ export async function realHandler(options: CommandOptions): Promise { return 0; } -function listContext(context: any) { +function listContext(context: Context) { const keys = contextKeys(context); if (keys.length === 0) { @@ -66,7 +65,7 @@ function listContext(context: any) { // Print config by default const data: any[] = [[colors.green('#'), colors.green('Key'), colors.green('Value')]]; for (const [i, key] of keys) { - const jsonWithoutNewlines = JSON.stringify(context[key], undefined, 2).replace(/\s+/g, ' '); + const jsonWithoutNewlines = JSON.stringify(context.all[key], undefined, 2).replace(/\s+/g, ' '); data.push([i, key, jsonWithoutNewlines]); } @@ -94,7 +93,7 @@ function invalidateContext(context: Context, key: string) { } } -function keyByNumber(context: any, n: number) { +function keyByNumber(context: Context, n: number) { for (const [i, key] of contextKeys(context)) { if (n === i) { return key; diff --git a/packages/aws-cdk/test/commands/context-command.test.ts b/packages/aws-cdk/test/commands/context-command.test.ts index 87389fd1896f6..6a73c8008b52e 100644 --- a/packages/aws-cdk/test/commands/context-command.test.ts +++ b/packages/aws-cdk/test/commands/context-command.test.ts @@ -39,3 +39,26 @@ test('context reset can remove a context key', async () => { baz: 'quux', }); }); + +test('context reset can remove a context key using number', async () => { + // GIVEN + const configuration = new Configuration(); + configuration.context.set('foo', 'bar'); + configuration.context.set('baz', 'quux'); + + expect(configuration.context.all).toEqual({ + foo: 'bar', + baz: 'quux', + }); + + // WHEN + await realHandler({ + configuration, + args: { reset: '1' }, + } as any); + + // THEN + expect(configuration.context.all).toEqual({ + foo: 'bar', + }); +});