Skip to content

Commit 1511f03

Browse files
authored
Load .env variable from different types of .env file names (#446)
* Load .env variable from different types of .env file names * Update GraphQLConfigVariableAwareEndpoint.java Moved dotenv builder inside conditional * Requested change
1 parent e9734c1 commit 1511f03

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
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
18+
- Load variables from .env files. Supported file names: `.env.local`,`.env.development.local`,`.env.development`,`.env`
1919

2020
## Documentation
2121

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

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@
1414
import com.intellij.openapi.application.ApplicationManager;
1515
import com.intellij.openapi.fileEditor.FileDocumentManager;
1616
import com.intellij.openapi.project.Project;
17+
import com.intellij.openapi.project.ProjectUtil;
1718
import com.intellij.openapi.ui.DialogWrapper;
1819
import com.intellij.openapi.vfs.VirtualFile;
1920
import com.intellij.ui.components.JBLabel;
2021
import com.intellij.ui.components.JBTextField;
2122
import com.intellij.util.ui.FormBuilder;
2223
import io.github.cdimascio.dotenv.Dotenv;
24+
import io.github.cdimascio.dotenv.DotenvBuilder;
2325
import org.jetbrains.annotations.NotNull;
2426
import org.jetbrains.annotations.Nullable;
2527

2628
import javax.swing.*;
29+
import java.util.Arrays;
30+
import java.util.List;
2731
import java.util.Map;
2832
import java.util.function.Function;
2933
import java.util.regex.Matcher;
@@ -141,32 +145,59 @@ private String tryToGetVariableFromDotEnvFile(String varName) {
141145
if (configFile != null) {
142146
VirtualFile parentDir = configFile.getParent();
143147
if (parentDir != null) {
144-
dotenv = Dotenv
145-
.configure()
146-
.directory(parentDir.getPath())
147-
.ignoreIfMalformed()
148-
.ignoreIfMissing()
149-
.load();
150-
151-
varValue = dotenv.get(varName);
148+
String filename = getFileName(parentDir);
149+
if (filename != null) {
150+
dotenv = getDotenvBuilder(parentDir.getPath()).filename(filename).load();
151+
varValue = dotenv.get(varName);
152+
}
152153
}
153154
}
154155

155156
// If that didn't resolve try to load the env file from the root of the project
156157
String projectBasePath = project.getBasePath();
157158
if (varValue == null && projectBasePath != null) {
158-
dotenv = Dotenv
159-
.configure()
160-
.directory(projectBasePath)
161-
.ignoreIfMalformed()
162-
.ignoreIfMissing()
163-
.load();
164-
165-
varValue = dotenv.get(varName);
159+
VirtualFile projectDir = ProjectUtil.guessProjectDir(project);
160+
if (projectDir != null) {
161+
String filename = getFileName(projectDir);
162+
if (filename != null) {
163+
dotenv = getDotenvBuilder(projectDir.getPath()).filename(filename).load();
164+
varValue = dotenv.get(varName);
165+
}
166+
}
166167
}
167168
return varValue;
168169
}
169170

171+
private DotenvBuilder getDotenvBuilder(String path) {
172+
return Dotenv
173+
.configure()
174+
.directory(path)
175+
.ignoreIfMalformed()
176+
.ignoreIfMissing();
177+
}
178+
179+
private String getFileName(VirtualFile dirMaybeContainingEnvFile) {
180+
if (!dirMaybeContainingEnvFile.isDirectory())
181+
return null;
182+
183+
// List of supported names for .env files in prioritized order
184+
List<String> supportedEnvFiles = Arrays.asList(
185+
".env.local",
186+
".env.development.local",
187+
".env.development",
188+
".env");
189+
190+
// Look through the supported names and return the first we find
191+
for (String envFileName : supportedEnvFiles) {
192+
VirtualFile maybeEnvFile = dirMaybeContainingEnvFile.findChild(envFileName);
193+
if (maybeEnvFile != null && maybeEnvFile.exists()) {
194+
return envFileName;
195+
}
196+
}
197+
198+
return null;
199+
}
200+
170201
static class VariableDialog extends DialogWrapper {
171202

172203
private static Map<String, String> previousVariables = Maps.newHashMap();

0 commit comments

Comments
 (0)