Skip to content

Commit ccbd1b1

Browse files
authored
feat: parse .nvmrc .node-version files. (#7)
* feat: support parse legacy file * feat: support parse legacy file
1 parent 1aba0e8 commit ccbd1b1

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

hooks/parse_legacy_file.lua

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
--- Parse the legacy file found by vfox to determine the version of the tool.
2+
--- Useful to extract version numbers from files like JavaScript's package.json or Golangs go.mod.
3+
function PLUGIN:ParseLegacyFile(ctx)
4+
local filepath = ctx.filepath
5+
local file = io.open(filepath, "r")
6+
local content = file:read("*a")
7+
file:close()
8+
content = content:gsub("%s+", "")
9+
if content == "" then
10+
return {}
11+
end
12+
function resolve_legacy_version(strategy, query)
13+
local list = {}
14+
15+
if strategy == "latest_installed" then
16+
list = ctx:getInstalledVersions()
17+
elseif strategy == "latest_available" then
18+
for _, av in pairs(self:Available({})) do
19+
table.insert(list, av.version)
20+
end
21+
else
22+
-- Just return the original query
23+
return query
24+
end
25+
26+
local resolved = ""
27+
for _, item in pairs(list) do
28+
if item:match("^" .. query) then
29+
resolved = item
30+
break
31+
end
32+
end
33+
34+
if resolved ~= "" then
35+
return resolved
36+
elseif strategy ~= "latest_available" then
37+
-- If no version is installed, fallback to latest_available
38+
return resolve_legacy_version("latest_available", query)
39+
else
40+
-- Give up and pretty the query itself
41+
return query
42+
end
43+
end
44+
local query = resolve_version(content)
45+
46+
query = resolve_legacy_version("latest_installed", query)
47+
48+
return {
49+
version = query
50+
}
51+
end
52+
53+
function resolve_version(query)
54+
query = string.lower(query:gsub("v", ""))
55+
56+
if query:match("^lts-") then
57+
query = query:gsub("-", "/")
58+
end
59+
60+
local nodejs_codenames = {
61+
argon = 4,
62+
boron = 6,
63+
carbon = 8,
64+
dubnium = 10,
65+
erbium = 12,
66+
fermium = 14,
67+
gallium = 16,
68+
hydrogen = 18,
69+
iron = 20
70+
}
71+
72+
for codename, version_number in pairs(nodejs_codenames) do
73+
if query == "lts/" .. codename then
74+
query = tostring(version_number)
75+
break
76+
end
77+
end
78+
79+
if query == "lts" or query == "lts/*" then
80+
query = tostring(nodejs_codenames[#nodejs_codenames])
81+
end
82+
83+
return query
84+
end
85+

metadata.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ PLUGIN.license = "Apache 2.0"
1414
PLUGIN.description = "Node.js runtime environment."
1515

1616
--- !!! OPTIONAL !!!
17-
-- minimum compatible vfox version
17+
--- minimum compatible vfox version
1818
PLUGIN.minRuntimeVersion = "0.3.0"
19-
-- Some things that need user to be attention!
19+
--- Some things that need user to be attention!
2020
PLUGIN.notes = {}
21+
22+
--- List legacy configuration filenames for determining the specified version of the tool.
23+
--- such as ".node-version", ".nvmrc", etc.
24+
PLUGIN.legacyFilenames = {
25+
".node-version",
26+
".nvmrc"
27+
}

0 commit comments

Comments
 (0)