From 5d9d35404f39de5d7c9365cbc2aa39858929cbfc Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 23 Jan 2023 19:18:48 +0100 Subject: [PATCH] feat(util): utility method to walk over all modules in a directory --- lua/lazy/core/util.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index 7411664..fd2cce4 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -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)