|
| 1 | +// @ts-nocheck |
| 2 | +import { execSync } from "node:child_process"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { parseArgs } from "node:util"; |
| 7 | +import { parse as parseToml, stringify as stringifyToml } from "smol-toml"; |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | + |
| 12 | +const config = { |
| 13 | + preview: { |
| 14 | + docs: false, |
| 15 | + override: { |
| 16 | + name: "typix-preview", |
| 17 | + vars: { |
| 18 | + MODE: "client", |
| 19 | + PROVIDER_CLOUDFLARE_BUILTIN: true, |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + production: { |
| 24 | + docs: true, |
| 25 | + override: { |
| 26 | + vars: { |
| 27 | + MODE: "mixed", |
| 28 | + PROVIDER_CLOUDFLARE_BUILTIN: false, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * Read wrangler.toml file |
| 36 | + * @returns {any} Parsed TOML configuration object |
| 37 | + */ |
| 38 | +function readWranglerConfig() { |
| 39 | + const wranglerPath = path.join(__dirname, "..", "wrangler.toml"); |
| 40 | + const content = fs.readFileSync(wranglerPath, "utf8"); |
| 41 | + return parseToml(content); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Write wrangler.toml file |
| 46 | + * @param {any} config - Configuration object |
| 47 | + */ |
| 48 | +function writeWranglerConfig(config) { |
| 49 | + const wranglerPath = path.join(__dirname, "..", "wrangler.toml"); |
| 50 | + const content = stringifyToml(config); |
| 51 | + fs.writeFileSync(wranglerPath, content, "utf8"); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Deep merge objects |
| 56 | + * @param {any} target - Target object |
| 57 | + * @param {any} source - Source object |
| 58 | + * @returns {any} Merged object |
| 59 | + */ |
| 60 | +function deepMerge(target, source) { |
| 61 | + const result = { ...target }; |
| 62 | + |
| 63 | + for (const key in source) { |
| 64 | + if ( |
| 65 | + source[key] && |
| 66 | + typeof source[key] === "object" && |
| 67 | + !Array.isArray(source[key]) |
| 68 | + ) { |
| 69 | + result[key] = deepMerge(result[key] || {}, source[key]); |
| 70 | + } else { |
| 71 | + result[key] = source[key]; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return result; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Update wrangler configuration |
| 80 | + * @param {any} override - Configuration override for the environment |
| 81 | + * @param {any} baseConfig - Base configuration copy |
| 82 | + */ |
| 83 | +function updateWranglerConfig(override, baseConfig) { |
| 84 | + const updatedConfig = deepMerge(baseConfig, override); |
| 85 | + writeWranglerConfig(updatedConfig); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Build and deploy |
| 90 | + */ |
| 91 | +function buildAndDeploy(docs) { |
| 92 | + console.log("📦 Building application..."); |
| 93 | + execSync("pnpm build", { stdio: "inherit" }); |
| 94 | + console.log("✅ Application built successfully"); |
| 95 | + |
| 96 | + if (docs) { |
| 97 | + console.log("📚 Building documentation..."); |
| 98 | + execSync("pnpm build:docs", { stdio: "inherit" }); |
| 99 | + console.log("✅ Documentation built successfully"); |
| 100 | + |
| 101 | + console.log("📦 Documentation copied to dist/home successfully"); |
| 102 | + } |
| 103 | + |
| 104 | + console.log("🚀 Deploying to Cloudflare..."); |
| 105 | + execSync("pnpm deploy", { stdio: "inherit" }); |
| 106 | + console.log("✅ Deployed successfully"); |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Deploy multiple environments |
| 111 | + * @param {string[]} environments - Environment list |
| 112 | + */ |
| 113 | +function deployEnvironments(environments) { |
| 114 | + console.log( |
| 115 | + `🔄 Starting deployment for environments: ${environments.join(", ")}` |
| 116 | + ); |
| 117 | + |
| 118 | + // Read original configuration as a copy |
| 119 | + const baseConfig = readWranglerConfig(); |
| 120 | + console.log("📋 Base configuration loaded"); |
| 121 | + |
| 122 | + for (const environment of environments) { |
| 123 | + console.log(`\n--- Deploying ${environment} environment ---`); |
| 124 | + const envConfig = config[environment]; |
| 125 | + try { |
| 126 | + updateWranglerConfig(envConfig.override, baseConfig); |
| 127 | + console.log( |
| 128 | + `✅ ${environment} environment configuration updated successfully` |
| 129 | + ); |
| 130 | + |
| 131 | + // Build and deploy |
| 132 | + buildAndDeploy(envConfig.docs); |
| 133 | + } catch (error) { |
| 134 | + console.error( |
| 135 | + `❌ Failed to deploy ${environment} environment:`, |
| 136 | + error instanceof Error ? error.message : String(error) |
| 137 | + ); |
| 138 | + process.exit(1); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + console.log( |
| 143 | + `\n🎉 All environments (${environments.join(", ")}) deployed successfully!` |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +// Command line interface |
| 148 | +function main() { |
| 149 | + try { |
| 150 | + const { values } = parseArgs({ |
| 151 | + options: { |
| 152 | + preview: { |
| 153 | + type: "boolean", |
| 154 | + short: "p", |
| 155 | + default: false, |
| 156 | + }, |
| 157 | + production: { |
| 158 | + type: "boolean", |
| 159 | + short: "P", |
| 160 | + default: false, |
| 161 | + }, |
| 162 | + help: { |
| 163 | + type: "boolean", |
| 164 | + short: "h", |
| 165 | + default: false, |
| 166 | + }, |
| 167 | + }, |
| 168 | + allowPositionals: false, |
| 169 | + }); |
| 170 | + |
| 171 | + const environments = []; |
| 172 | + |
| 173 | + if (values.preview) { |
| 174 | + environments.push("preview"); |
| 175 | + } |
| 176 | + |
| 177 | + if (values.production) { |
| 178 | + environments.push("production"); |
| 179 | + } |
| 180 | + |
| 181 | + if (environments.length === 0) { |
| 182 | + environments.push("production"); |
| 183 | + } |
| 184 | + |
| 185 | + for (const env of environments) { |
| 186 | + if (!(env in config)) { |
| 187 | + console.error(`❌ Unknown environment: ${env}`); |
| 188 | + console.log("Available environments: production, preview"); |
| 189 | + process.exit(1); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + deployEnvironments(environments); |
| 194 | + } catch (error) { |
| 195 | + console.error( |
| 196 | + "❌ Error parsing arguments:", |
| 197 | + error instanceof Error ? error.message : String(error) |
| 198 | + ); |
| 199 | + console.log("Use --help for usage information"); |
| 200 | + process.exit(1); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +main(); |
0 commit comments