Skip to content

Commit 65190f9

Browse files
Elad Ben-IsraelRomainMuller
Elad Ben-Israel
authored andcommitted
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`.
1 parent 9de3a84 commit 65190f9

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

packages/@aws-cdk/assets/lib/asset.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,16 @@ export class Asset extends cdk.Construct {
6565
*/
6666
public readonly assetPath: string;
6767

68-
private readonly bucket: s3.BucketRef;
68+
/**
69+
* The S3 bucket in which this asset resides.
70+
*/
71+
public readonly bucket: s3.BucketRef;
72+
73+
/**
74+
* Indicates if this asset is a zip archive. Allows constructs to ensure that the
75+
* correct file type was used.
76+
*/
77+
public readonly isZipArchive: boolean;
6978

7079
/**
7180
* The S3 prefix where all different versions of this asset are stored
@@ -78,6 +87,11 @@ export class Asset extends cdk.Construct {
7887
// resolve full path
7988
this.assetPath = path.resolve(props.path);
8089

90+
// sets isZipArchive based on the type of packaging and file extension
91+
this.isZipArchive = props.packaging === AssetPackaging.ZipDirectory
92+
? true
93+
: this.assetPath.toLowerCase().endsWith('.zip');
94+
8195
validateAssetOnDisk(this.assetPath, props.packaging);
8296

8397
// add parameters for s3 bucket and s3 key. those will be set by
Binary file not shown.

packages/@aws-cdk/assets/test/test.asset.ts

+24
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,28 @@ export = {
112112

113113
test.done();
114114
},
115+
116+
'isZipArchive indicates if the asset represents a .zip file (either explicitly or via ZipDirectory packaging)'(test: Test) {
117+
// GIVEN
118+
const stack = new cdk.Stack();
119+
120+
// WHEN
121+
const nonZipAsset = new FileAsset(stack, 'NonZipAsset', {
122+
path: path.join(__dirname, 'sample-asset-directory', 'sample-asset-file.txt')
123+
});
124+
125+
const zipDirectoryAsset = new ZipDirectoryAsset(stack, 'ZipDirectoryAsset', {
126+
path: path.join(__dirname, 'sample-asset-directory')
127+
});
128+
129+
const zipFileAsset = new FileAsset(stack, 'ZipFileAsset', {
130+
path: path.join(__dirname, 'sample-asset-directory', 'sample-zip-asset.zip')
131+
});
132+
133+
// THEN
134+
test.equal(nonZipAsset.isZipArchive, false);
135+
test.equal(zipDirectoryAsset.isZipArchive, true);
136+
test.equal(zipFileAsset.isZipArchive, true);
137+
test.done();
138+
}
115139
};

0 commit comments

Comments
 (0)