move clean() to the fs file

This commit is contained in:
2025-04-26 00:05:16 -05:00
parent 2ccbb7ea74
commit a3a3652294
2 changed files with 43 additions and 42 deletions

View File

@ -19,4 +19,45 @@ function fs:link(package, cb)
end)
end
--- clean out old packages
---@param package package any package
function fs:clean(package)
h.uv.fs_scandir(
package.get_base_dir(),
vim.schedule_wrap(function(err, handle)
if err then
logger:log("error", "failed to clean; reason: %s", err)
else
local queue = {}
while handle do
local name = h.uv.fs_scandir_next(handle)
if name then
queue[name] = package.get_base_dir().."/"..name
else
break
end
end
-- keep packages that still exist
for _, pkg in pairs(package.get_packages()) do
queue[pkg.name] = nil
end
for name, dir in pairs(queue) do
local co = coroutine.create(function()
local ok = vim.fn.delete(dir, "rf")
if ok then
logger:log("clean", "deleted %s", name)
else
logger:log("error", "failed to delete %s", name)
end
end)
coroutine.resume(co)
end
end
end)
)
end
return fs