rn-env-setup
v1.3.0
Published
CLI to automate Android product flavors, iOS schemes/xcconfig, and JS env generation for React Native multi-environment apps
Maintainers
Readme
rn-env-setup
Set up and manage multiple React Native environments (like dev, staging, prod) from one config file.
Install
npm install -g rn-env-setup
# or use without installing:
npx rn-env-setup initQuick Start
cd your-react-native-app
npx rn-env-setup init
npx rn-env-setup validate
npx rn-env-setup generateThis creates native environment setup for Android + iOS and generates helpful scripts in your app package.json.
Minimal rn-env.yaml
appName: MyApp
packageName: com.myapp
jsEnvStrategy: react-native-config
generateJsEnv: true
generateCiSnippets: false
enableRollback: true
environments:
- name: dev
appName: MyApp Dev
bundleId: com.myapp.dev
apiUrl: https://dev.api.example.com
variables:
FEATURE_NEW_UI: true
- name: staging
appName: MyApp Staging
bundleId: com.myapp.staging
apiUrl: https://staging.api.example.com
- name: prod
appName: MyApp
bundleId: com.myapp
apiUrl: https://api.example.comCLI Commands
rn-env-setup init: Interactive setup and createsrn-env.yamlrn-env-setup validate: Validates config onlyrn-env-setup generate: Applies Android + iOS + JS env generationrn-env-setup generate --dry-run: Preview changes onlyrn-env-setup generate --android-only: Skip iOS generationrn-env-setup generate --ios-only: Skip Android generationrn-env-setup generate --config path/to/rn-env.yaml: Use a custom config pathrn-env-setup rollback: Restores backed-up files
Common Scripts Generated In Your App
After generate, your app package.json gets scripts like:
env:android:run:devenv:android:apk:dev:debugenv:android:apk:dev:releaseenv:android:aab:dev:releaseenv:ios:run:dev
(And equivalent scripts for each environment name.)
FAQ
1) How do I edit environments and regenerate?
- Update
rn-env.yaml:- add/remove environments
- change
bundleId,apiUrl,variables, app names
- Run:
npx rn-env-setup validate
npx rn-env-setup generateUse this first if you want to preview:
npx rn-env-setup generate --dry-run2) How do I add Firebase google-services.json for each environment?
Use Android flavor-specific files:
android/app/src/dev/google-services.jsonandroid/app/src/staging/google-services.jsonandroid/app/src/prod/google-services.json
You can also keep a default fallback at:
android/app/google-services.json
Make sure your Android Firebase Gradle setup is already present in your RN app (com.google.gms.google-services plugin + classpath).
If you also use iOS Firebase, keep environment-specific GoogleService-Info.plist files and select/copy the right one per scheme/build step.
3) How do I run the app for an environment?
Use generated scripts:
npm run env:android:run:dev
npm run env:ios:run:devReplace dev with staging or prod.
4) How do I build APK, AAB, and IPA for different environments?
Android:
npm run env:android:apk:dev:debug
npm run env:android:apk:dev:release
npm run env:android:aab:dev:releaseiOS archive (IPA flow):
cd ios
xcodebuild -workspace MyApp.xcworkspace -scheme MyApp-Dev -configuration Dev -archivePath build/MyApp-Dev.xcarchive archiveThen export IPA using your ExportOptions.plist:
xcodebuild -exportArchive -archivePath build/MyApp-Dev.xcarchive -exportPath build -exportOptionsPlist ExportOptions.plist5) How do I use environment variables or base URL in code?
Each environment gets apiUrl and optional variables from rn-env.yaml.
If jsEnvStrategy: react-native-config:
import Config from 'react-native-config';
const baseUrl = Config.API_URL;
const featureFlag = Config.FEATURE_NEW_UI;If jsEnvStrategy: custom, use generated src/env.ts:
import Config from './src/env';
const baseUrl = Config.API_URL;6) How do I write test cases for different environments?
Use environment-specific test runs.
Jest example (API URL assertions):
const envs = ['dev', 'staging', 'prod'];
envs.forEach((env) => {
test(`API URL exists for ${env}`, () => {
process.env.ENV = env;
// load env config and assert URL/flags
});
});Recommended strategy:
- Unit tests: validate env config mapping and feature flags
- Integration tests: mock API client per environment base URL
- E2E tests: run per environment build/scheme in CI matrix
License
MIT
