Skip to content

Commit

Permalink
🐛 (ios) fix env retreiving on runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tpucci committed Jan 30, 2020
1 parent f71bf9d commit 38c5f60
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 12 deletions.
17 changes: 9 additions & 8 deletions README.md
@@ -1,8 +1,8 @@
<h1 align="center">React Native Config</h1>

<p align="center">Module to expose config variables to your javascript code in React Native, supporting both iOS and Android.</p>
<p align="center">Bring some [12 factor](http://12factor.net/config) love to your mobile apps!</p>
<p align="center">Forked from [luggit's repo](https://github.com/luggit/react-native-config)</p>
<p align="center">Bring some <a href="http://12factor.net/config">12 factor</a> love to your mobile apps!</p>
<p align="center">Forked from <a href="https://github.com/luggit/react-native-config">luggit's repo</a></p>

<p align="center">

Expand Down Expand Up @@ -86,14 +86,15 @@ pod install

# react-native-config codegen
ios/react-native-config.xcconfig
ios/envfile
```

- In the Xcode menu, go to Product > Scheme > Edit Scheme
- Expand the "Build" settings on left
- Click "Pre-actions", and under the plus sign select "New Run Script Action"
- Where it says "Type a script or drag a script file", type:
```
ENVFILE=.env ${SRCROOT}/../node_modules/@bam.tech/react-native-config/ios/ReactNativeConfig/BuildXCConfig.rb ${SRCROOT}/.. ${SRCROOT}/react-native-config.xcconfig
echo ".env.development" > "${SRCROOT}/envfile"
```

### Optional : Multi-environment support
Expand All @@ -114,7 +115,7 @@ For each environment, use the following step (and change `ANOTHER_ENV` by your e
## Javascript

```js
import Config from '@bam.tech/react-native-config';
import Config from "@bam.tech/react-native-config";

Config.API_URL; // 'https://myapi.com'
Config.GOOGLE_MAPS_API_KEY; // 'abcdefgh'
Expand Down Expand Up @@ -195,12 +196,12 @@ export default {
```js
// __mocks__/@bam.tech/react-native-config.js

import fs from 'fs';
import path from 'path';
import fs from "fs";
import path from "path";

import dotenv from 'dotenv';
import dotenv from "dotenv";

const buf = fs.readFileSync(path.resolve(__dirname, '..', '..', '.env'));
const buf = fs.readFileSync(path.resolve(__dirname, "..", "..", ".env"));
const config = dotenv.parse(buf);

export default config;
Expand Down
2 changes: 0 additions & 2 deletions ios/ReactNativeConfig/BuildDotenvConfig.rb
Expand Up @@ -25,6 +25,4 @@
path = File.join(m_output_path, 'GeneratedDotEnv.m')
File.open(path, 'w') { |f| f.puts template }

File.delete('/tmp/envfile') if custom_env

puts "Wrote to #{path}"
4 changes: 2 additions & 2 deletions ios/ReactNativeConfig/ReadDotEnv.rb
Expand Up @@ -12,9 +12,9 @@ def read_dot_env(envs_root)
puts "going to read env file from root folder #{envs_root}"

# pick a custom env file if set
if File.exist?('/tmp/envfile')
if File.exist?("#{envs_root}/ios/envfile")
custom_env = true
file = File.read('/tmp/envfile').strip
file = File.read("#{envs_root}/ios/envfile").strip
else
custom_env = false
file = ENV['ENVFILE'] || defaultEnvFile
Expand Down
Binary file removed readme-pics/1.ios_new_file.png
Binary file not shown.
Binary file removed readme-pics/2.ios_file_type.png
Binary file not shown.
Binary file removed readme-pics/3.ios_apply_config.png
Binary file not shown.

0 comments on commit 38c5f60

Please sign in to comment.