feat(util): utility method to walk over all modules in a directory

This commit is contained in:
Folke Lemaitre 2023-01-23 19:18:48 +01:00
parent 26a67e3c48
commit 5d9d35404f
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 16 additions and 0 deletions

View File

@ -201,6 +201,22 @@ function M.walk(path, fn)
end)
end
---@param root string
---@param fn fun(modname:string, modpath:string)
---@param modname? string
function M.walkmods(root, fn, modname)
modname = modname and (modname:gsub("%.$", "") .. ".") or ""
M.ls(root, function(path, name, type)
if name == "init.lua" then
fn(modname:gsub("%.$", ""), path)
elseif (type == "file" or type == "link") and name:sub(-4) == ".lua" then
fn(modname .. name:sub(1, -5), path)
elseif type == "directory" then
M.walkmods(path, fn, modname .. name .. ".")
end
end)
end
---@param modname string
---@param fn fun(modname:string, modpath:string)
function M.lsmod(modname, fn)