Skip to content

Commit 58025c0

Browse files
rix0rrrRomainMuller
authored andcommitted
fix(toolkit): correctly reset context from the shell command (#1903)
1 parent 7731565 commit 58025c0

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

packages/aws-cdk/lib/commands/context.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import colors = require('colors/safe');
22
import yargs = require('yargs');
33
import { CommandOptions } from '../command-api';
44
import { print } from '../logging';
5-
import { PROJECT_CONFIG } from '../settings';
5+
import { Context, PROJECT_CONFIG } from '../settings';
66
import { renderTable } from '../util';
77

88
export const command = 'context';
@@ -34,8 +34,7 @@ export async function realHandler(options: CommandOptions): Promise<number> {
3434
await configuration.saveContext();
3535
print('All context values cleared.');
3636
} else if (args.reset) {
37-
invalidateContext(contextValues, args.reset);
38-
configuration.context.setAll(contextValues);
37+
invalidateContext(configuration.context, args.reset);
3938
await configuration.saveContext();
4039
} else {
4140
// List -- support '--json' flag
@@ -77,16 +76,16 @@ function listContext(context: any) {
7776
print(`Run ${colors.blue('cdk context --reset KEY_OR_NUMBER')} to remove a context key. It will be refreshed on the next CDK synthesis run.`);
7877
}
7978

80-
function invalidateContext(context: any, key: string) {
79+
function invalidateContext(context: Context, key: string) {
8180
const i = parseInt(key, 10);
8281
if (`${i}` === key) {
8382
// Twas a number and we fully parsed it.
8483
key = keyByNumber(context, i);
8584
}
8685

8786
// Unset!
88-
if (key in context) {
89-
delete context[key];
87+
if (context.has(key)) {
88+
context.unset(key);
9089
print(`Context value ${colors.blue(key)} reset. It will be refreshed on the next SDK synthesis run.`);
9190
} else {
9291
print(`No context value with key ${colors.blue(key)}`);

packages/aws-cdk/lib/settings.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class Configuration {
2424
private readonly commandLineArguments: Settings;
2525
private projectConfig: Settings;
2626
private projectContext: Settings;
27+
private loaded = false;
2728

2829
constructor(commandLineArguments?: yargs.Arguments) {
2930
this.commandLineArguments = commandLineArguments
@@ -48,13 +49,17 @@ export class Configuration {
4849
.merge(this.commandLineArguments)
4950
.makeReadOnly();
5051

52+
this.loaded = true;
53+
5154
return this;
5255
}
5356

5457
/**
5558
* Save the project config
5659
*/
5760
public async saveContext(): Promise<this> {
61+
if (!this.loaded) { return this; }
62+
5863
if (this.context.modifiedBottom) {
5964
await this.projectConfig.save(PROJECT_CONFIG);
6065
}
@@ -85,6 +90,14 @@ export class Context {
8590
constructor(private readonly bottom: Settings, private readonly bottomPrefixPath: string[], private readonly top: Settings) {
8691
}
8792

93+
public get keys(): string[] {
94+
return Object.keys(this.everything());
95+
}
96+
97+
public has(key: string) {
98+
return this.keys.indexOf(key) > -1;
99+
}
100+
88101
public everything(): {[key: string]: any} {
89102
const b = this.bottom.get(this.bottomPrefixPath) || {};
90103
const t = this.top.get([]) || {};
@@ -105,18 +118,12 @@ export class Context {
105118
}
106119
}
107120

108-
public setAll(values: object) {
109-
for (const [key, value] of Object.entries(values)) {
110-
this.set(key, value);
111-
}
112-
}
113-
114121
public unset(key: string) {
115122
this.set(key, undefined);
116123
}
117124

118125
public clear() {
119-
for (const key of Object.keys(this.everything())) {
126+
for (const key of this.keys) {
120127
this.unset(key);
121128
}
122129
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Test } from 'nodeunit';
2+
import { realHandler } from '../../lib/commands/context';
3+
import { Configuration } from '../../lib/settings';
4+
5+
export = {
6+
async 'context reset can remove a context key'(test: Test) {
7+
// GIVEN
8+
const configuration = new Configuration();
9+
configuration.context.set('foo', 'bar');
10+
configuration.context.set('baz', 'quux');
11+
12+
// WHEN
13+
await realHandler({
14+
configuration,
15+
args: { reset: 'foo' }
16+
} as any);
17+
18+
// THEN
19+
test.deepEqual(configuration.context.everything(), {
20+
baz: 'quux'
21+
});
22+
23+
test.done();
24+
},
25+
};

0 commit comments

Comments
 (0)