Skip to content

Commit

Permalink
fix for nested function constructs
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacobsson committed Jul 21, 2023
1 parent bb2520b commit d97c3ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
39 changes: 33 additions & 6 deletions src/commands/local/cdk-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -21,26 +21,28 @@ 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) {
for (const fn of Object.keys(synthedTemplate.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;
Expand Down Expand Up @@ -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 };
}
1 change: 1 addition & 0 deletions src/commands/local/lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit d97c3ac

Please sign in to comment.