dotfiles/vim/lua/helpers.lua

40 lines
1.0 KiB
Lua
Raw Normal View History

-- Return the node_modules path for this project. If the project root contains a
-- frontend/ directory, assume node_modules lives there.
function Get_project_node_modules_path()
local root_path = vim.fn.getcwd()
local frontend_path = root_path .. "/frontend"
local is_frontend = vim.fn.isdirectory(frontend_path) ~= 0
local base_path = root_path
if is_frontend then
base_path = frontend_path
end
return base_path .. "/node_modules"
end
-- Return the project's go module path from go.mod, or an empty string if the file cannot be read.
function Get_project_go_module_path()
local root_path = vim.fn.getcwd()
local mod_path = root_path .. "/go.mod"
if vim.fn.filereadable(mod_path) == 0 then
return ""
end
local file = io.open(mod_path, "r")
if file == nil then
return ""
end
io.input(file)
-- The first line of go.mod, something like:
-- module github.com/username/repo
local line = io.read()
io.close(file)
local match = string.match(line, "^module (.+)")
if match == nil then
return ""
end
return match
end