From 65190f9747d2e2eb99d14b0bd70846d813051be4 Mon Sep 17 00:00:00 2001 From: Elad Ben-Israel Date: Wed, 17 Oct 2018 09:59:31 +0300 Subject: [PATCH] feat(assets): isZipArchive indicates if this is a zip asset (#944) Adds a property to `Asset` called `isZipArchive` which allows checking if the assets represents a zip file. This will be true both for `FileAsset` that references an actual .zip file and for `ZipDirectoryAsset`. --- packages/@aws-cdk/assets/lib/asset.ts | 16 +++++++++++- .../sample-zip-asset.zip | Bin 0 -> 211 bytes packages/@aws-cdk/assets/test/test.asset.ts | 24 ++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 packages/@aws-cdk/assets/test/sample-asset-directory/sample-zip-asset.zip 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 0000000000000000000000000000000000000000..71224bd574e4b201374ee5f7d55ce7f0e523cd9b GIT binary patch literal 211 zcmWIWW@h1H0D&}CQ(sOuvsPgs8-zs}WEhGQa|?1(brXw=Q%iKyGILV(N-9c1LpT|j z3q+Pgw+9`IF0J5ZU}VwW)T_aArkc4>Utb|3H76%uAtg1jNCB)bz?+dtjv1F75+JJ? h7=d_6BZ!5=MplT8Xto7-v$BCSF#=&UkWK<|7ytsvE`0z1 literal 0 HcmV?d00001 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(); + } };