Skip to content

Commit

Permalink
fix(cli): cdk context --reset <number> does not work (#10753)
Browse files Browse the repository at this point in the history
This PR fixes #3033 while also addressing the issues that arose from #10619

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
diesal11 committed Oct 19, 2020
1 parent 2640d9a commit 2f3a167
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/aws-cdk/lib/commands/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export function handler(args: yargs.Arguments) {
export async function realHandler(options: CommandOptions): Promise<number> {
const { configuration, args } = options;

const contextValues = configuration.context.all;

if (args.clear) {
configuration.context.clear();
await configuration.saveContext();
Expand All @@ -40,17 +38,18 @@ export async function realHandler(options: CommandOptions): Promise<number> {
} 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();

return 0;
}

function listContext(context: any) {
function listContext(context: Context) {
const keys = contextKeys(context);

if (keys.length === 0) {
Expand All @@ -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]);
}

Expand Down Expand Up @@ -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;
Expand All @@ -107,7 +106,7 @@ function keyByNumber(context: any, n: number) {
* Return enumerated keys in a definitive order
*/
function contextKeys(context: Context): [number, string][] {
const keys = Object.keys(context);
const keys = context.keys;
keys.sort();
return enumerate1(keys);
}
Expand Down
23 changes: 23 additions & 0 deletions packages/aws-cdk/test/commands/context-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});

0 comments on commit 2f3a167

Please sign in to comment.