From 41aaba823268d2f2924db4ba1a533edc12d28ca5 Mon Sep 17 00:00:00 2001 From: Stuart Date: Wed, 9 Feb 2022 16:43:39 +0000 Subject: [PATCH] fix: get artifact and metafile paths from sls function object Gets the paths to the metadata file and the zipped artifact from the FunctionDefinitionHandler object exposed by the Serverless API. This makes the plugin work correctly when e.g. the handler path is something like `src/myFunctionHandler.main`. Previously this was failing for me where e.g. the function name in Serverless was 'myFunction' but the file path was `myFunctionHandler.ts`. --- plugin/bundleVisualizer.ts | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/plugin/bundleVisualizer.ts b/plugin/bundleVisualizer.ts index b27832d..61852f4 100644 --- a/plugin/bundleVisualizer.ts +++ b/plugin/bundleVisualizer.ts @@ -1,6 +1,7 @@ import { exec } from 'child_process'; import { readdirSync, statSync } from 'fs'; -import { join } from 'path'; +import { join, parse } from 'path'; +import { FunctionDefinitionHandler } from 'serverless'; import type { ServerlessAnalyzeBundlePlugin } from './serverlessAnalyzeBundle'; @@ -43,12 +44,10 @@ async function bundleVisualizer(this: ServerlessAnalyzeBundlePlugin): Promise fullFunctionName.endsWith(functionName)); - - if (functionZipName === undefined) { + const fullZipPath = slsFunction.package?.artifact; + if (fullZipPath === undefined) { this.serverless.cli.log( `🤯 Analyze failed: function ${functionName} was not found`, 'ServerlessAnalyzeBundlePlugin', @@ -57,6 +56,7 @@ async function bundleVisualizer(this: ServerlessAnalyzeBundlePlugin): Promise fileName.includes(`/${functionName}/`) && fileName.endsWith('-meta.json'), + const allFiles = getAllFiles(`${TEMP_DIR_LOCATION}`); + const handlerPath = (slsFunction as FunctionDefinitionHandler).handler.split('.')[0]; + const metafileName = allFiles.filter( + fileName => fileName.includes(handlerPath) && fileName.endsWith('-meta.json'), )[0]; + + if (!metafileName) { + this.serverless.cli.log( + `🤯 Analyze failed: function ${functionName} metadata was not found`, + 'ServerlessAnalyzeBundlePlugin', + { color: 'red' }, + ); + + return; + } + await pExec( [ 'node_modules/.bin/esbuild-visualizer',