aboutsummaryrefslogtreecommitdiff
path: root/configs/nvim/lua/crupest/system/find.lua
blob: 857c6e7068a8b794274453168a57e8b1ed2e2ccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
}