diff options
Diffstat (limited to 'configs/nvim/lua/crupest/system')
-rw-r--r-- | configs/nvim/lua/crupest/system/find.lua | 55 | ||||
-rw-r--r-- | configs/nvim/lua/crupest/system/fs.lua | 50 |
2 files changed, 96 insertions, 9 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 +} + diff --git a/configs/nvim/lua/crupest/system/fs.lua b/configs/nvim/lua/crupest/system/fs.lua index 25ce02c..b52a822 100644 --- a/configs/nvim/lua/crupest/system/fs.lua +++ b/configs/nvim/lua/crupest/system/fs.lua @@ -1,11 +1,39 @@ +local function clean_path(path) + return path and (string.gsub(path, "[/\\]+", "/")) +end + +local function full_path(name) + return vim.fn.fnamemodify(name, ":p:gs?\\?/?") +end + +local function path_get_dir(path) + return vim.fn.fnamemodify(path, ":p:h:gs?\\?/?") +end + +local function walk_up(path, func) + local current_path = full_path(path) + while true do + local result = func(current_path) + if result ~= nil then + return result + end + local new_path = path_get_dir(current_path) + if new_path == current_path then + break + end + current_path = new_path + end + return nil +end + local function exist(path) return vim.uv.fs_stat(path) end local function isfile(path) - local s = vim.uv.fs_stat(path) - if not s then return false end - return s.type == "file" + local s = vim.uv.fs_stat(path) + if not s then return false end + return s.type == "file" end local function isdir(path) @@ -16,24 +44,25 @@ end local function mkdir(dir) local parents = {} - require("crupest/system").walk_up(dir, function(p) + + walk_up(dir, function(p) table.insert(parents, 1, p) end) for _, v in ipairs(parents) do if exist(v) and not isdir(v) then - vim.notify(v.." is not a dir. Can't make dir "..dir, vim.log.levels.ERROR) + vim.notify(v .. " is not a dir. Can't make dir " .. dir, vim.log.levels.ERROR) return end if not exist(v) then - vim.notify("Creating dir "..v) + vim.notify("Creating dir " .. v) assert(vim.uv.fs_mkdir(v, 504)) -- mode = 0770 end end end local function copy(old, new) - mkdir(vim.fn.fnamemodify(new, ":p:h")) + mkdir(path_get_dir(new)) assert(vim.uv.fs_copyfile(old, new)) end @@ -42,11 +71,15 @@ local function remove(path) end local function move(old, new) - mkdir(vim.fn.fnamemodify(new, ":p:h")) + mkdir(path_get_dir(new)) assert(vim.uv.fs_rename(old, new)) end return { + clean_path = clean_path, + full_path = full_path, + path_get_dir = path_get_dir, + walk_up = walk_up, exist = exist, isfile = isfile, isdir = isdir, @@ -55,4 +88,3 @@ return { remove = remove, move = move } - |