Skip to content

Commit

Permalink
update nodejs (#1)
Browse files Browse the repository at this point in the history
* update nodejs

* update nodejs
  • Loading branch information
ahai-code authored Mar 26, 2024
1 parent b673cf0 commit bcbf342
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 96 deletions.
35 changes: 25 additions & 10 deletions hooks/available.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
local util = require("util")
local json = require("json")
local nodejsUtils = require("nodejs_utils")

--- Return all available versions provided by this plugin
--- @param ctx table Empty table used as context, for future extension
--- @return table Descriptions of available versions and accompanying tool descriptions
available_result = nil
function PLUGIN:Available(ctx)
util:DoSomeThing()
local runtimeVersion = ctx.runtimeVersion
return {
{
version = "xxxx",
note = "LTS",
if available_result then
return available_result
end
local resp, err = http.get({
url = nodejsUtils.VersionSourceUrl
})
if err ~= nil or resp.status_code ~= 200 then
return {}
end
local body = json.decode(resp.body)
local result = {}

for _, v in ipairs(body) do
table.insert(result, {
version = string.gsub(v.version, "^v", ""),
note = v.lts and "LTS" or "",
addition = {
{
name = "npm",
version = "8.8.8",
version = v.npm,
}
}
}
}
})
end
table.sort(result, nodejsUtils.compare_versions)
available_result = result
return result
end
41 changes: 16 additions & 25 deletions hooks/env_keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,21 @@
--- @field ctx.path string SDK installation directory
function PLUGIN:EnvKeys(ctx)
--- this variable is same as ctx.sdkInfo['plugin-name'].path
local mainPath = ctx.path
local runtimeVersion = ctx.runtimeVersion
local mainSdkInfo = ctx.main
local mpath = mainSdkInfo.path
local mversion = mainSdkInfo.version
local mname = mainSdkInfo.name
local sdkInfo = ctx.sdkInfo['sdk-name']
local path = sdkInfo.path
local version = sdkInfo.version
local name = sdkInfo.name
return {
{
key = "JAVA_HOME",
value = mainPath
},
{
key = "PATH",
value = mainPath .. "/bin"
},
{
key = "PATH",
value = mainPath .. "/bin2"
},

}
local version_path = ctx.path
if RUNTIME.osType == "windows" then
return {
{
key = "PATH",
value = version_path
},
}
else
return {
{
key = "PATH",
value = version_path .. "/bin"
},
}
end

end
2 changes: 1 addition & 1 deletion hooks/post_install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function PLUGIN:PostInstall(ctx)
--- ctx.rootPath SDK installation directory
local rootPath = ctx.rootPath
local runtimeVersion = ctx.runtimeVersion
local sdkInfo = ctx.sdkInfo['sdk-name']
local sdkInfo = ctx.sdkInfo['nodejs']
local path = sdkInfo.path
local version = sdkInfo.version
local name = sdkInfo.name
Expand Down
12 changes: 0 additions & 12 deletions hooks/post_plugin_add.lua

This file was deleted.

4 changes: 0 additions & 4 deletions hooks/prase_legacy_file.lua

This file was deleted.

71 changes: 41 additions & 30 deletions hooks/pre_install.lua
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
local http = require("http")
local nodejsUtils = require("nodejs_utils")
--- Returns some pre-installed information, such as version number, download address, local files, etc.
--- If checksum is provided, vfox will automatically check it for you.
--- @param ctx table
--- @field ctx.version string User-input version
--- @return table Version information
function PLUGIN:PreInstall(ctx)
local version = ctx.version
local runtimeVersion = ctx.runtimeVersion

if version == "latest" then
local lists = self:Available({})
version = lists[1].version
end

if not nodejsUtils.is_semver_simple(version) then
local lists = self:Available({})
local shorthands = nodejsUtils.calculate_shorthand(lists)
version = shorthands[version]
end

if (version == nil) then
error("version not found for provided version " .. version)
end

local arch_type = RUNTIME.archType
local ext = ".tar.gz"
local osType = RUNTIME.osType
if RUNTIME.archType == "amd64" then
arch_type = "x64"
end
if RUNTIME.osType == "windows" then
ext = ".zip"
osType = "win"
end
local filename = nodejsUtils.FileName:format(version, osType, arch_type, ext)
local baseUrl = nodejsUtils.NodeBaseUrl:format(version)

local resp, err = http.get({
url = baseUrl .. "SHASUMS256.txt"
})
if err ~= nil or resp.status_code ~= 200 then
error("get checksum failed")
end
local checksum = nodejsUtils.get_checksum(resp.body, filename)
return {
--- Version number
version = "xxx",
--- remote URL or local file path [optional]
url = "xxx",
--- SHA256 checksum [optional]
sha256 = "xxx",
--- md5 checksum [optional]
md5 = "xxx",
--- sha1 checksum [optional]
sha1 = "xxx",
--- sha512 checksum [optional]
sha512 = "xx",
--- additional need files [optional]
addition = {
{
--- additional file name !
name = "xxx",
--- remote URL or local file path [optional]
url = "xxx",
--- SHA256 checksum [optional]
sha256 = "xxx",
--- md5 checksum [optional]
md5 = "xxx",
--- sha1 checksum [optional]
sha1 = "xxx",
--- sha512 checksum [optional]
sha512 = "xx",
}
}
version = version,
url = baseUrl .. filename,
sha256 = checksum,
}
end
90 changes: 90 additions & 0 deletions lib/nodejs_utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
local NODEJS_UTILS={}

NODEJS_UTILS.NodeBaseUrl = "https://nodejs.org/dist/v%s/"
NODEJS_UTILS.FileName = "node-v%s-%s-%s%s"
NODEJS_UTILS.npmDownloadUrl = "https://github.com/npm/cli/archive/v%s.%s"
NODEJS_UTILS.VersionSourceUrl = "https://nodejs.org/dist/index.json"

function NODEJS_UTILS.compare_versions(v1o, v2o)
local v1 = v1o.version
local v2 = v2o.version
local v1_parts = {}
for part in string.gmatch(v1, "[^.]+") do
table.insert(v1_parts, tonumber(part))
end

local v2_parts = {}
for part in string.gmatch(v2, "[^.]+") do
table.insert(v2_parts, tonumber(part))
end

for i = 1, math.max(#v1_parts, #v2_parts) do
local v1_part = v1_parts[i] or 0
local v2_part = v2_parts[i] or 0
if v1_part > v2_part then
return true
elseif v1_part < v2_part then
return false
end
end

return false
end


function NODEJS_UTILS.get_checksum(file_content, file_name)
for line in string.gmatch(file_content, '([^\n]*)\n?') do
local checksum, name = string.match(line, '(%w+)%s+(%S+)')
if name == file_name then
return checksum
end
end
return nil
end

function NODEJS_UTILS.is_semver_simple(str)
-- match pattern: three digits, separated by dot
local pattern = "^%d+%.%d+%.%d+$"
return str:match(pattern) ~= nil
end


function NODEJS_UTILS.extract_semver(semver)
local pattern = "^(%d+)%.(%d+)%.[%d.]+$"
local major, minor = semver:match(pattern)
return major, minor
end


function NODEJS_UTILS.calculate_shorthand(list)
local versions_shorthand = {}
for _, v in ipairs(list) do
local version = v.version
local major, minor = extract_semver(version)

if major then
if not versions_shorthand[major] then
versions_shorthand[major] = version
else
if compare_versions({version = version}, {version = versions_shorthand[major]}) then
versions_shorthand[major] = version
end
end

if minor then
local major_minor = major .. "." .. minor
if not versions_shorthand[major_minor] then
versions_shorthand[major_minor] = version
else
if compare_versions({version = version}, {version = versions_shorthand[major_minor]}) then
versions_shorthand[major_minor] = version
end
end
end
end
end

return versions_shorthand
end

return NODEJS_UTILS
19 changes: 5 additions & 14 deletions metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,19 @@ PLUGIN = {}

--- !!! MUST BE SET !!!
--- Plugin name
PLUGIN.name = "java"
PLUGIN.name = "nodejs"
--- Plugin author
PLUGIN.author = "your name"
PLUGIN.author = "Aooohan"
--- Plugin version
PLUGIN.version = "0.0.1"
PLUGIN.version = "0.0.4"
--- Plugin repository
PLUGIN.homepage = "https://github.com/version-fox/vfox-plugin-template"
PLUGIN.homepage = "https://github.com/version-fox/vfox-nodejs"
--- Plugin license
PLUGIN.license = "MIT"
--- Plugin description
PLUGIN.description = "xxx"
PLUGIN.description = "Node.js"

--- !!! OPTIONAL !!!
-- minimum compatible vfox version
PLUGIN.minRuntimeVersion = "0.2.2"
-- legacy filenames
PLUGIN.legacyFilenames = {
".nvmrc",
".node-version",
}

PLUGIN.notes ={
"some thing",
"some thing",
}

0 comments on commit bcbf342

Please sign in to comment.