Skip to content

Instantly share code, notes, and snippets.

@believer
Last active October 14, 2025 18:03
Show Gist options
  • Select an option

  • Save believer/4b7aace3383aea5bdf98da3a2c0c2eb6 to your computer and use it in GitHub Desktop.

Select an option

Save believer/4b7aace3383aea5bdf98da3a2c0c2eb6 to your computer and use it in GitHub Desktop.
ENV for React Native
# Here's a comment
TEST=test
API=http://www.example.com/api
NUMBER=1
import Foundation
@objc(Config)
class Config: NSObject {
@objc static func requiresMainQueueSetup() -> Bool {
return false
}
@objc var constantsToExport: [String: Any] {
return Environment.values
}
}
# #!/bin/bash
set -euo pipefail
ENV_NAME="${1:-dev}"
ENV_FILE=".env.$ENV_NAME"
APP_NAME=test
if [ ! -f "$ENV_FILE" ]; then
echo "⚠️ No env file found at $ENV_FILE"
exit 1
fi
# Generate Android Environment.kt
ANDROID_OUTPUT="android/app/src/main/java/com/$APP_NAME/Environment.kt"
mkdir -p "$(dirname "$ANDROID_OUTPUT")"
cat <<EOF > "$ANDROID_OUTPUT"
// Generated automatically from $ENV_FILE - do not edit
package com.$APP_NAME
object Environment {
val values: Map<String, String> = mapOf(
EOF
grep -Ev '^(#|[[:space:]]*$)' "$ENV_FILE" | while IFS='=' read -r key value; do
key_trimmed=$(echo "$key" | xargs)
value_trimmed=$(echo "$value" | xargs)
value_escaped=$(printf '%s' "$value_trimmed" | sed 's/"/\\"/g')
echo " \"$key_trimmed\" to \"$value_escaped\"," >> "$ANDROID_OUTPUT"
done
echo " )" >> "$ANDROID_OUTPUT"
echo "}" >> "$ANDROID_OUTPUT"
echo "✅ Android Environment.kt generated for $ENV_NAME"
#!/bin/bash
set -euo pipefail
# Get the env file for the configuration lowercased
ENV_FILE="$SRCROOT/../.env.${CONFIGURATION,,}"
if [ ! -f "$ENV_FILE" ]; then
echo "⚠️ No env file found at $ENV_FILE"
exit 0
fi
OUTPUT_FILE="$SRCROOT/Environment.swift"
echo "Generating $OUTPUT_FILE from $ENV_FILE"
# Start the Swift file
cat <<EOF > "$OUTPUT_FILE"
// Generated automatically from $ENV_FILE — do not edit.
@objcMembers
class Environment: NSObject {
static let values: [String: Any] = [
EOF
# Grab every non-comment, non-empty line
# Sort by the key and iterate over the values
# adding a line to the environment
grep -Ev '^(#|[[:space:]]*$)' "$ENV_FILE" | sort | while IFS='=' read -r key value; do
key_trimmed=$(echo "$key" | xargs)
value_trimmed=$(echo "$value" | xargs)
# Escape double quotes inside value
value_escaped=$(printf '%s' "$value_trimmed" | sed 's/"/\\"/g')
# Generate a line in the Swift dictionary
echo " \"$key_trimmed\" : \"$value_escaped\"" >> "$OUTPUT_FILE"
done
# Close file
echo " ]
}" >> "$OUTPUT_FILE"
echo "✅ Generated $OUTPUT_FILE"
// Generated automatically from .env.dev - do not edit
package com.myapp
object Environment {
val values: Map<String, String> = mapOf(
"TEST" to "test",
"API" to "http://www.example.com/api",
"NUMBER" to "1",
)
}
// Generated automatically from .env — do not edit.
@objcMembers
class Environment: NSObject {
static let values: [String: Any] = [
"API" : "http://www.example.com/api"
"NUMBER" : "1"
"TEST" : "test"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment