-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy_cache_dir.lua
58 lines (54 loc) · 1.68 KB
/
copy_cache_dir.lua
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
---This was a request I received:
---The idea is to copy node_modules/ when you create a new worktree so you
---don't have to npm install from a blank slate
---You must pass something to opts.matches or this action will do nothing
---This action is meant to be passed to add, otherwise it doesn't make sense.
---@class arbor.action.copy_cache_dir.opts
---@field matches? string[]
---@field background? boolean defaults to true
---@param info? arbor.git.info
---@param opts? arbor.action.copy_cache_dir.opts
---@return arbor.git.info | nil
return function(info, opts)
if not opts or not opts.matches then
require("arbor._lib.notify").warn("copy cache dir failed: no matches defined")
return
end
if not info or not info.new_path then
require("arbor._lib.notify").warn("copy cache dir failed: no new path set")
return
end
if
not info.branch_info
or not info.branch_info.worktree_path
or string.len(info.branch_info.worktree_path) == 0
then
-- Don't notify on this one, creating from a remote branch is an expected
-- behavior where this won't be defined
return
end
if opts.background == nil then
opts.background = true
end
local src_base = info.branch_info.worktree_path
local dst_base = info.new_path
for _, match in ipairs(opts.matches) do
local job = require("arbor").git.job({
command = "cp",
args = { "-R", src_base .. "/" .. match, dst_base .. "/" .. match },
enabled_recording = true,
on_exit = function(job, code)
if code ~= 0 then
require("arbor._lib.notify").warn(
"Failed to copy " .. match .. ":\n" .. table.concat(job:stderr_result(), "\n")
)
end
end,
})
if opts.background then
job:start()
else
job:sync()
end
end
end