Skip to content
Merged
9 changes: 5 additions & 4 deletions packages/aws-cdk-lib/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ function sortObject(object: { [key: string]: any }): { [key: string]: any } {
/**
* Returns the single archive file of a directory or undefined
*/
function singleArchiveFile(directory: string): string | undefined {
function findSingleFile(directory: string, archiveOnly: boolean): string | undefined {
if (!fs.existsSync(directory)) {
throw new Error(`Directory ${directory} does not exist.`);
}
Expand All @@ -571,7 +571,7 @@ function singleArchiveFile(directory: string): string | undefined {
if (content.length === 1) {
const file = path.join(directory, content[0]);
const extension = getExtension(content[0]).toLowerCase();
if (fs.statSync(file).isFile() && ARCHIVE_EXTENSIONS.includes(extension)) {
if (fs.statSync(file).isFile() && (!archiveOnly || ARCHIVE_EXTENSIONS.includes(extension))) {
return file;
}
}
Expand All @@ -590,7 +590,7 @@ interface BundledAsset {
* and the type of output.
*/
function determineBundledAsset(bundleDir: string, outputType: BundlingOutput): BundledAsset {
const archiveFile = singleArchiveFile(bundleDir);
const archiveFile = findSingleFile(bundleDir, outputType !== BundlingOutput.SINGLE_FILE);

// auto-discover means that if there is an archive file, we take it as the
// bundle, otherwise, we will archive here.
Expand All @@ -602,8 +602,9 @@ function determineBundledAsset(bundleDir: string, outputType: BundlingOutput): B
case BundlingOutput.NOT_ARCHIVED:
return { path: bundleDir, packaging: FileAssetPackaging.ZIP_DIRECTORY };
case BundlingOutput.ARCHIVED:
case BundlingOutput.SINGLE_FILE:
if (!archiveFile) {
throw new Error('Bundling output directory is expected to include only a single archive file when `output` is set to `ARCHIVED`');
throw new Error('Bundling output directory is expected to include only a single file when `output` is set to `ARCHIVED` or `SINGLE_FILE`');
}
return { path: archiveFile, packaging: FileAssetPackaging.FILE, extension: getExtension(archiveFile) };
}
Expand Down
9 changes: 9 additions & 0 deletions packages/aws-cdk-lib/core/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ export enum BundlingOutput {
* it will be used as the bundle output as-is. Otherwise, all the files in the bundling output directory will be zipped.
*/
AUTO_DISCOVER = 'auto-discover',

/**
* The bundling output directory includes a single file which
* will be used as the final bundle. If the output directory does not
* include exactly a single file, bundling will fail.
*
* Similar to ARCHIVED but for non-archive files
*/
SINGLE_FILE = 'single-file',
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/aws-cdk-lib/core/test/staging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1257,8 +1257,10 @@ describe('staging', () => {
command: [DockerStubCommand.MULTIPLE_FILES],
outputType: BundlingOutput.ARCHIVED,
},
})).toThrow(/Bundling output directory is expected to include only a single archive file when `output` is set to `ARCHIVED`/);
})).toThrow(/Bundling output directory is expected to include only a single file when `output` is set to `ARCHIVED` or `SINGLE_FILE`/);
});

// TODO test here hoegertn
Copy link
Contributor

@mrgrain mrgrain Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

});

describe('staging with docker cp', () => {
Expand Down