Skip to content

Commit

Permalink
feat(assets): isZipArchive indicates if this is a zip asset (#944)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Elad Ben-Israel authored and RomainMuller committed Oct 17, 2018
1 parent 9de3a84 commit 65190f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/@aws-cdk/assets/lib/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Binary file not shown.
24 changes: 24 additions & 0 deletions packages/@aws-cdk/assets/test/test.asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

0 comments on commit 65190f9

Please sign in to comment.