-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Extract
getTemplateAfterAspectInvocation
for more usage
- Loading branch information
Showing
3 changed files
with
31 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./assertions"; | ||
export * from "./simple-gu-stack"; | ||
export * from "./attach-policy-to-test-role"; | ||
export * from "./template"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { App } from "aws-cdk-lib"; | ||
import { Template } from "aws-cdk-lib/assertions"; | ||
import { CloudFormationStackArtifact } from "aws-cdk-lib/cx-api"; | ||
import type { GuStack } from "../../constructs/core"; | ||
|
||
/** | ||
* `Aspects` appear to run only at synth time. | ||
* This means we must synth the stack to see the results of the `Aspect`. | ||
* | ||
* @see https://github.com/aws/aws-cdk/issues/29047 | ||
* | ||
* @param stack the stack to synthesise | ||
*/ | ||
export function getTemplateAfterAspectInvocation(stack: GuStack): Template { | ||
const app = App.of(stack); | ||
|
||
if (!app) { | ||
throw new Error(`Unable to locate the enclosing App from GuStack ${stack.node.id}`); | ||
} | ||
|
||
const { artifacts } = app.synth(); | ||
const cfnStack = artifacts.find((_): _ is CloudFormationStackArtifact => _ instanceof CloudFormationStackArtifact); | ||
|
||
if (!cfnStack) { | ||
throw new Error("Unable to locate a CloudFormationStackArtifact"); | ||
} | ||
|
||
return Template.fromJSON(cfnStack.template as Record<string, unknown>); | ||
} |