aboutsummaryrefslogtreecommitdiff
path: root/configs/nvim/lua/crupest/system/find.lua
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2023-10-02 00:02:38 +0800
committercrupest <crupest@outlook.com>2023-10-02 22:48:22 +0800
commit66ceacf580d7d2d7f5893dd4bae993b7f135ae14 (patch)
treeb4f7b0ad5a60086aa2b0cea250492efad5634545 /configs/nvim/lua/crupest/system/find.lua
parent412ad3daceb6d3bcd5c5c2832a5d87a622556477 (diff)
downloadcrupest-66ceacf580d7d2d7f5893dd4bae993b7f135ae14.tar.gz
crupest-66ceacf580d7d2d7f5893dd4bae993b7f135ae14.tar.bz2
crupest-66ceacf580d7d2d7f5893dd4bae993b7f135ae14.zip
Update nvim configs.
Diffstat (limited to 'configs/nvim/lua/crupest/system/find.lua')
-rw-r--r--configs/nvim/lua/crupest/system/find.lua55
1 files changed, 55 insertions, 0 deletions
diff --git a/configs/nvim/lua/crupest/system/find.lua b/configs/nvim/lua/crupest/system/find.lua
new file mode 100644
index 0000000..857c6e7
--- /dev/null
+++ b/configs/nvim/lua/crupest/system/find.lua
@@ -0,0 +1,55 @@
+local system = require("crupest.system")
+local fs = require("crupest.system.fs");
+
+local function get_exe(path)
+ if system.is_win then
+ local exts = { "exe", "CMD", "cmd", "ps1" }
+ for _, ext in ipairs(exts) do
+ if string.find(path, "%."..ext.."$") and fs.isfile(path) then
+ return path
+ end
+ end
+ for _, ext in ipairs(exts) do
+ local p = path.."."..ext
+ if fs.isfile(p) then return p end
+ end
+ return nil
+ end
+
+ if vim.fn.executable(path) ~= 0 then
+ return path
+ end
+
+ return nil
+end
+
+local function find_node_modules(path)
+ return fs.walk_up(path, function (current_path)
+ local node_modules_path = current_path.."/node_modules"
+ if fs.isdir(node_modules_path) 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 exe_path end
+ return nil
+end
+
+local function escape_space(str)
+ return (string.gsub(str, " ", "\\ " ))
+end
+
+return {
+ get_exe = get_exe,
+ find_node_modules = find_node_modules,
+ find_npm_exe = find_npm_exe,
+ escape_space = escape_space
+}
+