From d97c3ac9072e62d7fb834da6a399281abac44cdd Mon Sep 17 00:00:00 2001 From: ljacobsson Date: Sat, 22 Jul 2023 01:47:03 +0200 Subject: [PATCH] fix for nested function constructs --- package.json | 2 +- src/commands/local/cdk-wrapper.js | 39 ++++++++++++++++++++++++++----- src/commands/local/lib/connect.js | 1 + 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index cb6e027..a7a04bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "samp-cli", - "version": "1.0.15", + "version": "1.0.16", "description": "CLI tool for extended productivity with AWS Serverless Application Model (SAM)", "main": "index.js", "scripts": { diff --git a/src/commands/local/cdk-wrapper.js b/src/commands/local/cdk-wrapper.js index d916899..fadd16c 100644 --- a/src/commands/local/cdk-wrapper.js +++ b/src/commands/local/cdk-wrapper.js @@ -2,7 +2,7 @@ const cdk = require('aws-cdk-lib'); const fs = require('fs'); const { yamlDump } = require('yaml-cfn'); const path = require('path'); - +const jsonpath = require('jsonpath'); const baseDir = `${process.cwd()}/.samp-out`; for (const file of getAllJsFiles(baseDir)) { @@ -21,18 +21,20 @@ const className = Object.keys(TargetStack)[0]; const templatePath = `${process.cwd()}/cdk.out/${process.env.SAMP_STACKNAME}.template.json`; const synthedTemplate = JSON.parse(fs.readFileSync(templatePath, 'utf8')); -const app = new cdk.App(); -const stack = new TargetStack[className](null, process.env.SAMP_STACKNAME, {}); +const stack = new TargetStack[className](null, process.env.SAMP_STACKNAME, {}); const resources = stack.node._children; const mockTemplate = { AWSTemplateFormatVersion: "2010-09-09", Transform: ['AWS::Serverless-2016-10-31'], Resources: {} }; for (const key of Object.keys(resources)) { - const resource = resources[key]; - const fingerprintOptions = resource.node._children.Code?.node?._children?.Stage?.fingerprintOptions; + const resource = JSON.parse(JSON.stringify(resources[key], getCircularReplacer())); + delete resource.node?._children?._children?._children?.stack; + const fingerprintOptions = findShallowestOccurrence(resource, 'fingerprintOptions').occurrence; + const entry = fingerprintOptions?.bundling?.relativeEntryPath || (fingerprintOptions?.path ? `${fingerprintOptions?.path}/` : null); + let logicalId = null; let handler if (entry) { @@ -40,7 +42,7 @@ for (const key of Object.keys(resources)) { const resource = synthedTemplate.Resources[fn]; if (resource.Type === "AWS::Lambda::Function") { if (resource.Metadata?.["aws:asset:is-bundled"] === false) continue; - if (resource.Metadata?.['aws:cdk:path'].endsWith(`/${key}/Resource`)) { + if (resource.Metadata?.['aws:cdk:path'].includes(`/${key}/`) && resource.Metadata?.['aws:cdk:path'].includes('/Resource')) { logicalId = fn; handler = resource.Properties.Handler; break; @@ -94,3 +96,28 @@ function getAllJsFiles(directory) { return fileArray; } + + +function findShallowestOccurrence(obj, key, depth = 0) { + let shallowestDepth = Infinity; + let shallowestOccurrence = null; + + for (const prop in obj) { + if (obj.hasOwnProperty(prop)) { + if (prop === key) { + if (depth < shallowestDepth) { + shallowestDepth = depth; + shallowestOccurrence = obj[prop]; + } + } else if (typeof obj[prop] === 'object') { + const occurrence = findShallowestOccurrence(obj[prop], key, depth + 1); + if (occurrence && occurrence.depth < shallowestDepth) { + shallowestDepth = occurrence.depth; + shallowestOccurrence = occurrence.occurrence; + } + } + } + } + + return { occurrence: shallowestOccurrence, depth: shallowestDepth }; +} \ No newline at end of file diff --git a/src/commands/local/lib/connect.js b/src/commands/local/lib/connect.js index f319a96..a920a8f 100644 --- a/src/commands/local/lib/connect.js +++ b/src/commands/local/lib/connect.js @@ -340,6 +340,7 @@ async function updateFunctions(func, lambdaClient) { } else if (error.name === "ResourceConflictException") { console.log("Lambda is currently updating, sleeping for 1 second..."); } else { + console.log("Error updating function. Please make sure that your have deployed your stack and that your function exists in AWS"); throw error; } await new Promise(resolve => setTimeout(resolve, 1000));