diff options
author | crupest <crupest@outlook.com> | 2023-09-08 17:57:37 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-09-13 14:43:16 +0800 |
commit | f6872f1b55c73d6382819cb531162a4b18ca621b (patch) | |
tree | 2edc1c0a7f2f071f8bd41971114ade19ced37216 /configs/nvim/lua/crupest/system.lua | |
parent | e1cf1bfebeca544abb34e860d86a89900b74df09 (diff) | |
download | crupest-f6872f1b55c73d6382819cb531162a4b18ca621b.tar.gz crupest-f6872f1b55c73d6382819cb531162a4b18ca621b.tar.bz2 crupest-f6872f1b55c73d6382819cb531162a4b18ca621b.zip |
Update nvim config.
Diffstat (limited to 'configs/nvim/lua/crupest/system.lua')
-rw-r--r-- | configs/nvim/lua/crupest/system.lua | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/configs/nvim/lua/crupest/system.lua b/configs/nvim/lua/crupest/system.lua new file mode 100644 index 0000000..74f8670 --- /dev/null +++ b/configs/nvim/lua/crupest/system.lua @@ -0,0 +1,69 @@ +local function clean_path(path) + return path and (string.gsub(path, "[/\\]+", "/")) +end + +local function get_exe(path) + if vim.fn.has("win32") ~= 0 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) ~= 0 then + return path + end + + return nil +end + +local function walk_up(path, func) + local current_path = vim.fn.fnamemodify(path, ":p") + while true do + local result = func(current_path) + if result then + return result + end + local new_path = vim.fn.fnamemodify(current_path, ":h") + if new_path == current_path then + break + end + current_path = new_path + end + return nil +end + +local function find_node_modules(path) + return walk_up(path, function (current_path) + local node_modules_path = current_path.."/node_modules" + if vim.fn.isdirectory(node_modules_path) ~= 0 then + return node_modules_path + end + return nil + end) +end + +local function find_npm_exe(path, exe) + local node_modules_path = find_node_modules(path) + if not node_modules_path then return nil end + local try_exe_path = node_modules_path.."/.bin/"..exe + local exe_path = get_exe(try_exe_path) + if exe_path then return clean_path(exe_path) end + return nil +end + +return { + clean_path = clean_path, + get_exe = get_exe, + walk_up = walk_up, + find_node_modules = find_node_modules, + find_npm_exe = find_npm_exe +} + |