Skip to content

Commit 0e358c1

Browse files
authored
Load .env files in dir of currently opened file (#63)
1 parent 155816b commit 0e358c1

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lua/rest-nvim/utils/init.lua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,24 @@ end
4848
-- read_env_file Reads the environment variables found in the `.env` file and
4949
-- returns a table with the variables
5050
M.read_env_file = function()
51+
5152
local variables = {}
52-
local env_file_path = vim.fn.getcwd() .. "/.env"
53+
54+
-- Directories to search for env files
55+
local env_file_paths = {
56+
-- current working directory
57+
vim.fn.getcwd() .. "/.env",
58+
-- directory of the currently opened file
59+
vim.fn.expand("%:p:h") .. "/.env"
60+
}
61+
5362
-- If there's an env file in the current working dir
54-
if M.file_exists(env_file_path) then
55-
for line in io.lines(env_file_path) do
56-
local vars = M.split(line, "=", 1)
57-
variables[vars[1]] = vars[2]
63+
for _, env_file_path in ipairs(env_file_paths) do
64+
if M.file_exists(env_file_path) then
65+
for line in io.lines(env_file_path) do
66+
local vars = M.split(line, "=", 1)
67+
variables[vars[1]] = vars[2]
68+
end
5869
end
5970
end
6071

0 commit comments

Comments
 (0)