From 8798e923f85cf7341cae1fd584d6d9231ebc7aa5 Mon Sep 17 00:00:00 2001 From: Garth Poitras <411908+gpoitch@users.noreply.github.com> Date: Mon, 17 Aug 2020 13:05:50 -0400 Subject: [PATCH] [bugfix] template cleaning (#26) * [bugfix] template cleaning * dont need to await --- src/commands/deploy.js | 8 ++------ src/commands/package.js | 15 +++++++++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/commands/deploy.js b/src/commands/deploy.js index ef6650d..0d9d7fb 100644 --- a/src/commands/deploy.js +++ b/src/commands/deploy.js @@ -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 { diff --git a/src/commands/package.js b/src/commands/package.js index c8bf7e5..9abe02c 100644 --- a/src/commands/package.js +++ b/src/commands/package.js @@ -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() { @@ -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) @@ -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 = @@ -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 : ''}` + @@ -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 } }