Skip to content

Commit a9aaedc

Browse files
authored
fix: Skip invocation of hooks and unzip / delete commands if no hooks are registered (#259)
Thanks for creating this construct - it makes working with .NET Lambda functions in CDK a much better experience! Out of curiosity I have been reading through the code base to learn more about how this construct has been implemented, and I noticed a minor optimization that can be done. The construct was already (implicitly) using `AUTO_DISCOVER` as bundling output type, which according to the docs means: > If the bundling output directory contains a single archive file (zip or jar) it will be used as the bundle output as-is. Otherwise, all the files in the bundling output directory will be zipped. This also means that if no hooks are registered, then there is a such no need to go through the work of unzipping the produced `package.zip` file, just for the CDK to zip it again. This PR introduces a simple check, to see if hooks are registered, and if not, the produced command is simply just the `dotnetPackageCommand` which spits out a `package.zip`, which, due to the use of `AUTO_DISCOVER` as bundling output type, is then picked up and use as code asset by the CDK. On top of this, while the bundling output type in CDK does default to `AUTO_DISCOVER`, I have now configured it in code, just to make it explicit that the this "auto mode" is in fact used as part of the construct's logic.
1 parent cd22051 commit a9aaedc

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/private/bundling.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class Bundling implements cdk.BundlingOptions {
4242
: cdk.AssetHashType.SOURCE,
4343
exclude: ['**/bin/', '**/obj/'],
4444
bundling: {
45+
outputType: cdk.BundlingOutput.AUTO_DISCOVER,
4546
image: bundling.image,
4647
command: bundling.command,
4748
environment: bundling.environment,
@@ -164,6 +165,12 @@ export class Bundling implements cdk.BundlingOptions {
164165
]
165166
.filter((c) => !!c)
166167
.join(' ');
168+
169+
// Skip invocation of hooks and unzip / delete commands if no hooks are registered
170+
if (!this.props.commandHooks) {
171+
return dotnetPackageCommand;
172+
}
173+
167174
const unzipCommand: string =
168175
osPlatform === 'win32'
169176
? [
@@ -182,11 +189,11 @@ export class Bundling implements cdk.BundlingOptions {
182189
: ['rm', packageFile].filter((c) => !!c).join(' ');
183190

184191
return chain([
185-
...(this.props.commandHooks?.beforeBundling(inputDir, outputDir) ?? []),
192+
...(this.props.commandHooks.beforeBundling(inputDir, outputDir) ?? []),
186193
dotnetPackageCommand,
187194
unzipCommand,
188195
deleteCommand,
189-
...(this.props.commandHooks?.afterBundling(inputDir, outputDir) ?? []),
196+
...(this.props.commandHooks.afterBundling(inputDir, outputDir) ?? []),
190197
]);
191198
}
192199
}

0 commit comments

Comments
 (0)