fix(cache): added support for top level lua linked directories. Fixes #233

This commit is contained in:
Folke Lemaitre 2022-12-29 18:40:28 +01:00
parent 8544c389ab
commit 853d4d5838
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 10 additions and 6 deletions

View File

@ -114,7 +114,7 @@ function M.check_loaded(modname)
end end
---@param modname string ---@param modname string
---@return any ---@return fun()|string
function M.loader(modname) function M.loader(modname)
modname = modname:gsub("/", ".") modname = modname:gsub("/", ".")
local entry = M.cache[modname] local entry = M.cache[modname]
@ -139,7 +139,7 @@ function M.loader(modname)
end end
end end
end end
return chunk or (err and error(err)) or "not found in lazy cache" return chunk or err or ("module " .. modname .. " not found")
end end
---@param modpath string ---@param modpath string
@ -197,8 +197,12 @@ function M.load(modkey, modpath)
end end
function M.require(modname) function M.require(modname)
local chunk = M.loader(modname)
if type(chunk) == "string" then
error(chunk)
end
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
local mod = M.loader(modname)() local mod = chunk()
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = mod package.loaded[modname] = mod
return mod return mod
@ -216,10 +220,10 @@ function M._index(path)
M.indexed[path] = true M.indexed[path] = true
Util.ls(path .. "/lua", function(_, name, t) Util.ls(path .. "/lua", function(_, name, t)
local topname local topname
if t == "directory" then if name:sub(-4) == ".lua" then
topname = name
elseif name:sub(-4) == ".lua" then
topname = name:sub(1, -5) topname = name:sub(1, -5)
elseif t == "link" or t == "directory" then
topname = name
end end
if topname then if topname then
M.topmods[topname] = M.topmods[topname] or {} M.topmods[topname] = M.topmods[topname] or {}