diff --git a/packages/@aws-cdk/assets/lib/asset.ts b/packages/@aws-cdk/assets/lib/asset.ts index 18e1adc93c3d2..ffef5ebcb3cef 100644 --- a/packages/@aws-cdk/assets/lib/asset.ts +++ b/packages/@aws-cdk/assets/lib/asset.ts @@ -65,7 +65,16 @@ export class Asset extends cdk.Construct { */ public readonly assetPath: string; - private readonly bucket: s3.BucketRef; + /** + * The S3 bucket in which this asset resides. + */ + public readonly bucket: s3.BucketRef; + + /** + * Indicates if this asset is a zip archive. Allows constructs to ensure that the + * correct file type was used. + */ + public readonly isZipArchive: boolean; /** * The S3 prefix where all different versions of this asset are stored @@ -78,6 +87,11 @@ export class Asset extends cdk.Construct { // resolve full path this.assetPath = path.resolve(props.path); + // sets isZipArchive based on the type of packaging and file extension + this.isZipArchive = props.packaging === AssetPackaging.ZipDirectory + ? true + : this.assetPath.toLowerCase().endsWith('.zip'); + validateAssetOnDisk(this.assetPath, props.packaging); // add parameters for s3 bucket and s3 key. those will be set by diff --git a/packages/@aws-cdk/assets/test/sample-asset-directory/sample-zip-asset.zip b/packages/@aws-cdk/assets/test/sample-asset-directory/sample-zip-asset.zip new file mode 100644 index 0000000000000..71224bd574e4b Binary files /dev/null and b/packages/@aws-cdk/assets/test/sample-asset-directory/sample-zip-asset.zip differ diff --git a/packages/@aws-cdk/assets/test/test.asset.ts b/packages/@aws-cdk/assets/test/test.asset.ts index c16a9e74d1ab5..55b6cdb256d5f 100644 --- a/packages/@aws-cdk/assets/test/test.asset.ts +++ b/packages/@aws-cdk/assets/test/test.asset.ts @@ -112,4 +112,28 @@ export = { test.done(); }, + + 'isZipArchive indicates if the asset represents a .zip file (either explicitly or via ZipDirectory packaging)'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + + // WHEN + const nonZipAsset = new FileAsset(stack, 'NonZipAsset', { + path: path.join(__dirname, 'sample-asset-directory', 'sample-asset-file.txt') + }); + + const zipDirectoryAsset = new ZipDirectoryAsset(stack, 'ZipDirectoryAsset', { + path: path.join(__dirname, 'sample-asset-directory') + }); + + const zipFileAsset = new FileAsset(stack, 'ZipFileAsset', { + path: path.join(__dirname, 'sample-asset-directory', 'sample-zip-asset.zip') + }); + + // THEN + test.equal(nonZipAsset.isZipArchive, false); + test.equal(zipDirectoryAsset.isZipArchive, true); + test.equal(zipFileAsset.isZipArchive, true); + test.done(); + } };