Skip to content

Commit

Permalink
chore(core): embedded export for cfn-parse (#19773)
Browse files Browse the repository at this point in the history
This PR moves `cfn-parse.ts` under a folder within `@aws-cdk/core/lib`, where it is "secretly" exported. You can access the API of `cfn-parse` by importing `import * as cfn_parse from '@aws-cdk/core/lib/cfn-parse';`.

The motivation behind this change is to stabilize the code generated by `cfn2ts`. `cfn2ts` imports `cfn_parse` but until now, this was not possible in v2 since `aws-cdk-lib` is export-restricted. This change should allow cdk v2 users the ability to generate L1 code via `cfn2ts`.

Closes #18037. 

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc committed Apr 7, 2022
1 parent 3665db9 commit f82d96b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
@@ -1,5 +1,5 @@
import * as core from '@aws-cdk/core';
import * as cfn_parse from '@aws-cdk/core/lib/cfn-parse';
import * as cfn_parse from '@aws-cdk/core/lib/helpers-internal';
import { Construct } from 'constructs';
import * as cfn_type_to_l1_mapping from './cfn-type-to-l1-mapping';
import * as futils from './file-utils';
Expand Down
@@ -1,7 +1,7 @@
import { Construct } from 'constructs';
import { CfnHook } from './cfn-hook';
import { FromCloudFormationOptions } from './cfn-parse';
import { CfnResource } from './cfn-resource';
import { FromCloudFormationOptions } from './helpers-internal';
import { undefinedIfAllValuesAreEmpty } from './util';

/**
Expand Down
@@ -1,20 +1,20 @@
import { CfnCondition } from './cfn-condition';
import { CfnElement } from './cfn-element';
import { Fn } from './cfn-fn';
import { CfnMapping } from './cfn-mapping';
import { Aws } from './cfn-pseudo';
import { CfnResource } from './cfn-resource';
import { CfnCondition } from '../cfn-condition';
import { CfnElement } from '../cfn-element';
import { Fn } from '../cfn-fn';
import { CfnMapping } from '../cfn-mapping';
import { Aws } from '../cfn-pseudo';
import { CfnResource } from '../cfn-resource';
import {
CfnAutoScalingReplacingUpdate, CfnAutoScalingRollingUpdate, CfnAutoScalingScheduledAction, CfnCodeDeployLambdaAliasUpdate,
CfnCreationPolicy, CfnDeletionPolicy, CfnResourceAutoScalingCreationPolicy, CfnResourceSignal, CfnUpdatePolicy,
} from './cfn-resource-policy';
import { CfnTag } from './cfn-tag';
import { Lazy } from './lazy';
import { CfnReference, ReferenceRendering } from './private/cfn-reference';
import { IResolvable } from './resolvable';
import { Validator } from './runtime';
import { isResolvableObject, Token } from './token';
import { undefinedIfAllValuesAreEmpty } from './util';
} from '../cfn-resource-policy';
import { CfnTag } from '../cfn-tag';
import { Lazy } from '../lazy';
import { CfnReference, ReferenceRendering } from '../private/cfn-reference';
import { IResolvable } from '../resolvable';
import { Validator } from '../runtime';
import { isResolvableObject, Token } from '../token';
import { undefinedIfAllValuesAreEmpty } from '../util';

/**
* The class used as the intermediate result from the generated L1 methods
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/core/lib/helpers-internal/index.ts
@@ -0,0 +1 @@
export * from './cfn-parse';
5 changes: 5 additions & 0 deletions packages/@aws-cdk/core/package.json
Expand Up @@ -162,6 +162,11 @@
"lines": 55,
"branches": 35
},
"ubergen": {
"exports": {
"./lib/helpers-internal": "./lib/helpers-internal/index.js"
}
},
"keywords": [
"aws",
"cdk",
Expand Down
4 changes: 2 additions & 2 deletions tools/@aws-cdk/cfn2ts/lib/codegen.ts
Expand Up @@ -58,8 +58,8 @@ export default class CodeGenerator {
this.code.line('/* eslint-disable max-len */ // This is generated code - line lengths are difficult to control');
this.code.line();
this.code.line(`import * as ${CORE} from '${coreImport}';`);
// explicitly import the cfn-parse.ts file from @core, which is not part of the public API of the module
this.code.line(`import * as ${CFN_PARSE} from '${coreImport}/${coreImport === '.' ? '' : 'lib/'}cfn-parse';`);
// import cfn-parse from an embedded folder inside @core, since it is not part of the public API of the module
this.code.line(`import * as ${CFN_PARSE} from '${coreImport}/${coreImport === '.' ? '' : 'lib/'}helpers-internal';`);
}

public emitCode(): void {
Expand Down
12 changes: 10 additions & 2 deletions tools/@aws-cdk/ubergen/bin/ubergen.ts
Expand Up @@ -97,6 +97,11 @@ interface PackageJson {
* @default true
*/
readonly explicitExports?: boolean;

/**
* An exports section that should be ignored for v1 but included for ubergen
*/
readonly exports?: Record<string, string>;
};
exports?: Record<string, string>;
}
Expand Down Expand Up @@ -305,8 +310,11 @@ async function prepareSourceFiles(libraries: readonly LibraryReference[], packag
function copySubmoduleExports(targetExports: Record<string, string>, library: LibraryReference, subdirectory: string) {
const visibleName = library.shortName;

for (const [relPath, relSource] of Object.entries(library.packageJson.exports ?? {})) {
targetExports[`./${unixPath(path.join(visibleName, relPath))}`] = `./${unixPath(path.join(subdirectory, relSource))}`;
// Do both REAL "exports" section, as well as virtual, ubergen-only "exports" section
for (const exportSet of [library.packageJson.exports, library.packageJson.ubergen?.exports]) {
for (const [relPath, relSource] of Object.entries(exportSet ?? {})) {
targetExports[`./${unixPath(path.join(visibleName, relPath))}`] = `./${unixPath(path.join(subdirectory, relSource))}`;
}
}

// If there was an export for '.' in the original submodule, this assignment will overwrite it,
Expand Down

0 comments on commit f82d96b

Please sign in to comment.