Skip to content

Commit

Permalink
switch
Browse files Browse the repository at this point in the history
  • Loading branch information
jogold committed Jun 9, 2020
1 parent 1ff601d commit 17df113
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions packages/@aws-cdk/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,16 @@ export class AssetStaging extends Construct {
constructor(scope: Construct, id: string, props: AssetStagingProps) {
super(scope, id);

if (props.assetHash && props.assetHashType && props.assetHashType !== AssetHashType.CUSTOM) {
throw new Error(`Cannot specify \`${props.assetHashType}\` for \`assetHashType\` when \`assetHash\` is specified. Use \`CUSTOM\` or leave \`undefined\`.`);
}

this.sourcePath = props.sourcePath;
this.fingerprintOptions = props;

if (props.bundling) {
this.bundleDir = this.bundle(props.bundling);
}

if (props.assetHash && props.assetHashType && props.assetHashType !== AssetHashType.CUSTOM) {
throw new Error(`Cannot specify \`${props.assetHashType}\` for \`assetHashType\` when \`assetHash\` is specified. Use \`CUSTOM\` or leave \`undefined\`.`);
}
const hashType = props.assetHash ? AssetHashType.CUSTOM : props.assetHashType ?? AssetHashType.SOURCE;
this.assetHash = this.calculateHash(hashType, props.assetHash);

Expand Down Expand Up @@ -176,24 +175,21 @@ export class AssetStaging extends Construct {
}

private calculateHash(hashType: AssetHashType, assetHash?: string): string {
if (hashType === AssetHashType.SOURCE) {
return FileSystem.fingerprint(this.sourcePath, this.fingerprintOptions);
}

if (hashType === AssetHashType.BUNDLE) {
if (!this.bundleDir) {
throw new Error('Cannot use `AssetHashType.BUNDLE` when `bundling` is not specified.');
}
return FileSystem.fingerprint(this.bundleDir, this.fingerprintOptions);
}

if (hashType === AssetHashType.CUSTOM) {
if (!assetHash) {
throw new Error('`assetHash` must be specified when `assetHashType` is set to `AssetHashType.CUSTOM`.');
}
return assetHash;
switch (hashType) {
case AssetHashType.SOURCE:
return FileSystem.fingerprint(this.sourcePath, this.fingerprintOptions);
case AssetHashType.BUNDLE:
if (!this.bundleDir) {
throw new Error('Cannot use `AssetHashType.BUNDLE` when `bundling` is not specified.');
}
return FileSystem.fingerprint(this.bundleDir, this.fingerprintOptions);
case AssetHashType.CUSTOM:
if (!assetHash) {
throw new Error('`assetHash` must be specified when `assetHashType` is set to `AssetHashType.CUSTOM`.');
}
return assetHash;
default:
throw new Error('Unknown asset hash type.');
}

throw new Error('Unknown asset hash type.');
}
}

0 comments on commit 17df113

Please sign in to comment.