From f82d96bfed427f8e49910ac7c77004765b2f5f6c Mon Sep 17 00:00:00 2001 From: Kaizen Conroy <36202692+kaizen3031593@users.noreply.github.com> Date: Thu, 7 Apr 2022 04:52:59 -0400 Subject: [PATCH] chore(core): embedded export for cfn-parse (#19773) 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* --- .../cloudformation-include/lib/cfn-include.ts | 2 +- .../lib/cfn-codedeploy-blue-green-hook.ts | 2 +- .../lib/{ => helpers-internal}/cfn-parse.ts | 28 +++++++++---------- .../core/lib/helpers-internal/index.ts | 1 + packages/@aws-cdk/core/package.json | 5 ++++ tools/@aws-cdk/cfn2ts/lib/codegen.ts | 4 +-- tools/@aws-cdk/ubergen/bin/ubergen.ts | 12 ++++++-- 7 files changed, 34 insertions(+), 20 deletions(-) rename packages/@aws-cdk/core/lib/{ => helpers-internal}/cfn-parse.ts (98%) create mode 100644 packages/@aws-cdk/core/lib/helpers-internal/index.ts diff --git a/packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts b/packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts index 957cb68594128..318846833a426 100644 --- a/packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts +++ b/packages/@aws-cdk/cloudformation-include/lib/cfn-include.ts @@ -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'; diff --git a/packages/@aws-cdk/core/lib/cfn-codedeploy-blue-green-hook.ts b/packages/@aws-cdk/core/lib/cfn-codedeploy-blue-green-hook.ts index 7ea09579e0b04..0a4f58d4425b9 100644 --- a/packages/@aws-cdk/core/lib/cfn-codedeploy-blue-green-hook.ts +++ b/packages/@aws-cdk/core/lib/cfn-codedeploy-blue-green-hook.ts @@ -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'; /** diff --git a/packages/@aws-cdk/core/lib/cfn-parse.ts b/packages/@aws-cdk/core/lib/helpers-internal/cfn-parse.ts similarity index 98% rename from packages/@aws-cdk/core/lib/cfn-parse.ts rename to packages/@aws-cdk/core/lib/helpers-internal/cfn-parse.ts index 28391d8916028..eff30d648d162 100644 --- a/packages/@aws-cdk/core/lib/cfn-parse.ts +++ b/packages/@aws-cdk/core/lib/helpers-internal/cfn-parse.ts @@ -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 diff --git a/packages/@aws-cdk/core/lib/helpers-internal/index.ts b/packages/@aws-cdk/core/lib/helpers-internal/index.ts new file mode 100644 index 0000000000000..217b9a9610dfd --- /dev/null +++ b/packages/@aws-cdk/core/lib/helpers-internal/index.ts @@ -0,0 +1 @@ +export * from './cfn-parse'; \ No newline at end of file diff --git a/packages/@aws-cdk/core/package.json b/packages/@aws-cdk/core/package.json index 35cdfcc8d0d46..62fda15ee57c9 100644 --- a/packages/@aws-cdk/core/package.json +++ b/packages/@aws-cdk/core/package.json @@ -162,6 +162,11 @@ "lines": 55, "branches": 35 }, + "ubergen": { + "exports": { + "./lib/helpers-internal": "./lib/helpers-internal/index.js" + } + }, "keywords": [ "aws", "cdk", diff --git a/tools/@aws-cdk/cfn2ts/lib/codegen.ts b/tools/@aws-cdk/cfn2ts/lib/codegen.ts index 79a33b2c3e46e..07904d8fbccf1 100644 --- a/tools/@aws-cdk/cfn2ts/lib/codegen.ts +++ b/tools/@aws-cdk/cfn2ts/lib/codegen.ts @@ -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 { diff --git a/tools/@aws-cdk/ubergen/bin/ubergen.ts b/tools/@aws-cdk/ubergen/bin/ubergen.ts index d48276a260b2c..f6a96a94fb710 100644 --- a/tools/@aws-cdk/ubergen/bin/ubergen.ts +++ b/tools/@aws-cdk/ubergen/bin/ubergen.ts @@ -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; }; exports?: Record; } @@ -305,8 +310,11 @@ async function prepareSourceFiles(libraries: readonly LibraryReference[], packag function copySubmoduleExports(targetExports: Record, 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,