aboutsummaryrefslogtreecommitdiff
path: root/configs/nvim/lua/crupest-util.lua
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-09-08 20:18:59 +0800
committercrupest <crupest@outlook.com>2023-09-11 00:36:49 +0800
commitc2ead01d0d15ff7f6945fdd92d295d2ec9712963 (patch)
tree8d86e634afd5e83fafcb8615bc5050191cffbff1 /configs/nvim/lua/crupest-util.lua
parentd91d7d18af7e1c937cb8215f84588b24b4432086 (diff)
downloadcrupest-c2ead01d0d15ff7f6945fdd92d295d2ec9712963.tar.gz
crupest-c2ead01d0d15ff7f6945fdd92d295d2ec9712963.tar.bz2
crupest-c2ead01d0d15ff7f6945fdd92d295d2ec9712963.zip
Update neovim config.
Diffstat (limited to 'configs/nvim/lua/crupest-util.lua')
-rw-r--r--configs/nvim/lua/crupest-util.lua63
1 files changed, 63 insertions, 0 deletions
diff --git a/configs/nvim/lua/crupest-util.lua b/configs/nvim/lua/crupest-util.lua
new file mode 100644
index 0000000..dcfc706
--- /dev/null
+++ b/configs/nvim/lua/crupest-util.lua
@@ -0,0 +1,63 @@
+local M = {}
+
+M.clean_path = function (path)
+ return string.gsub(path, "[/\\]+", "/")
+end
+
+M.get_exe = function (path)
+ if vim.fn.has("win32") then
+ local suffixes = { ".exe", ".CMD", ".cmd", ".ps1" }
+ for _, v in ipairs(suffixes) do
+ if string.find(path, v.."$") and vim.uv.fs_stat(path) then
+ return path
+ end
+ end
+ for _, v in ipairs(suffixes) do
+ local p = path..v
+ if vim.uv.fs_stat(p) then return p end
+ end
+ return nil
+ end
+
+ if vim.fn.executable(path) then
+ return path
+ end
+ return nil
+end
+
+M.walk_up = function (path, func)
+ local current_path = vim.fn.fnamemodify(path, ":p")
+ while true do
+ print(current_path)
+ local result = func(current_path)
+ if result then
+ return result
+ end
+ local new_path = vim.fn.fnamemodify(current_path, ":p:h")
+ print(new_path)
+ if new_path == current_path then
+ break
+ end
+ end
+ return nil
+end
+
+M.find_node_modules = function (path)
+ return M.walk_up(path, function (current_path)
+ local node_modules_path = current_path.."/node_modules"
+ if vim.fn.isdirectory(node_modules_path) then
+ return node_modules_path
+ end
+ return nil
+ end)
+end
+
+M.find_npm_exe = function (path, exe)
+ local node_modules_path = M.find_node_modules(path)
+ if not node_modules_path then return nil end
+ local exe_path = M.get_exe(node_modules_path.."/.bin/"..exe)
+ if exe_path then return M.clean_path(exe_path) end
+ return nil
+end
+
+return M