-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.lua
83 lines (78 loc) · 1.88 KB
/
init.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
local luarequire
local mname = "node_modules"
local sep = string.match(package.config, "(.)")
local lastpath
local function fileExists(path)
local file, exists = io.open(path), false
if file then
exists = true
file:close()
end
return exists
end
local function loadModule (request, path)
local prv = ""
local dirs = {}
for dir in path:gmatch("[^" .. sep .. "]+") do
local nxt = dir
if "" ~= prv then
nxt = prv .. "." .. dir
end
local new
if mname == dir then
new = nxt
else
new = nxt .. "." .. mname
end
table.insert(dirs, 1, new .. "." .. request)
prv = nxt
end
local files = {"init", request}
for i, dir in ipairs(dirs) do
for _, file in ipairs(files) do
local path = dir:gsub("%.", sep)
if fileExists(path .. sep .. file .. ".lua") then
lastpath = path
return luarequire(dir .. "." .. file)
end
end
end
return luarequire(request)
end
local function loadFile (request, path)
while request:sub(1, 1) == "." do
if request:sub(1, 3) == "../" then
path = path:match("(.*)" .. sep)
request = request:sub(4)
else
request = request:sub(3)
end
end
local filepath = ""
if path then
filepath = path .. sep
end
filepath = filepath .. request:gsub("/", sep)
if fileExists(filepath .. ".lua") then
lastpath = path
return luarequire(filepath:gsub(sep, "."))
end
return luarequire(request)
end
local function newrequire (request)
local source = debug.getinfo(2, "S").source
local path = source:match("@%." .. sep .. "(.*)" .. sep) or mname
if source == "=[C]" then
path = lastpath
end
if request:sub(1, 1) == "." then
return loadFile(request, path)
else
return loadModule(request, path)
end
end
if not package.loaded["lua-loader"] then
package.loaded["lua-loader"] = true
luarequire = require
require = newrequire
end