Skip to content

Commit

Permalink
feat: add template option to choose the diagram type
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Jan 28, 2023
1 parent fdb560a commit 9fd887b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ Run the following command to analyze a function:
```bash
serverless package --analyze myFunctionName
```

## Options 🛠

### `--analyze`

The name of the function to analyze.

### `--template`

The bundle template to use. Should be one of `sunburst`, `treemap`, `network`. Defaults to `treemap`.
18 changes: 7 additions & 11 deletions plugin/bundleVisualizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Metadata, visualizer } from 'esbuild-visualizer';
import { Metadata, TemplateType, visualizer } from 'esbuild-visualizer';
import { mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'fs';
import { mkdir } from 'fs/promises';
import StreamZip from 'node-stream-zip';
Expand Down Expand Up @@ -31,7 +31,7 @@ const TMP_FOLDER = join(tmpdir(), 'serverless-esbuild-bundle-analyzer');

async function bundleVisualizer(
this: ServerlessAnalyzeBundlePlugin,
options: { logging: Plugin.Logging },
options: { logging: Plugin.Logging; template: TemplateType },
): Promise<void> {
const { analyze: functionName } = this.options;
if (functionName === undefined) {
Expand All @@ -41,17 +41,13 @@ async function bundleVisualizer(

const fullZipPath = slsFunction.package?.artifact;
if (fullZipPath === undefined) {
options.logging.log.info(
`🤯 Analyze failed: function ${functionName} was not found`,
'ServerlessAnalyzeBundlePlugin',
{ color: 'red' },
);

return;
// @ts-expect-error serverless is badly typed 🤔
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
throw new serverless.classes.Error(`🤯 Analyze failed: function ${functionName} was not found`);
}
const functionZipName = parse(fullZipPath).base;

options.logging.log.info(
options.logging.log.notice(
`⏳ Analyzing function ${functionName}`,
'ServerlessAnalyzeBundlePlugin',
);
Expand Down Expand Up @@ -81,7 +77,7 @@ async function bundleVisualizer(

const fileContent = await visualizer(jsonContent, {
title: `${functionName} function bundle visualizer `,
template: 'treemap',
template: options.template,
});

const filename = `${TEMP_DIR_LOCATION}/${functionName}.html`;
Expand Down
20 changes: 18 additions & 2 deletions plugin/serverlessAnalyzeBundle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { TemplateType } from 'esbuild-visualizer';
import Serverless from 'serverless';
import Plugin from 'serverless/classes/Plugin';

import bundleVisualizer from './bundleVisualizer';

interface OptionsExtended extends Serverless.Options {
analyze?: string;
template?: TemplateType;
}

export class ServerlessAnalyzeBundlePlugin implements Plugin {
Expand All @@ -29,16 +31,30 @@ export class ServerlessAnalyzeBundlePlugin implements Plugin {
type: 'string',
usage: 'Specify the function you want to analyze (e.g. "--analyze \'helloWorld\'")',
},
template: {
// @ts-expect-error plugin is badly typed 🤔
type: 'string',
usage:
"Specify the template you want to use (e.g. \"--template 'treemap'\"). Should be one of 'sunburst', 'treemap', 'network'. Defaults to 'treemap'",
},
},
},
};
this.hooks = {
'after:package:finalize': async () => {
const { analyze } = this.options;
const { analyze, template } = this.options;
if (analyze === undefined) {
return;
}
await this.bundleVisualizer({ logging });
if (template !== undefined && !['sunburst', 'treemap', 'network'].includes(template)) {
// @ts-expect-error serverless is badly typed 🤔
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
throw new serverless.classes.Error(
`🤯 Analyze failed: template ${template} is not supported. Should be one of 'sunburst', 'treemap', 'network'`,
);
}

await this.bundleVisualizer({ logging, template: template ?? 'treemap' });
},
};

Expand Down

0 comments on commit 9fd887b

Please sign in to comment.