|
| 1 | +import * as z from "zod/mini"; |
| 2 | + |
| 3 | +// Load English locale for better error messages |
| 4 | +z.config(z.locales.en()); |
| 5 | + |
| 6 | +export interface TestConfig { |
| 7 | + projectId: string; |
| 8 | + testRunId: string; |
| 9 | + runtime: "node" | "python"; |
| 10 | + nodeVersion: string; |
| 11 | + firebaseAdmin: string; |
| 12 | + region: string; |
| 13 | + storageRegion: string; |
| 14 | + debug?: string; |
| 15 | + databaseUrl: string; |
| 16 | + storageBucket: string; |
| 17 | + firebaseAppId: string; |
| 18 | + firebaseMeasurementId: string; |
| 19 | + firebaseAuthDomain: string; |
| 20 | + firebaseApiKey: string; |
| 21 | + googleAnalyticsApiSecret: string; |
| 22 | +} |
| 23 | + |
| 24 | +// Environment validation schema |
| 25 | +const environmentSchema = z.object({ |
| 26 | + PROJECT_ID: z.string().check(z.minLength(1, "PROJECT_ID is required")), |
| 27 | + DATABASE_URL: z.string().check(z.minLength(1, "DATABASE_URL is required")), |
| 28 | + STORAGE_BUCKET: z.string().check(z.minLength(1, "STORAGE_BUCKET is required")), |
| 29 | + FIREBASE_APP_ID: z.string().check(z.minLength(1, "FIREBASE_APP_ID is required")), |
| 30 | + FIREBASE_MEASUREMENT_ID: z.string().check(z.minLength(1, "FIREBASE_MEASUREMENT_ID is required")), |
| 31 | + FIREBASE_AUTH_DOMAIN: z.string().check(z.minLength(1, "FIREBASE_AUTH_DOMAIN is required")), |
| 32 | + FIREBASE_API_KEY: z.string().check(z.minLength(1, "FIREBASE_API_KEY is required")), |
| 33 | + GOOGLE_ANALYTICS_API_SECRET: z |
| 34 | + .string() |
| 35 | + .check(z.minLength(1, "GOOGLE_ANALYTICS_API_SECRET is required")), |
| 36 | + TEST_RUNTIME: z.enum(["node", "python"]), |
| 37 | + NODE_VERSION: z.optional(z.string()), |
| 38 | + FIREBASE_ADMIN: z.optional(z.string()), |
| 39 | + REGION: z.optional(z.string()), |
| 40 | + STORAGE_REGION: z.optional(z.string()), |
| 41 | + DEBUG: z.optional(z.string()), |
| 42 | +}); |
| 43 | + |
| 44 | +/** |
| 45 | + * Validates that all required environment variables are set and have valid values. |
| 46 | + * Exits the process with code 1 if validation fails. |
| 47 | + */ |
| 48 | +export function validateEnvironment(): void { |
| 49 | + try { |
| 50 | + environmentSchema.parse(process.env); |
| 51 | + } catch (error) { |
| 52 | + console.error("Environment validation failed:"); |
| 53 | + if (error && typeof error === "object" && "errors" in error) { |
| 54 | + const zodError = error as { errors: Array<{ path: string[]; message: string }> }; |
| 55 | + zodError.errors.forEach((err) => { |
| 56 | + console.error(` ${err.path.join(".")}: ${err.message}`); |
| 57 | + }); |
| 58 | + } else { |
| 59 | + console.error("Unexpected error during environment validation:", error); |
| 60 | + } |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Loads and validates environment configuration, returning a typed config object. |
| 67 | + * @returns TestConfig object with all validated environment variables |
| 68 | + */ |
| 69 | +export function loadConfig(): TestConfig { |
| 70 | + // Validate environment first to ensure all required variables are set |
| 71 | + const validatedEnv = environmentSchema.parse(process.env); |
| 72 | + |
| 73 | + // TypeScript type guard to ensure TEST_RUNTIME is the correct type |
| 74 | + const validRuntimes = ["node", "python"] as const; |
| 75 | + type ValidRuntime = (typeof validRuntimes)[number]; |
| 76 | + const runtime: ValidRuntime = validatedEnv.TEST_RUNTIME; |
| 77 | + |
| 78 | + let firebaseAdmin = validatedEnv.FIREBASE_ADMIN; |
| 79 | + if (!firebaseAdmin && runtime === "node") { |
| 80 | + firebaseAdmin = "^12.0.0"; |
| 81 | + } else if (!firebaseAdmin && runtime === "python") { |
| 82 | + firebaseAdmin = "6.5.0"; |
| 83 | + } else if (!firebaseAdmin) { |
| 84 | + throw new Error("FIREBASE_ADMIN is not set"); |
| 85 | + } |
| 86 | + |
| 87 | + const testRunId = `t${Date.now()}`; |
| 88 | + |
| 89 | + return { |
| 90 | + projectId: validatedEnv.PROJECT_ID, |
| 91 | + testRunId, |
| 92 | + runtime, |
| 93 | + nodeVersion: validatedEnv.NODE_VERSION ?? "18", |
| 94 | + firebaseAdmin, |
| 95 | + region: validatedEnv.REGION ?? "us-central1", |
| 96 | + storageRegion: validatedEnv.STORAGE_REGION ?? "us-central1", |
| 97 | + debug: validatedEnv.DEBUG, |
| 98 | + databaseUrl: validatedEnv.DATABASE_URL, |
| 99 | + storageBucket: validatedEnv.STORAGE_BUCKET, |
| 100 | + firebaseAppId: validatedEnv.FIREBASE_APP_ID, |
| 101 | + firebaseMeasurementId: validatedEnv.FIREBASE_MEASUREMENT_ID, |
| 102 | + firebaseAuthDomain: validatedEnv.FIREBASE_AUTH_DOMAIN, |
| 103 | + firebaseApiKey: validatedEnv.FIREBASE_API_KEY, |
| 104 | + googleAnalyticsApiSecret: validatedEnv.GOOGLE_ANALYTICS_API_SECRET, |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Creates Firebase configuration object for deployment. |
| 110 | + * @param config - The test configuration object |
| 111 | + * @returns Firebase configuration object |
| 112 | + */ |
| 113 | +export function createFirebaseConfig(config: TestConfig) { |
| 114 | + return { |
| 115 | + projectId: config.projectId, |
| 116 | + projectDir: process.cwd(), |
| 117 | + sourceDir: `${process.cwd()}/functions`, |
| 118 | + runtime: config.runtime === "node" ? "nodejs18" : "python311", |
| 119 | + }; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * Creates environment configuration for function deployment. |
| 124 | + * @param config - The test configuration object |
| 125 | + * @returns Environment configuration object |
| 126 | + */ |
| 127 | +export function createEnvironmentConfig(config: TestConfig) { |
| 128 | + const firebaseConfig = { |
| 129 | + databaseURL: config.databaseUrl, |
| 130 | + projectId: config.projectId, |
| 131 | + storageBucket: config.storageBucket, |
| 132 | + }; |
| 133 | + |
| 134 | + return { |
| 135 | + DEBUG: config.debug, |
| 136 | + FIRESTORE_PREFER_REST: "true", |
| 137 | + GCLOUD_PROJECT: config.projectId, |
| 138 | + FIREBASE_CONFIG: JSON.stringify(firebaseConfig), |
| 139 | + REGION: config.region, |
| 140 | + STORAGE_REGION: config.storageRegion, |
| 141 | + }; |
| 142 | +} |
0 commit comments