React Native Envs Example
How it works
You have the environments in the ./envs folder.
In this example the ./envs/general.js are general envs, for all environments, and you can override in each environment.
All of it is just JavaScript objects. So you can create yours own environments and other files to share between your environments and override if needed.
And we install this folder as a dependencie:
yarn add "./envs"
# or
npm i "./envs"How to use
import React from 'react';
import {SafeAreaView, View, Text} from 'react-native';
import envs from 'envs';
const App = () => (
<SafeAreaView>
<View>
<Text>IS_ANDROID: {envs.IS_ANDROID ? 'true' : 'false'}</Text>
<Text>IS_IOS: {envs.IS_IOS ? 'true' : 'false'}</Text>
<Text>APP_NAME: {envs.APP_NAME}</Text>
<Text>APP_ENV: {envs.APP_ENV}</Text>
<Text>API_HOST: {envs.API_HOST}</Text>
</View>
</SafeAreaView>
);
export default App;Setting each environment
Use the scripts in package.json (if you create new environment create the scripts properly scritps).
Set development
yarn set:developmentSet production
yarn set:prodSet staging
yarn set:stagingUse the script before the commands to run in dev and to build, you can see examples in the package.json file.
Warning: env.js file
You should keep the ./envs/env.js file in your repository, this way you won't have trouble with ESLint and tests with the import failling (especially if you are, for example, using git hooks with Husky for ESLint check and tests).
To avoid git keep tracking the changes in this file you can locally run:
git update-index --skip-worktree ./envs/env.jsTo track this file again:
git update-index --no-skip-worktree ./envs/env.jsWarning: set scripts
The scripts to set environments are tested only in MacOS. Not sure if works properly in Windows.
"set:development": "echo \"export default 'development';\"> ./envs/env.js",
"set:prod": "echo \"export default 'production';\"> ./envs/env.js",
"set:staging": "echo \"export default 'staging';\"> ./envs/env.js",License
This project is licensed under the MIT License - see the LICENSE.md file for details.