Skip to content

Commit c54ae62

Browse files
committed
Read environment variables (#602)
1 parent 60990d3 commit c54ae62

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ GraphQL language support for WebStorm, IntelliJ IDEA and other IDEs based on the
1515
- Syntax highlighting, code-formatting, folding, commenter, and brace-matching
1616
- 'Find Usages' and 'Go to Declaration' for schema types, fields, and fragments
1717
- 'Structure view' to navigate GraphQL files
18-
- Load variables from .env files. Supported file names: `.env.local`,`.env.development.local`,`.env.development`,`.env.dev.local`,`.env.dev`,`.env`
18+
- Load variables from shell or .env files. Supported file names: `.env.local`,`.env.development.local`,`.env.development`,`.env.dev.local`,`.env.dev`,`.env`
1919
- Built-in Relay and Apollo Federation type definitions
2020

2121
## Documentation

src/main/com/intellij/lang/jsgraphql/ide/project/graphqlconfig/model/GraphQLConfigVariableAwareEndpoint.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
import com.intellij.openapi.project.Project;
1818
import com.intellij.openapi.project.ProjectUtil;
1919
import com.intellij.openapi.ui.DialogWrapper;
20+
import com.intellij.openapi.util.text.StringUtil;
2021
import com.intellij.openapi.vfs.VirtualFile;
2122
import com.intellij.ui.components.JBLabel;
2223
import com.intellij.ui.components.JBTextField;
24+
import com.intellij.util.EnvironmentUtil;
2325
import com.intellij.util.ui.FormBuilder;
2426
import io.github.cdimascio.dotenv.Dotenv;
2527
import io.github.cdimascio.dotenv.DotenvBuilder;
2628
import org.jetbrains.annotations.NotNull;
2729
import org.jetbrains.annotations.Nullable;
30+
import org.jetbrains.annotations.VisibleForTesting;
2831

2932
import javax.swing.*;
3033
import java.util.Arrays;
@@ -39,6 +42,7 @@
3942
*/
4043
public class GraphQLConfigVariableAwareEndpoint {
4144

45+
@VisibleForTesting
4246
Function<String, String> GET_ENV_VAR = System::getProperty;
4347

4448
private final static Pattern ENV_PATTERN = Pattern.compile("\\$\\{(?<var>[^}]*)}");
@@ -92,7 +96,7 @@ public String expandVariables(String rawValue) {
9296

9397
Matcher matcher = ENV_PATTERN.matcher(rawValue);
9498

95-
StringBuffer sb = new StringBuffer(rawValue.length());
99+
StringBuilder sb = new StringBuilder(rawValue.length());
96100
while (matcher.find()) {
97101
String var = matcher.group(1);
98102
String[] parts = var.split(":");
@@ -117,16 +121,17 @@ public String expandVariables(String rawValue) {
117121
continue;
118122
}
119123

120-
// Try to load the variable from the system
124+
// Try to load the variable from the jvm parameters
121125
String varValue = GET_ENV_VAR.apply(varName);
122-
123126
// If the variable wasn't found in the system try to see if the user has a .env file with the variable
124-
if (varValue == null || varValue.trim().isEmpty()) {
127+
if (StringUtil.isEmptyOrSpaces(varValue)) {
125128
varValue = tryToGetVariableFromDotEnvFile(varName);
126129
}
127-
130+
if (StringUtil.isEmptyOrSpaces(varValue)) {
131+
varValue = EnvironmentUtil.getValue(varName);
132+
}
128133
// If it still wasn't found present the user with a dialog to enter the variable
129-
if (varValue == null || varValue.trim().isEmpty()) {
134+
if (StringUtil.isEmptyOrSpaces(varValue)) {
130135
final VariableDialog dialog = new VariableDialog(project, varName);
131136
if (dialog.showAndGet()) {
132137
varValue = dialog.getValue();

0 commit comments

Comments
 (0)