diff --git a/hooks/parse_legacy_file.lua b/hooks/parse_legacy_file.lua new file mode 100644 index 0000000..a6bdda5 --- /dev/null +++ b/hooks/parse_legacy_file.lua @@ -0,0 +1,85 @@ +--- Parse the legacy file found by vfox to determine the version of the tool. +--- Useful to extract version numbers from files like JavaScript's package.json or Golangs go.mod. +function PLUGIN:ParseLegacyFile(ctx) + local filepath = ctx.filepath + local file = io.open(filepath, "r") + local content = file:read("*a") + file:close() + content = content:gsub("%s+", "") + if content == "" then + return {} + end + function resolve_legacy_version(strategy, query) + local list = {} + + if strategy == "latest_installed" then + list = ctx:getInstalledVersions() + elseif strategy == "latest_available" then + for _, av in pairs(self:Available({})) do + table.insert(list, av.version) + end + else + -- Just return the original query + return query + end + + local resolved = "" + for _, item in pairs(list) do + if item:match("^" .. query) then + resolved = item + break + end + end + + if resolved ~= "" then + return resolved + elseif strategy ~= "latest_available" then + -- If no version is installed, fallback to latest_available + return resolve_legacy_version("latest_available", query) + else + -- Give up and pretty the query itself + return query + end + end + local query = resolve_version(content) + + query = resolve_legacy_version("latest_installed", query) + + return { + version = query + } +end + +function resolve_version(query) + query = string.lower(query:gsub("v", "")) + + if query:match("^lts-") then + query = query:gsub("-", "/") + end + + local nodejs_codenames = { + argon = 4, + boron = 6, + carbon = 8, + dubnium = 10, + erbium = 12, + fermium = 14, + gallium = 16, + hydrogen = 18, + iron = 20 + } + + for codename, version_number in pairs(nodejs_codenames) do + if query == "lts/" .. codename then + query = tostring(version_number) + break + end + end + + if query == "lts" or query == "lts/*" then + query = tostring(nodejs_codenames[#nodejs_codenames]) + end + + return query +end + diff --git a/metadata.lua b/metadata.lua index d0036ec..6fdeaaf 100644 --- a/metadata.lua +++ b/metadata.lua @@ -14,7 +14,14 @@ PLUGIN.license = "Apache 2.0" PLUGIN.description = "Node.js runtime environment." --- !!! OPTIONAL !!! --- minimum compatible vfox version +--- minimum compatible vfox version PLUGIN.minRuntimeVersion = "0.3.0" --- Some things that need user to be attention! +--- Some things that need user to be attention! PLUGIN.notes = {} + +--- List legacy configuration filenames for determining the specified version of the tool. +--- such as ".node-version", ".nvmrc", etc. +PLUGIN.legacyFilenames = { + ".node-version", + ".nvmrc" +}