Skip to content

Commit

Permalink
[bugfix] template cleaning (#26)
Browse files Browse the repository at this point in the history
* [bugfix] template cleaning

* dont need to await
  • Loading branch information
gpoitch authored Aug 17, 2020
1 parent b098ccc commit 8798e92
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
8 changes: 2 additions & 6 deletions src/commands/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,18 @@ async function getApiUrl(stackName) {
return apiId && region && `https://${apiId}.execute-api.${region}.amazonaws.com`
}

async function cleanPackagedTemplates(paths) {
return Promise.all(paths.filter(Boolean).map((path) => deleteFileAsync(path)))
}

module.exports = async function deploy(input) {
await validate(input)

const packageResults = await packageProject(input)
const { templatePathEnvMerged, templatePathPackaged, environment, stackName, bucketName } = packageResults
const { templatePathPackaged, environment, stackName, bucketName } = packageResults

const deployParams = [].concat(input.parameters || [], `environment=${environment}`)
try {
await deployStack(templatePathPackaged, stackName, bucketName, input.capabilities, deployParams)
log.success('Deployed')
} finally {
cleanPackagedTemplates([templatePathPackaged, templatePathEnvMerged])
deleteFileAsync(templatePathPackaged)
}

try {
Expand Down
15 changes: 9 additions & 6 deletions src/commands/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { basename, extname, dirname, join } = require('path')
const yaml = require('js-yaml')
const deepmerge = require('deepmerge')
const log = require('../log')
const { findTemplatePath, spawnAsync, readFileAsync, writeFileAsync } = require('../utils')
const { findTemplatePath, spawnAsync, readFileAsync, writeFileAsync, deleteFileAsync } = require('../utils')

// Older versions of aws cli don't support json for `aws cloudformation package`
function checkCliVersion() {
Expand Down Expand Up @@ -40,7 +40,7 @@ async function mergeEnvTemplate(baseTemplatePath, baseTemplateJson, environment)
try {
enviromentTemplateString = await readFileAsync(environmentTemplatePath, 'utf8')
} catch (e) {
return [baseTemplatePath, baseTemplateJson]
return []
}
log.info(`Merging ${environment} template (${environmentTemplatePath}) with base template (${baseTemplatePath})`)
const enviromentTemplateJson = parseTemplate(enviromentTemplateString, templateExt)
Expand All @@ -57,9 +57,11 @@ module.exports = async function packageProject(input) {
const templateExt = extname(templatePath)
const templateJson = parseTemplate(templateString, templateExt)
const environment = input.environment || 'development'
const [templatePathEnvMerged, mergedTemplateJson] = await mergeEnvTemplate(templatePath, templateJson, environment)
const [templatePathEnvMerged, templateJsonEnvMerged] = await mergeEnvTemplate(templatePath, templateJson, environment)
const resolvedTemplatePath = templatePathEnvMerged || templatePath
const resolvedTemplateJson = templateJsonEnvMerged || templateJson
const templatePathPackaged = filePathWithSuffix(templatePath, '-packaged')
const parameters = mergedTemplateJson.Parameters
const parameters = resolvedTemplateJson.Parameters
const stackName = input['stack-name'] || `${parameters.stackName && parameters.stackName.Default}-${environment}`
const bucketName = input['s3-bucket'] || (parameters.bucketName && parameters.bucketName.Default)
const s3Prefix =
Expand All @@ -68,7 +70,7 @@ module.exports = async function packageProject(input) {
`${stackName}/${new Date().getFullYear()}`
const command =
`aws cloudformation package ` +
`--template-file ${templatePathEnvMerged} ` +
`--template-file ${resolvedTemplatePath} ` +
`--output-template-file ${templatePathPackaged} ` +
`--s3-bucket ${bucketName}` +
`${s3Prefix ? ' --s3-prefix ' + s3Prefix : ''}` +
Expand All @@ -79,5 +81,6 @@ module.exports = async function packageProject(input) {
log.info('Packaging and uploading code...').command(command)
await spawnAsync(command)
log.success('Code packaged & uploaded')
return { templatePathEnvMerged, templatePathPackaged, environment, stackName, bucketName }
if (templatePathEnvMerged) deleteFileAsync(templatePathEnvMerged)
return { templatePathPackaged, environment, stackName, bucketName }
}

0 comments on commit 8798e92

Please sign in to comment.