diff --git a/plugin/bundleVisualizer.ts b/plugin/bundleVisualizer.ts index b27832d..cd6090d 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'; @@ -36,19 +37,15 @@ const getAllFiles = function (dirPath: string, arrayOfFilesInput: string[] = []) return arrayOfFiles; }; -const TMP_FOLDER = '/tmp/serverless-esbuild-bundle-analyzer'; - async function bundleVisualizer(this: ServerlessAnalyzeBundlePlugin): Promise { const { analyze: functionName } = this.options; if (functionName === undefined) { return; } + const slsFunction = this.serverless.service.getFunction(functionName); - const functionZipName = this.serverless.service - .getAllFunctionsNames() - .find(fullFunctionName => 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 +54,7 @@ async function bundleVisualizer(this: ServerlessAnalyzeBundlePlugin): Promise fileName.includes(`/${functionName}/`) && fileName.endsWith('-meta.json'), - )[0]; + const allFiles = getAllFiles(`${TEMP_DIR_LOCATION}`); + const metafileName = allFiles.filter(fileName => { + const handlerPath = (slsFunction as FunctionDefinitionHandler).handler.split('.')[0]; + + return 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', @@ -81,4 +92,6 @@ async function bundleVisualizer(this: ServerlessAnalyzeBundlePlugin): Promise