envilder
Version:
A CLI and GitHub Action that securely centralizes your environment variables from AWS SSM or Azure Key Vault as a single source of truth
201 lines • 7.89 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateConfig = validateConfig;
exports.deploy = deploy;
const node_path_1 = __importDefault(require("node:path"));
/**
* CDK Infrastructure Deployment Entry Point
*/
const aws_cdk_lib_1 = require("aws-cdk-lib");
const types_1 = require("../lib/core/types");
const staticWebsiteStack_1 = require("../lib/stacks/staticWebsiteStack");
// ============================================================================
// Configuration
// ============================================================================
const config = {
repoName: "envilder",
branch: "main",
environment: types_1.AppEnvironment.Production,
domain: {
name: "envilder.com",
certificateId: "e04983fe-1561-4ebe-9166-83f77789964a",
hostedZoneId: "Z0718467FEEOZ35UNCTO",
},
stacks: {
frontend: {
staticWebsites: [
{
name: "Website",
projectPath: "envilder/src/website/dist",
},
],
},
},
};
// ============================================================================
// Utils
// ============================================================================
function getRootPath(rootPath) {
return rootPath !== null && rootPath !== void 0 ? rootPath : node_path_1.default.join(process.cwd(), "../../../");
}
function resolveFullPath(rootPath, relativePath) {
return node_path_1.default.join(rootPath, relativePath);
}
function logInfo(message) {
process.stderr.write(`${message}\x1b[E\n`);
}
function logError(error) {
process.stderr.write(`\x1b[31m❌ Error: ${error.message}\x1b[0m\x1b[E\n`);
if (error.stack) {
process.stderr.write(`${error.stack}\x1b[E\n`);
}
}
function logTable(entries) {
const MAX_VALUE_WIDTH = 40;
const truncate = (s) => 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);
}
function validateConfig(config) {
const errors = [];
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");
}
if (config.environment === undefined || config.environment === null) {
errors.push("environment is required and cannot be empty");
}
if (!config.domain) {
errors.push("domain configuration is required");
}
else {
if (!config.domain.name || config.domain.name.trim() === "") {
errors.push("domain.name is required and cannot be empty");
}
if (!config.domain.certificateId ||
config.domain.certificateId.trim() === "") {
errors.push("domain.certificateId is required and cannot be empty");
}
if (!config.domain.hostedZoneId ||
config.domain.hostedZoneId.trim() === "") {
errors.push("domain.hostedZoneId is required and cannot be empty");
}
}
if (!config.stacks) {
errors.push("stacks configuration is required");
}
else {
if (!config.stacks.frontend) {
errors.push("stacks.frontend is required");
}
else {
const { staticWebsites } = config.stacks.frontend;
if (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
// ============================================================================
function deploy(configOverride) {
const effectiveConfig = configOverride !== null && configOverride !== void 0 ? configOverride : config;
const rootPath = getRootPath(effectiveConfig.rootPath);
try {
validateConfig(effectiveConfig);
// Log deployment info
const entries = [
{ 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 });
}
if (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 aws_cdk_lib_1.App();
const envFromCli = {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
};
const stacks = [];
for (const websiteConfig of effectiveConfig.stacks.frontend
.staticWebsites) {
const distFolderPath = resolveFullPath(rootPath, websiteConfig.projectPath);
const stack = new staticWebsiteStack_1.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) {
if (error instanceof Error) {
logError(error);
}
throw error;
}
}
if (!process.env.VITEST) {
deploy();
}
//# sourceMappingURL=main.js.map