aboutsummaryrefslogtreecommitdiff
path: root/configs/nvim/lua/crupest/system/fs.lua
blob: 25ce02c85ec3b2e1db2bd68e38e331f5687886d6 (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
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 = {}
    require("crupest/system").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(vim.fn.fnamemodify(new, ":p:h"))
    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(vim.fn.fnamemodify(new, ":p:h"))
    assert(vim.uv.fs_rename(old, new))
end

return {
    exist = exist,
    isfile = isfile,
    isdir = isdir,
    mkdir = mkdir,
    copy = copy,
    remove = remove,
    move = move
}