Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | 1x 2x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 6x 1x 1x 4x 4x 2x 4x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | #!/usr/bin/env node
import path from "node:path";
import type { Environment, Stack } from "aws-cdk-lib";
/**
* CDK Infrastructure Deployment Entry Point
*/
import { App } from "aws-cdk-lib";
import { AppEnvironment } from "../lib/core/types";
import { StaticWebsiteStack } from "../lib/stacks/staticWebsiteStack";
// ============================================================================
// Types
// ============================================================================
interface StaticWebsiteConfig {
name: string;
projectPath: string;
subdomain?: string;
}
interface DeploymentConfig {
repoName: string;
branch: string;
environment: AppEnvironment;
domain: {
name: string;
certificateId: string;
hostedZoneId: string;
};
stacks: {
frontend: {
staticWebsites: readonly StaticWebsiteConfig[];
};
};
rootPath?: string;
}
// ============================================================================
// Configuration
// ============================================================================
const config: DeploymentConfig = {
repoName: "envilder",
branch: "main",
environment: AppEnvironment.Production,
domain: {
name: "envilder.com",
certificateId: "e04983fe-1561-4ebe-9166-83f77789964a",
hostedZoneId: "Z0718467FEEOZ35UNCTO",
},
stacks: {
frontend: {
staticWebsites: [
{
name: "Website",
projectPath: "envilder/src/apps/website/dist",
},
],
},
},
};
// ============================================================================
// Utils
// ============================================================================
function getRootPath(rootPath?: string): string {
return rootPath ?? path.join(process.cwd(), "../../../");
}
function resolveFullPath(rootPath: string, relativePath: string): string {
return path.join(rootPath, relativePath);
}
function logInfo(message: string): void {
process.stderr.write(`${message}\x1b[E\n`);
}
function logError(error: Error): void {
process.stderr.write(`\x1b[31m❌ Error: ${error.message}\x1b[0m\x1b[E\n`);
Eif (error.stack) {
process.stderr.write(`${error.stack}\x1b[E\n`);
}
}
function logTable(
entries: ReadonlyArray<{ label: string; value: string }>,
): void {
const MAX_VALUE_WIDTH = 40;
const truncate = (s: string) =>
s.length > MAX_VALUE_WIDTH ? `…${s.slice(-(MAX_VALUE_WIDTH - 1))}` : s;
const rows = entries.map(({ label, value }) => ({
label,
value: truncate(value),
}));
const maxLabel = Math.max(...rows.map((e) => e.label.length));
const maxValue = Math.max(...rows.map((e) => e.value.length));
const header = " 📁 Deployment Info ";
const innerWidth = maxLabel + maxValue + 4;
const padding = Math.max(0, innerWidth - header.length);
const nl = "\x1b[E\n";
process.stderr.write(nl);
process.stderr.write(`╭─${header}${"─".repeat(padding)}╮${nl}`);
for (const { label, value } of rows) {
process.stderr.write(
`│ ${label.padEnd(maxLabel)} │ ${value.padEnd(maxValue)} │${nl}`,
);
}
process.stderr.write(
`╰${"─".repeat(maxLabel + 2)}┴${"─".repeat(maxValue + 2)}╯${nl}`,
);
process.stderr.write(nl);
}
export function validateConfig(config: DeploymentConfig): void {
const errors: string[] = [];
if (!config.repoName || config.repoName.trim() === "") {
errors.push("repoName is required and cannot be empty");
}
if (!config.branch || config.branch.trim() === "") {
errors.push("branch is required and cannot be empty");
}
Iif (config.environment === undefined || config.environment === null) {
errors.push("environment is required and cannot be empty");
}
Iif (!config.domain) {
errors.push("domain configuration is required");
} else {
Iif (!config.domain.name || config.domain.name.trim() === "") {
errors.push("domain.name is required and cannot be empty");
}
Iif (
!config.domain.certificateId ||
config.domain.certificateId.trim() === ""
) {
errors.push("domain.certificateId is required and cannot be empty");
}
Iif (
!config.domain.hostedZoneId ||
config.domain.hostedZoneId.trim() === ""
) {
errors.push("domain.hostedZoneId is required and cannot be empty");
}
}
Iif (!config.stacks) {
errors.push("stacks configuration is required");
} else {
Iif (!config.stacks.frontend) {
errors.push("stacks.frontend is required");
} else {
const { staticWebsites } = config.stacks.frontend;
Eif (staticWebsites) {
for (const [index, website] of staticWebsites.entries()) {
if (!website.name || website.name.trim() === "") {
errors.push(`frontend.staticWebsites[${index}].name is required`);
}
if (!website.projectPath || website.projectPath.trim() === "") {
errors.push(
`frontend.staticWebsites[${index}].projectPath is required`,
);
}
}
}
}
}
if (errors.length > 0) {
throw new Error(
`Configuration validation failed with ${errors.length} error(s):\n${errors.join("\n")}`,
);
}
}
// ============================================================================
// Deployment
// ============================================================================
export function deploy(configOverride?: DeploymentConfig): Stack[] {
const effectiveConfig = configOverride ?? config;
const rootPath = getRootPath(effectiveConfig.rootPath);
try {
validateConfig(effectiveConfig);
// Log deployment info
const entries: Array<{ label: string; value: string }> = [
{ label: "Repository", value: effectiveConfig.repoName },
{ label: "Branch", value: effectiveConfig.branch },
{ label: "Environment", value: String(effectiveConfig.environment) },
];
if (process.env.CDK_DEFAULT_REGION) {
entries.push({ label: "Region", value: process.env.CDK_DEFAULT_REGION });
}
Eif (process.env.CDK_DEFAULT_ACCOUNT) {
entries.push({
label: "Account",
value: `***${process.env.CDK_DEFAULT_ACCOUNT.slice(-4)}`,
});
}
entries.push({ label: "Root Path", value: rootPath });
for (const ws of effectiveConfig.stacks.frontend.staticWebsites) {
entries.push({
label: ws.name,
value: resolveFullPath(rootPath, ws.projectPath),
});
}
logTable(entries);
logInfo("🎯 Requested stacks:");
const app = new App();
const envFromCli: Environment = {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
};
const stacks: Stack[] = [];
for (const websiteConfig of effectiveConfig.stacks.frontend
.staticWebsites) {
const distFolderPath = resolveFullPath(
rootPath,
websiteConfig.projectPath,
);
const stack = new StaticWebsiteStack(app, {
env: envFromCli,
name: websiteConfig.name,
domains: [
{
subdomain: websiteConfig.subdomain,
domainName: effectiveConfig.domain.name,
certificateId: effectiveConfig.domain.certificateId,
hostedZoneId: effectiveConfig.domain.hostedZoneId,
},
],
distFolderPath,
envName: effectiveConfig.environment,
githubRepo: effectiveConfig.repoName,
stackName: `${effectiveConfig.repoName}-${websiteConfig.name}`,
});
stacks.push(stack);
}
return stacks;
} catch (error) {
Eif (error instanceof Error) {
logError(error);
}
throw error;
}
}
Iif (!process.env.VITEST) {
deploy();
}
|