forked from expo/expo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[expo-constants] Source env when creating app config (expo#29099)
# Why Let's say you have a **.env** with the following config: ``` HELLO_WORLD=123 ``` Then you add that to your `extra` in **app.config.js** ```js { ... "extra": { "helloWorld": process.env.HELLO_WORLD ?? 'ENV VAR NOT DEFINED' } } ``` Then in your app you read the value: ```js export default function App() { return ( <View style={styles.container}> <Text>process.env.HELLO_WORLD: {Constants.expoConfig.extra.helloWorld}</Text> </View> ); } ``` Then you run a build in Expo Go, you will see the value 123. Similarly, if you load it in a dev build, it show the same result. But if you run `npx expo run:android --variant release`, then you will see "ENV VAR NOT DEFINED". Similarly, if you committed that **.env** file to your app (or loaded it in a pre-install hook) on EAS Build, then you will see "ENV VAR NOT DEFINED". This happens because we do not load `@expo/env` in our **getAppConfig.js** script in `expo-constants`. # How Load the env before calling `getConfig(..)` from **getAppConfig.js**. # Test Plan - Applied patch with patch-package - Ran `npx expo run:ios --configuration Release`, `npx expo run:android --variant release`, `eas build -p all --profile preview` - verified in each case that the env is loaded as expected.
- Loading branch information
1 parent
0cb8c0a
commit f0075a4
Showing
17 changed files
with
146 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/expo-constants/scripts/__tests__/getAppConfig-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const { getConfig } = require('@expo/config'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { load } = require('@expo/env'); | ||
|
||
jest.mock('fs'); | ||
jest.mock('path'); | ||
jest.mock('@expo/config'); | ||
jest.mock('@expo/env', () => ({ | ||
load: jest.fn(), | ||
})); | ||
|
||
const possibleProjectRoot = '/path/to/project'; | ||
const destinationDir = '/path/to/destination'; | ||
const mockExpConfig = { some: 'config' }; | ||
|
||
describe('getAppConfig', () => { | ||
beforeAll(() => { | ||
jest.resetAllMocks(); | ||
// Mock the arguments | ||
process.argv[2] = possibleProjectRoot; | ||
process.argv[3] = destinationDir; | ||
|
||
fs.existsSync.mockImplementation((filePath) => { | ||
if (filePath === path.join(possibleProjectRoot, 'package.json')) { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
path.join.mockImplementation((...args) => args.join('/')); | ||
path.resolve.mockImplementation((...args) => args.join('/')); | ||
jest.spyOn(process, 'chdir').mockImplementation(() => {}); | ||
getConfig.mockReturnValue({ exp: mockExpConfig }); | ||
|
||
// Import the script (this runs the script) | ||
require('../build/getAppConfig'); | ||
}); | ||
|
||
it('should call writeFileSync with the correct parameters', () => { | ||
// Verify writeFileSync was called with the expected arguments | ||
expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
path.join(destinationDir, 'app.config'), | ||
JSON.stringify(mockExpConfig) | ||
); | ||
}); | ||
|
||
it('should load from @expo/env', () => { | ||
expect(load).toHaveBeenCalledWith(possibleProjectRoot); | ||
}) | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1 @@ | ||
const { getConfig } = require('@expo/config'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const possibleProjectRoot = process.argv[2]; | ||
const destinationDir = process.argv[3]; | ||
|
||
// Remove projectRoot validation when we no longer support React Native <= 62 | ||
let projectRoot; | ||
if (fs.existsSync(path.join(possibleProjectRoot, 'package.json'))) { | ||
projectRoot = possibleProjectRoot; | ||
} else if (fs.existsSync(path.join(possibleProjectRoot, '..', 'package.json'))) { | ||
projectRoot = path.resolve(possibleProjectRoot, '..'); | ||
} | ||
|
||
process.chdir(projectRoot); | ||
|
||
const { exp } = getConfig(projectRoot, { | ||
isPublicConfig: true, | ||
skipSDKVersionRequirement: true, | ||
}); | ||
fs.writeFileSync(path.join(destinationDir, 'app.config'), JSON.stringify(exp)); | ||
require('./build/getAppConfig'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('expo-module-scripts/jest-preset-scripts'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { getConfig } from '@expo/config'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
const possibleProjectRoot = process.argv[2]; | ||
const destinationDir = process.argv[3]; | ||
|
||
// TODO: Verify we can remove projectRoot validation, now that we no longer | ||
// support React Native <= 62 | ||
let projectRoot; | ||
if (fs.existsSync(path.join(possibleProjectRoot, 'package.json'))) { | ||
projectRoot = possibleProjectRoot; | ||
} else if (fs.existsSync(path.join(possibleProjectRoot, '..', 'package.json'))) { | ||
projectRoot = path.resolve(possibleProjectRoot, '..'); | ||
} else { | ||
throw new Error( | ||
`Unable to locate project (no package.json found) at path: ${possibleProjectRoot}` | ||
); | ||
} | ||
|
||
require('@expo/env').load(projectRoot); | ||
process.chdir(projectRoot); | ||
|
||
const { exp } = getConfig(projectRoot, { | ||
isPublicConfig: true, | ||
skipSDKVersionRequirement: true, | ||
}); | ||
fs.writeFileSync(path.join(destinationDir, 'app.config'), JSON.stringify(exp)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "expo-module-scripts/tsconfig.plugin", | ||
"compilerOptions": { | ||
"outDir": "build", | ||
"rootDir": "src" | ||
}, | ||
"include": ["./src"], | ||
"exclude": ["**/__mocks__/*", "**/__tests__/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./babel.config.cli'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/** @type {import('jest').Config} */ | ||
module.exports = require('./jest-preset-cli'); |