diff --git a/cdk/bin/cdk.ts b/cdk/bin/cdk.ts index 13a0527..843d76a 100644 --- a/cdk/bin/cdk.ts +++ b/cdk/bin/cdk.ts @@ -2,7 +2,7 @@ import 'source-map-support/register'; import * as cdk from 'aws-cdk-lib'; import { CdkStack } from '../lib/cdk-stack'; -import { getDeploymentStage } from '../lib/deployment-stage'; +import { getDeploymentStage } from 'cdk-common'; const app = new cdk.App(); const deploymentStage = getDeploymentStage(app.node); diff --git a/cdk/lib/cdk-stack.ts b/cdk/lib/cdk-stack.ts index 467e05d..a9e3398 100644 --- a/cdk/lib/cdk-stack.ts +++ b/cdk/lib/cdk-stack.ts @@ -3,7 +3,7 @@ import { Construct } from 'constructs'; import { ContentsBucket } from './contents-bucket'; import { ContentsDistribution } from './contents-distribution'; -import { DeploymentStage } from './deployment-stage'; +import { DeploymentStage } from 'cdk-common'; type Props = StackProps & Readonly<{ // Deployment stage. diff --git a/cdk/lib/contents-bucket.ts b/cdk/lib/contents-bucket.ts index 9eb88aa..4443ffd 100644 --- a/cdk/lib/contents-bucket.ts +++ b/cdk/lib/contents-bucket.ts @@ -1,7 +1,7 @@ import { RemovalPolicy, aws_s3 as s3 } from 'aws-cdk-lib'; import { Construct } from 'constructs'; -import { DeploymentStage } from './deployment-stage'; +import { DeploymentStage } from 'cdk-common'; type Props = Readonly<{ // Deployment stage. diff --git a/cdk/lib/contents-distribution.ts b/cdk/lib/contents-distribution.ts index 69932e5..1cf106e 100644 --- a/cdk/lib/contents-distribution.ts +++ b/cdk/lib/contents-distribution.ts @@ -13,7 +13,7 @@ import { CODEMONGER_CERTIFICATE_ARN, } from './certificate-config'; import { ContentsBucket } from './contents-bucket'; -import { DeploymentStage } from './deployment-stage'; +import { DeploymentStage } from 'cdk-common'; type Props = Readonly<{ // S3 bucket for contents of the codemonger website. diff --git a/cdk/lib/deployment-stage.ts b/cdk/lib/deployment-stage.ts deleted file mode 100644 index 7a57d20..0000000 --- a/cdk/lib/deployment-stage.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { Node } from 'constructs'; - -/** Possible deployment stages. */ -export const DEPLOYMENT_STAGES = ['development', 'production'] as const; - -/** Deployment stage type. */ -export type DeploymentStage = typeof DEPLOYMENT_STAGES[number]; - -/** Name of the CDK context value for the deployment stage. */ -export const DEPLOYMENT_STAGE_CDK_CONTEXT = 'codemonger:stage'; - -/** - * Converts a given string into an equivalent deployment stage. - * - * Case-insensitive. - * - * @throws RangeError - * - * If `stageStr` is invalid deployment stage. - */ -export function asDeploymentStage(stageStr: string): DeploymentStage { - const stageStrL = stageStr.toLowerCase(); - for (const stage of DEPLOYMENT_STAGES) { - if (stage === stageStrL) { - return stage; - } - } - throw new RangeError(`invalid deployment stage: ${stageStr}`); -} - -/** - * Returns the deployment stage specified to the CDK context. - * - * The deployment stage must be specified to the `"codemonger:stage"` CDK - * context value. - * - * @param node - * - * CDK construct node that provides the CDK context. - * - * @throws RangeError - * - * If no deployment stage is specified, - * or the specified deployment stage is invalid. - */ -export function getDeploymentStage(node: Node): DeploymentStage { - const stage = node.tryGetContext(DEPLOYMENT_STAGE_CDK_CONTEXT); - if (typeof stage !== 'string') { - throw new RangeError(`invalid deployment stage: ${stage}`); - } - return asDeploymentStage(stage); -}