|
14 | 14 | import com.intellij.openapi.application.ApplicationManager; |
15 | 15 | import com.intellij.openapi.fileEditor.FileDocumentManager; |
16 | 16 | import com.intellij.openapi.project.Project; |
| 17 | +import com.intellij.openapi.project.ProjectUtil; |
17 | 18 | import com.intellij.openapi.ui.DialogWrapper; |
18 | 19 | import com.intellij.openapi.vfs.VirtualFile; |
19 | 20 | import com.intellij.ui.components.JBLabel; |
20 | 21 | import com.intellij.ui.components.JBTextField; |
21 | 22 | import com.intellij.util.ui.FormBuilder; |
22 | 23 | import io.github.cdimascio.dotenv.Dotenv; |
| 24 | +import io.github.cdimascio.dotenv.DotenvBuilder; |
23 | 25 | import org.jetbrains.annotations.NotNull; |
24 | 26 | import org.jetbrains.annotations.Nullable; |
25 | 27 |
|
26 | 28 | import javax.swing.*; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
27 | 31 | import java.util.Map; |
28 | 32 | import java.util.function.Function; |
29 | 33 | import java.util.regex.Matcher; |
@@ -141,32 +145,59 @@ private String tryToGetVariableFromDotEnvFile(String varName) { |
141 | 145 | if (configFile != null) { |
142 | 146 | VirtualFile parentDir = configFile.getParent(); |
143 | 147 | 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 | + } |
152 | 153 | } |
153 | 154 | } |
154 | 155 |
|
155 | 156 | // If that didn't resolve try to load the env file from the root of the project |
156 | 157 | String projectBasePath = project.getBasePath(); |
157 | 158 | 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 | + } |
166 | 167 | } |
167 | 168 | return varValue; |
168 | 169 | } |
169 | 170 |
|
| 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 | + |
170 | 201 | static class VariableDialog extends DialogWrapper { |
171 | 202 |
|
172 | 203 | private static Map<String, String> previousVariables = Maps.newHashMap(); |
|
0 commit comments