Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(toolkit): De-duplicate environments before bootstrap #625

Merged
merged 1 commit into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/@aws-cdk/cdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions packages/aws-cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ async function initCommandLine() {
environmentGlobs = [ '**' ]; // default to ALL
}
const stackInfos = await selectStacks();
const availableEnvironments = stackInfos.map(stack => stack.environment)
.filter(env => env !== undefined);
const availableEnvironments = distinct(stackInfos.map(stack => stack.environment)
.filter(env => env !== undefined) as cxapi.Environment[]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typecast should not be necessary, from the type of distinct() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type that tsc infers is an array of optionnals, as it cannot push out the .filter restriction to non-undefined, so I have to help it a little :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah

const environments = availableEnvironments.filter(env => environmentGlobs.find(glob => minimatch(env!.name, glob)));
if (environments.length === 0) {
const globs = JSON.stringify(environmentGlobs);
Expand All @@ -284,6 +284,24 @@ async function initCommandLine() {
throw e;
}
}));

/**
* De-duplicates a list of environments, such that a given account and region is only represented exactly once
* in the result.
*
* @param envs the possibly full-of-duplicates list of environments.
*
* @return a de-duplicated list of environments.
*/
function distinct(envs: cxapi.Environment[]): cxapi.Environment[] {
const unique: { [id: string]: cxapi.Environment } = {};
for (const env of envs) {
const id = `${env.account || 'default'}/${env.region || 'default'}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we somehow utilize the existing concept of environment name (which I believe is rendered in the same way)?

if (id in unique) { continue; }
unique[id] = env;
}
return Object.values(unique);
}
}

/**
Expand Down