From e62c12c9da31913998afe612cfd315fa01debcd3 Mon Sep 17 00:00:00 2001 From: Lars Jacobsson Date: Sun, 11 Oct 2020 22:38:56 +0200 Subject: [PATCH] yaml dump fix --- package.json | 2 +- src/template-parser.js | 57 ++++++++++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 12fcf1a..ff57fc8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mhlabs/evb-cli", - "version": "1.1.14", + "version": "1.1.15", "description": "A package for building EventBridge/CloudWatch Events patterns", "main": "index.js", "scripts": { diff --git a/src/template-parser.js b/src/template-parser.js index ec6ca30..afd8443 100644 --- a/src/template-parser.js +++ b/src/template-parser.js @@ -133,11 +133,20 @@ async function extractSamDefinition() { const extractEvent = await inputUtil.selectFrom( events, - "Select SAM event to extract" + "Select SAM event to extract", + true ); - delete template.Resources[extractEvent.function].Properties.Events[extractEvent.event]; + delete template.Resources[extractEvent.function].Properties.Events[ + extractEvent.event + ]; + if ( + Object.keys(template.Resources[extractEvent.function].Properties.Events) + .length === 0 + ) { + delete template.Resources[extractEvent.function].Properties.Events; + } let suggestion = `${extractEvent.event}Rule`; if (template.Resources[suggestion]) { @@ -151,29 +160,35 @@ async function extractSamDefinition() { EventBusName: extractEvent.config.EventBusName || "default", EventPattern: extractEvent.config.Pattern, State: "ENABLED", - Targets: [{ - Arn: { "Fn::GetAtt": [extractEvent.function, "Arn"] }, - Id: suggestion, - Input: extractEvent.config.Input, - InputPath: extractEvent.config.InputPath - }], + Targets: [ + { + Arn: { "Fn::GetAtt": [extractEvent.function, "Arn"] }, + Id: suggestion, + }, + ], }, }; + + if (extractEvent.config.Input) { + template.Resources[resourceName].Properties.Targets[0].Input = + extractEvent.config.Input; + } + if (extractEvent.config.InputPath) { + template.Resources[resourceName].Properties.Targets[0].InputPath = + extractEvent.config.InputPath; + } template.Resources[`${resourceName}Permission`] = { Type: "AWS::Lambda::Permission", - Properties: { - FunctionName: { - Ref: extractEvent.function - }, - Action: "lambda:InvokeFunction", - Principal: "events.amazonaws.com", - SourceArn: { - "Fn::GetAtt": [ - suggestion, - "Arn" - ] - } - } + Properties: { + FunctionName: { + Ref: extractEvent.function, + }, + Action: "lambda:InvokeFunction", + Principal: "events.amazonaws.com", + SourceArn: { + "Fn::GetAtt": [suggestion, "Arn"], + }, + }, }; saveTemplate();