Skip to content

Commit

Permalink
Add option to pass env variables from launch configuration (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagiera authored Apr 25, 2024
1 parent 1f289e1 commit c85e2fd
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/docs/docs/launch-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ Below is an example of how the `launch.json` file could look like with android v
Here, we list other attributes that can be configured using launch configuration which doesn't fit in any of the above categories:

- `appRoot` – Location of the React Native application root folder relative to the workspace. This is used for monorepo type setups when the workspace root is not the root of the React Native project. The IDE extension tries to locate the React Native application root automatically, but in case it failes to do so (i.e. there are multiple applications defined in the workspace), you can use this setting to override the location.
- `env` – Environment variables to be passed to all build/run commands that the IDE is launching.

Below is a sample `launch.json` config file with `appRoot` setting specified:
Below is a sample `launch.json` config file with `appRoot` and `env` setting specified:

```json
{
Expand All @@ -138,7 +139,10 @@ Below is a sample `launch.json` config file with `appRoot` setting specified:
"type": "react-native-ide",
"request": "launch",
"name": "React Native IDE panel",
"appRoot": "packages/mobile"
"appRoot": "packages/mobile",
"env": {
"MY_SECRET_KEY": "bananas"
}
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions packages/vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
"type": "string",
"description": "Location of the React Native application root folder relative to the workspace. This is used for monorepo type setups when the workspace root is not the root of the React Native project. The IDE extension tries to locate the React Native application root automatically, but in case it failes to do so (i.e. there are multiple applications defined in the workspace), you can use this setting to override the location."
},
"env": {
"type": "object",
"description": "Environment variables to be passed to all build/run commands that the IDE is launching."
},
"ios": {
"description": "Provides a way to customize Xcode builds for iOS",
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/builders/buildAndroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function buildAndroid(
const buildProcess = cancelToken.adapt(
exec("./gradlew", gradleArgs, {
cwd: androidSourceDir,
env: { ...process.env, JAVA_HOME, ANDROID_HOME },
env: { ...process.env, ...buildOptions.env, JAVA_HOME, ANDROID_HOME },

Check warning on line 77 in packages/vscode-extension/src/builders/buildAndroid.ts

View workflow job for this annotation

GitHub Actions / check

Object Literal Property name `JAVA_HOME` must match one of the following formats: camelCase

Check warning on line 77 in packages/vscode-extension/src/builders/buildAndroid.ts

View workflow job for this annotation

GitHub Actions / check

Object Literal Property name `ANDROID_HOME` must match one of the following formats: camelCase
buffer: false,
})
);
Expand Down
1 change: 1 addition & 0 deletions packages/vscode-extension/src/builders/buildIOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function buildProject(
return exec("xcodebuild", xcodebuildArgs, {
env: {
...process.env,
...getLaunchConfiguration().env,
RCT_NO_LAUNCH_PACKAGER: "true",

Check warning on line 97 in packages/vscode-extension/src/builders/buildIOS.ts

View workflow job for this annotation

GitHub Actions / check

Object Literal Property name `RCT_NO_LAUNCH_PACKAGER` must match one of the following formats: camelCase
},
cwd: buildDir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DependencyChecker } from "./DependencyChecker";
import { command } from "../utilities/subprocess";
import { getIosSourceDir } from "../builders/buildIOS";
import { getAppRootFolder } from "../utilities/extensionContext";
import { getLaunchConfiguration } from "../utilities/launchConfiguration";

export class DependencyInstaller implements Disposable {
private webview: Webview;
Expand Down Expand Up @@ -70,6 +71,7 @@ export function installIOSDependencies(appRootFolder: string, forceCleanBuild: b
cwd: iosDirPath,
env: {
...process.env,
...getLaunchConfiguration().env,
LANG: "en_US.UTF-8",
},
});
Expand Down
2 changes: 2 additions & 0 deletions packages/vscode-extension/src/project/metro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Logger } from "../Logger";
import { extensionContext, getAppRootFolder } from "../utilities/extensionContext";
import { Devtools } from "./devtools";
import stripAnsi from "strip-ansi";
import { getLaunchConfiguration } from "../utilities/launchConfiguration";

export interface MetroDelegate {
onBundleError(): void;
Expand Down Expand Up @@ -133,6 +134,7 @@ export class Metro implements Disposable {

const metroEnv = {
...process.env,
...getLaunchConfiguration().env,
NODE_PATH: path.join(appRootFolder, "node_modules"),
RCT_METRO_PORT: "0",
RCT_DEVTOOLS_PORT: this.devtools.port.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { workspace } from "vscode";

export type LaunchConfigurationOptions = {
appRoot: string | undefined;
env: Record<string, string> | undefined;
ios:
| {
scheme: string | undefined;
Expand Down

0 comments on commit c85e2fd

Please sign in to comment.