Skip to content

Commit

Permalink
feat!: use @jcoreio/cloudformation-template-types
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the TS types for Template and Parameters have changed in
deployCloudFormationStack(s).  Now it uses @jcoreio/cloudformation-template-types
to automatically infer the parameter types from the template.
  • Loading branch information
jedwards1211 committed Jun 5, 2024
1 parent 4760a6c commit 4481427
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@aws-sdk/credential-providers": "^3.577.0",
"@aws-sdk/lib-storage": "^3.578.0",
"@babel/runtime": "^7.19.0",
"@jcoreio/cloudformation-template-types": "^1.3.1",
"@types/node": "^18.7.22",
"ansi-escapes": "^4.0.0",
"chalk": "^4.0.0",
Expand Down
21 changes: 20 additions & 1 deletion pnpm-lock.yaml

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

54 changes: 31 additions & 23 deletions src/deployCloudFormationStack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { inspect } from 'util'
import fs from 'fs-extra'
import Deployer from './Deployer'
import {
CloudFormationTemplate,
CloudFormationTemplateParameterValues,
CloudFormationTemplateOutputValues,
} from '@jcoreio/cloudformation-template-types'
import describeCloudFormationFailure from './describeCloudFormationFailure'
import getStackOutputs from './getStackOutputs'
import { Readable } from 'stream'
Expand Down Expand Up @@ -28,22 +33,20 @@ import {
} from '@aws-sdk/client-cloudformation'
import { S3Client } from '@aws-sdk/client-s3'

export type DeployCloudFormationStackInput = {
export type DeployCloudFormationStackInput<
Template extends CloudFormationTemplate = CloudFormationTemplate
> = {
cloudformation?: CloudFormationClient
region?: string
awsConfig?: CloudFormationClientConfig
approve?: boolean
StackName: string
Template?: any
Template?: Template
TemplateFile?: string
TemplateBody?: string | Buffer | (() => Readable)
BlanketDeletionPolicy?: 'Delete' | 'Retain'
StackPolicy?: SetStackPolicyCommandInput['StackPolicyBody']
Parameters?:
| {
[key: string]: Parameter['ParameterValue']
}
| Parameter[]
Parameters?: CloudFormationTemplateParameterValues<Template> | Parameter[]
Capabilities?: Capability[]
RoleARN?: string | undefined
NotificationARNs?: Array<string> | undefined
Expand All @@ -63,16 +66,18 @@ export type DeployCloudFormationStackInput = {
replaceIfCreateFailed?: boolean
}

export type DeployCloudFormationStackOutput = {
export type DeployCloudFormationStackOutput<
Template extends CloudFormationTemplate = CloudFormationTemplate
> = {
ChangeSetName: string
ChangeSetType: string
HasChanges: boolean
Outputs: {
[resourceName: string]: string
}
Outputs: CloudFormationTemplateOutputValues<Template>
}

export default async function deployCloudFormationStack({
export default async function deployCloudFormationStack<
Template extends CloudFormationTemplate = CloudFormationTemplate
>({
cloudformation: _cloudformation,
region,
awsConfig,
Expand All @@ -92,7 +97,9 @@ export default async function deployCloudFormationStack({
readOutputs,
replaceIfCreateFailed,
logEvents = true,
}: DeployCloudFormationStackInput): Promise<DeployCloudFormationStackOutput> {
}: DeployCloudFormationStackInput<Template>): Promise<
DeployCloudFormationStackOutput<Template>
> {
if (!StackName) throw new Error('missing StackName')
if (!awsConfig)
awsConfig = {
Expand All @@ -104,15 +111,16 @@ export default async function deployCloudFormationStack({
}
const cloudformation = _cloudformation || new CloudFormationClient(awsConfig)
const deployer = new Deployer(cloudformation)
const Parameters: Parameter[] | undefined =
_Parameters && !Array.isArray(_Parameters)
? Object.entries(_Parameters)
.map(([key, value]) => ({
ParameterKey: key,
ParameterValue: value == null ? undefined : String(value),
}))
.filter((p) => p.ParameterValue != null)
: _Parameters
const Parameters: Parameter[] | undefined = Array.isArray(_Parameters)
? _Parameters
: _Parameters
? Object.entries(_Parameters as Record<string, Parameter>)
.map(([key, value]) => ({
ParameterKey: key,
ParameterValue: value == null ? undefined : String(value),
}))
.filter((p) => p.ParameterValue != null)
: undefined
const Tags: Tag[] | undefined =
_Tags && !Array.isArray(_Tags)
? Object.entries(_Tags)
Expand Down Expand Up @@ -379,6 +387,6 @@ export default async function deployCloudFormationStack({
ChangeSetName,
ChangeSetType,
HasChanges,
Outputs,
Outputs: Outputs as any,
}
}

0 comments on commit 4481427

Please sign in to comment.