Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Commit

Permalink
🐛 fix(cdk flags): cleaned cdk args, hotswap off by default, turnable …
Browse files Browse the repository at this point in the history
…with flag (#78)

Hotswap is turned off by default to avoid confusion and errors for first-time deployment. It's
configurable with flag.

#78
  • Loading branch information
sladg committed Feb 19, 2023
1 parent 7bf6d05 commit c2d6b02
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ program
.option('--domainNamePrefix <prefix>', 'Prefix for creating DNS records, if left undefined, hostedZone will be used (example: app).', undefined)
.option('--customApiDomain <domain>', 'Domain to forward the requests to /api routes, by default API routes will be handled by the server lambda.', undefined)
.option('--redirectFromApex', 'Redirect from apex domain to specified address.', false)
.option('--profile <name>', 'AWS profile to use with CDK', undefined)
.option('--profile <name>', 'AWS profile to use with CDK.', undefined)
.option('--hotswap', 'Hotswap stack to speedup deployment.', false)
.action(async (options) => {
console.log('Our config is: ', options)
const {
Expand All @@ -81,6 +82,7 @@ program
domainNamePrefix,
customApiDomain,
redirectFromApex,
hotswap,
profile,
} = options

Expand All @@ -99,6 +101,7 @@ program
domainNamePrefix,
customApiDomain,
redirectFromApex,
hotswap,
profile,
}),
)
Expand Down
19 changes: 15 additions & 4 deletions lib/cli/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Props {
domainNamePrefix?: string
redirectFromApex?: boolean
profile?: string
hotswap: boolean
}

const cdkExecutable = require.resolve('aws-cdk/bin/cdk')
Expand All @@ -33,11 +34,21 @@ export const deployHandler = async ({
hostedZone,
customApiDomain,
redirectFromApex,
hotswap,
profile,
}: Props) => {
// All paths are absolute.
const cdkApp = `node ${appPath}`
const cdkCiFlags = `--require-approval never --ci --hotswap` + profile ? ` --profile ${profile}` : ``
const cdkBootstrapArgs = [`--app "node ${appPath}"`]
const cdkDeployArgs = [`--app "node ${appPath}"`, '--require-approval never', '--ci']

if (hotswap) {
cdkDeployArgs.push(`--hotswap`)
}

if (profile) {
cdkDeployArgs.push(`--profile ${profile}`)
cdkBootstrapArgs.push(`--profile ${profile}`)
}

const variables = {
STACK_NAME: stackName,
Expand All @@ -55,13 +66,13 @@ export const deployHandler = async ({

if (bootstrap) {
await executeAsyncCmd({
cmd: `${cdkExecutable} bootstrap --app "${cdkApp}"`,
cmd: `${cdkExecutable} bootstrap ${cdkBootstrapArgs.join(' ')}`,
env: variables,
})
}

await executeAsyncCmd({
cmd: `${cdkExecutable} deploy --app "${cdkApp}" ${cdkCiFlags}`,
cmd: `${cdkExecutable} deploy ${cdkDeployArgs.join(' ')}`,
env: variables,
})
}
9 changes: 6 additions & 3 deletions lib/cli/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ interface Props {
const cdkExecutable = require.resolve('aws-cdk/bin/cdk')

export const removeHandler = async ({ appPath, stackName, region, profile }: Props) => {
const cdkApp = `node ${appPath}`
const cdkCiFlags = `--force --ci` + profile ? ` --profile ${profile}` : ``
const cdkRemoveArgs = [`--app "node ${appPath}"`, '--force', '--require-approval never', '--ci']

if (profile) {
cdkRemoveArgs.push(`--profile ${profile}`)
}

const variables = {
STACK_NAME: stackName,
...(region && { AWS_REGION: region }),
}

await executeAsyncCmd({
cmd: `${cdkExecutable} destroy --app "${cdkApp}" ${cdkCiFlags}`,
cmd: `${cdkExecutable} destroy ${cdkRemoveArgs.join(' ')}`,
env: variables,
})
}
1 change: 1 addition & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const executeAsyncCmd = async ({ cmd, path, env }: CommandProps) => {
}

return new Promise((resolve, reject) => {
console.log('Executing command: ', cmd)
const sh = exec(cmd, { env: { ...process.env, ...env } }, (error, stdout, stderr) => {
if (error) {
reject(error)
Expand Down

0 comments on commit c2d6b02

Please sign in to comment.