aboutsummaryrefslogtreecommitdiff
path: root/configs/nvim/config-root/lua/crupest/utils/fs.lua
blob: d34c2f6519201e3feacb789d16c99ca0f830b58f (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
local function clean_path(path)
    if path == "/" then return path end
    path = string.gsub(path, "[/\\]+", "/")
    if string.sub(path, string.len(path)) == '/' then
        path = string.sub(path, 1, string.len(path) - 1)
    end
    return path
end

local function full_path(name)
    local path = vim.fn.fnamemodify(name, ":p")
    return clean_path(path)
end

local function escape_space(str)
    return (string.gsub(str, " ", "\\ "))
end

local function path_get_dir(path)
    return full_path(vim.fn.fnamemodify(clean_path(path), ":h"))
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"
end

local function isdir(path)
    local s = vim.uv.fs_stat(path)
    if not s then return false end
    return s.type == "directory"
end

local function mkdir(dir)
    local parents = {}

    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)
            return
        end
        if not exist(v) then
            vim.notify("Creating dir " .. v)
            assert(vim.uv.fs_mkdir(v, 504)) -- mode = 0770
        end
    end
end

local function copy(old, new)
    mkdir(path_get_dir(new))
    assert(vim.uv.fs_copyfile(old, new))
end

local function remove(path)
    assert(vim.uv.fs_unlink(path))
end

local function move(old, new)
    mkdir(path_get_dir(new))
    assert(vim.uv.fs_rename(old, new))
end

return {
    clean_path = clean_path,
    full_path = full_path,
    escape_space = escape_space,
    path_get_dir = path_get_dir,
    walk_up = walk_up,
    exist = exist,
    isfile = isfile,
    isdir = isdir,
    mkdir = mkdir,
    copy = copy,
    remove = remove,
    move = move
}