Skip to content

Commit 059c862

Browse files
authored
chore(toolkit-lib): cleanup unused and private refactor exports (#722)
Removes unused and internal exports for refactor. `MappingSource` is not used anymore for now. Mark `RefactorOptions` as `readonly` as per our best practices. Move `mappingsByEnvironment` and `parseMappingGroups` to be internal as intended. Also makes the API Extractor task fail on errors now, to prevent future issues with missing exports. This is how I noticed the unintentional exports in the first place. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent d71960e commit 059c862

File tree

7 files changed

+75
-131
lines changed

7 files changed

+75
-131
lines changed

.projenrc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ toolkitLib.gitignore.addPatterns(
10521052
const apiExtractorDocsTask = toolkitLib.addTask('docs', {
10531053
exec: [
10541054
// Run api-extractor to generate the API model
1055-
'api-extractor run || true',
1055+
'api-extractor run',
10561056
// Create a directory for the API model
10571057
'mkdir -p dist/api-extractor-docs/cdk/api/toolkit-lib',
10581058
// Copy the API model to the directory (with error handling)

packages/@aws-cdk/toolkit-lib/.projen/tasks.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/toolkit-lib/lib/actions/diff/private/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { ResourcesToImport } from '../../../api/resource-import';
1313
import { removeNonImportResources, ResourceMigrator } from '../../../api/resource-import';
1414
import { ToolkitError } from '../../../toolkit/toolkit-error';
1515
import { deserializeStructure, formatErrorMessage } from '../../../util';
16-
import { mappingsByEnvironment } from '../../refactor/index';
16+
import { mappingsByEnvironment } from '../../refactor/private/mapping-helpers';
1717

1818
export function prepareDiff(
1919
ioHelper: IoHelper,
Lines changed: 7 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,4 @@
1-
import type * as cxapi from '@aws-cdk/cx-api';
21
import type { StackSelector } from '../../api';
3-
import type { SdkProvider } from '../../api/aws-auth/sdk-provider';
4-
import type { ExcludeList } from '../../api/refactoring';
5-
import { groupStacks, InMemoryExcludeList, NeverExclude, RefactoringContext } from '../../api/refactoring';
6-
import { ToolkitError } from '../../toolkit/toolkit-error';
7-
8-
type MappingType = 'auto' | 'explicit';
9-
10-
/**
11-
* The source of the resource mappings to be used for refactoring.
12-
*/
13-
export class MappingSource {
14-
/**
15-
* The mapping will be automatically generated based on a comparison of
16-
* the deployed stacks and the local stacks.
17-
*
18-
* @param exclude - A list of resource locations to exclude from the mapping.
19-
*/
20-
public static auto(exclude: string[] = []): MappingSource {
21-
const excludeList = new InMemoryExcludeList(exclude);
22-
return new MappingSource('auto', [], excludeList);
23-
}
24-
25-
/**
26-
* An explicitly provided list of mappings, which will be used for refactoring.
27-
*/
28-
public static explicit(groups: MappingGroup[]): MappingSource {
29-
return new MappingSource('explicit', groups, new NeverExclude());
30-
}
31-
32-
/**
33-
* An explicitly provided list of mappings, which will be used for refactoring,
34-
* but in reverse, that is, the source locations will become the destination
35-
* locations and vice versa.
36-
*/
37-
public static reverse(groups: MappingGroup[]): MappingSource {
38-
const reverseGroups = groups.map((group) => ({
39-
...group,
40-
resources: Object.fromEntries(Object.entries(group.resources).map(([src, dst]) => [dst, src])),
41-
}));
42-
43-
return MappingSource.explicit(reverseGroups);
44-
}
45-
46-
/**
47-
* @internal
48-
*/
49-
public readonly source: MappingType;
50-
51-
/**
52-
* @internal
53-
*/
54-
public readonly groups: MappingGroup[];
55-
56-
/**
57-
* @internal
58-
*/
59-
public readonly exclude: ExcludeList;
60-
61-
private constructor(source: MappingType, groups: MappingGroup[], exclude: ExcludeList) {
62-
this.source = source;
63-
this.groups = groups;
64-
this.exclude = exclude;
65-
}
66-
}
672

683
export interface RefactorOptions {
694
/**
@@ -77,30 +12,30 @@ export interface RefactorOptions {
7712
* List of overrides to be applied to resolve possible ambiguities in the
7813
* computed list of mappings.
7914
*/
80-
overrides?: MappingGroup[];
15+
readonly overrides?: MappingGroup[];
8116

8217
/**
8318
* Criteria for selecting stacks to compare with the deployed stacks in the
8419
* target environment.
8520
*/
86-
stacks?: StackSelector;
21+
readonly stacks?: StackSelector;
8722

8823
/**
8924
* A list of names of additional deployed stacks to be included in the comparison.
9025
*/
91-
additionalStackNames?: string[];
26+
readonly additionalStackNames?: string[];
9227
}
9328

9429
export interface MappingGroup {
9530
/**
9631
* The account ID of the environment in which the mapping is valid.
9732
*/
98-
account: string;
33+
readonly account: string;
9934

10035
/**
10136
* The region of the environment in which the mapping is valid.
10237
*/
103-
region: string;
38+
readonly region: string;
10439

10540
/**
10641
* A collection of resource mappings, where each key is the source location
@@ -110,62 +45,7 @@ export interface MappingGroup {
11045
* location that is not already occupied by any resource.
11146
*
11247
*/
113-
resources: {
114-
[key: string]: string;
48+
readonly resources: {
49+
readonly [key: string]: string;
11550
};
11651
}
117-
118-
export function parseMappingGroups(s: string) {
119-
const mappingGroups = doParse();
120-
121-
// Validate that there are no duplicate destinations.
122-
// By construction, there are no duplicate sources, already.
123-
for (let group of mappingGroups) {
124-
const destinations = new Set<string>();
125-
126-
for (const destination of Object.values(group.resources)) {
127-
if (destinations.has(destination)) {
128-
throw new ToolkitError(
129-
`Duplicate destination resource '${destination}' in environment ${group.account}/${group.region}`,
130-
);
131-
}
132-
destinations.add(destination);
133-
}
134-
}
135-
136-
return mappingGroups;
137-
138-
function doParse(): MappingGroup[] {
139-
const content = JSON.parse(s);
140-
if (content.environments || !Array.isArray(content.environments)) {
141-
return content.environments;
142-
} else {
143-
throw new ToolkitError("Expected an 'environments' array");
144-
}
145-
}
146-
}
147-
148-
export interface EnvironmentSpecificMappings {
149-
readonly environment: cxapi.Environment;
150-
readonly mappings: Record<string, string>;
151-
}
152-
153-
export async function mappingsByEnvironment(
154-
stackArtifacts: cxapi.CloudFormationStackArtifact[],
155-
sdkProvider: SdkProvider,
156-
ignoreModifications?: boolean,
157-
): Promise<EnvironmentSpecificMappings[]> {
158-
const groups = await groupStacks(sdkProvider, stackArtifacts, []);
159-
return groups.map((group) => {
160-
const context = new RefactoringContext({
161-
...group,
162-
ignoreModifications,
163-
});
164-
return {
165-
environment: context.environment,
166-
mappings: Object.fromEntries(
167-
context.mappings.map((m) => [m.source.toLocationString(), m.destination.toLocationString()]),
168-
),
169-
};
170-
});
171-
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type * as cxapi from '@aws-cdk/cx-api';
2+
import type { MappingGroup } from '..';
3+
import type { SdkProvider } from '../../../api/aws-auth/sdk-provider';
4+
import { groupStacks, RefactoringContext } from '../../../api/refactoring';
5+
import { ToolkitError } from '../../../toolkit/toolkit-error';
6+
7+
export function parseMappingGroups(s: string) {
8+
const mappingGroups = doParse();
9+
10+
// Validate that there are no duplicate destinations.
11+
// By construction, there are no duplicate sources, already.
12+
for (let group of mappingGroups) {
13+
const destinations = new Set<string>();
14+
15+
for (const destination of Object.values(group.resources)) {
16+
if (destinations.has(destination)) {
17+
throw new ToolkitError(
18+
`Duplicate destination resource '${destination}' in environment ${group.account}/${group.region}`,
19+
);
20+
}
21+
destinations.add(destination);
22+
}
23+
}
24+
25+
return mappingGroups;
26+
27+
function doParse(): MappingGroup[] {
28+
const content = JSON.parse(s);
29+
if (content.environments || !Array.isArray(content.environments)) {
30+
return content.environments;
31+
} else {
32+
throw new ToolkitError("Expected an 'environments' array");
33+
}
34+
}
35+
}
36+
37+
interface EnvironmentSpecificMappings {
38+
readonly environment: cxapi.Environment;
39+
readonly mappings: Record<string, string>;
40+
}
41+
42+
export async function mappingsByEnvironment(
43+
stackArtifacts: cxapi.CloudFormationStackArtifact[],
44+
sdkProvider: SdkProvider,
45+
ignoreModifications?: boolean,
46+
): Promise<EnvironmentSpecificMappings[]> {
47+
const groups = await groupStacks(sdkProvider, stackArtifacts, []);
48+
return groups.map((group) => {
49+
const context = new RefactoringContext({
50+
...group,
51+
ignoreModifications,
52+
});
53+
return {
54+
environment: context.environment,
55+
mappings: Object.fromEntries(
56+
context.mappings.map((m) => [m.source.toLocationString(), m.destination.toLocationString()]),
57+
),
58+
};
59+
});
60+
}
61+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable import/no-relative-packages */
2+
export { mappingsByEnvironment, parseMappingGroups } from '../../../@aws-cdk/toolkit-lib/lib/actions/refactor/private/mapping-helpers';

packages/aws-cdk/lib/cli/cdk-toolkit.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { format } from 'util';
33
import { RequireApproval } from '@aws-cdk/cloud-assembly-schema';
44
import * as cxapi from '@aws-cdk/cx-api';
55
import type { DeploymentMethod, ToolkitAction, ToolkitOptions } from '@aws-cdk/toolkit-lib';
6-
import { parseMappingGroups, PermissionChangeType, Toolkit, ToolkitError, mappingsByEnvironment } from '@aws-cdk/toolkit-lib';
6+
import { PermissionChangeType, Toolkit, ToolkitError } from '@aws-cdk/toolkit-lib';
77
import * as chalk from 'chalk';
88
import * as chokidar from 'chokidar';
99
import * as fs from 'fs-extra';
@@ -32,6 +32,7 @@ import type { BootstrapEnvironmentOptions } from '../api/bootstrap';
3232
import { Bootstrapper } from '../api/bootstrap';
3333
import { ExtendedStackSelection, StackCollection } from '../api/cloud-assembly';
3434
import type { Deployments, SuccessfulDeployStackResult } from '../api/deployments';
35+
import { mappingsByEnvironment, parseMappingGroups } from '../api/refactor';
3536
import { type Tag } from '../api/tags';
3637
import { StackActivityProgress } from '../commands/deploy';
3738
import { listStacks } from '../commands/list-stacks';

0 commit comments

Comments
 (0)