-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-resolve.lua
64 lines (57 loc) · 1.54 KB
/
node-resolve.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
local fs = require("fs")
local separator = package.config:match("^([^\n]*)")
local moduleDir = "node_modules"
local packageFile = "package.json"
local function getMain (modulePath)
local packagePath = modulePath..separator..packageFile
if not fs.isFile(packagePath) then
return
end
local content = fs.read(packagePath)
if content then
local main = content:match("\"main\": \"([^\"]+)\"")
return modulePath..separator..main
end
end
local function splitPath (path)
return path:gmatch("[^/\\]+")
end
local function joinPaths (dirs)
return table.concat(dirs, separator)
end
local function checkModule (path, request)
if fs.isDirectory(path..separator..moduleDir..separator..request) and
fs.isFile(path..separator..moduleDir..separator..request..separator..packageFile)
then
return true
else
return false
end
end
local function loadModule (path, request)
local main = getMain(path..separator..moduleDir..separator..request)
print(main)
return fs.load(main)
end
local function nodeResolve (request)
local cwd = fs.currentDir()
local paths = {}
for path in splitPath(cwd) do
table.insert(paths, path)
end
if #paths == 0 then
table.insert(paths, "")
end
while #paths > 0 do
local path = joinPaths(paths)
if checkModule(path, request) then
return loadModule(path, request)
end
table.remove(paths, #paths)
end
end
if package.loaders ~= nil then
table.insert(package.loaders, 2, nodeResolve)
elseif package.searchers ~= nil then
table.insert(package.searchers, 2, nodeResolve)
end