Skip to content

Commit bcbf342

Browse files
authored
update nodejs (#1)
* update nodejs * update nodejs
1 parent b673cf0 commit bcbf342

8 files changed

Lines changed: 178 additions & 96 deletions

File tree

hooks/available.lua

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
local util = require("util")
1+
local json = require("json")
2+
local nodejsUtils = require("nodejs_utils")
23

34
--- Return all available versions provided by this plugin
45
--- @param ctx table Empty table used as context, for future extension
56
--- @return table Descriptions of available versions and accompanying tool descriptions
7+
available_result = nil
68
function PLUGIN:Available(ctx)
7-
util:DoSomeThing()
8-
local runtimeVersion = ctx.runtimeVersion
9-
return {
10-
{
11-
version = "xxxx",
12-
note = "LTS",
9+
if available_result then
10+
return available_result
11+
end
12+
local resp, err = http.get({
13+
url = nodejsUtils.VersionSourceUrl
14+
})
15+
if err ~= nil or resp.status_code ~= 200 then
16+
return {}
17+
end
18+
local body = json.decode(resp.body)
19+
local result = {}
20+
21+
for _, v in ipairs(body) do
22+
table.insert(result, {
23+
version = string.gsub(v.version, "^v", ""),
24+
note = v.lts and "LTS" or "",
1325
addition = {
1426
{
1527
name = "npm",
16-
version = "8.8.8",
28+
version = v.npm,
1729
}
1830
}
19-
}
20-
}
31+
})
32+
end
33+
table.sort(result, nodejsUtils.compare_versions)
34+
available_result = result
35+
return result
2136
end

hooks/env_keys.lua

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,21 @@
55
--- @field ctx.path string SDK installation directory
66
function PLUGIN:EnvKeys(ctx)
77
--- this variable is same as ctx.sdkInfo['plugin-name'].path
8-
local mainPath = ctx.path
9-
local runtimeVersion = ctx.runtimeVersion
10-
local mainSdkInfo = ctx.main
11-
local mpath = mainSdkInfo.path
12-
local mversion = mainSdkInfo.version
13-
local mname = mainSdkInfo.name
14-
local sdkInfo = ctx.sdkInfo['sdk-name']
15-
local path = sdkInfo.path
16-
local version = sdkInfo.version
17-
local name = sdkInfo.name
18-
return {
19-
{
20-
key = "JAVA_HOME",
21-
value = mainPath
22-
},
23-
{
24-
key = "PATH",
25-
value = mainPath .. "/bin"
26-
},
27-
{
28-
key = "PATH",
29-
value = mainPath .. "/bin2"
30-
},
31-
32-
}
8+
local version_path = ctx.path
9+
if RUNTIME.osType == "windows" then
10+
return {
11+
{
12+
key = "PATH",
13+
value = version_path
14+
},
15+
}
16+
else
17+
return {
18+
{
19+
key = "PATH",
20+
value = version_path .. "/bin"
21+
},
22+
}
23+
end
3324

3425
end

hooks/post_install.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function PLUGIN:PostInstall(ctx)
55
--- ctx.rootPath SDK installation directory
66
local rootPath = ctx.rootPath
77
local runtimeVersion = ctx.runtimeVersion
8-
local sdkInfo = ctx.sdkInfo['sdk-name']
8+
local sdkInfo = ctx.sdkInfo['nodejs']
99
local path = sdkInfo.path
1010
local version = sdkInfo.version
1111
local name = sdkInfo.name

hooks/post_plugin_add.lua

Lines changed: 0 additions & 12 deletions
This file was deleted.

hooks/prase_legacy_file.lua

Lines changed: 0 additions & 4 deletions
This file was deleted.

hooks/pre_install.lua

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
1+
local http = require("http")
2+
local nodejsUtils = require("nodejs_utils")
13
--- Returns some pre-installed information, such as version number, download address, local files, etc.
24
--- If checksum is provided, vfox will automatically check it for you.
35
--- @param ctx table
46
--- @field ctx.version string User-input version
57
--- @return table Version information
68
function PLUGIN:PreInstall(ctx)
79
local version = ctx.version
8-
local runtimeVersion = ctx.runtimeVersion
10+
11+
if version == "latest" then
12+
local lists = self:Available({})
13+
version = lists[1].version
14+
end
15+
16+
if not nodejsUtils.is_semver_simple(version) then
17+
local lists = self:Available({})
18+
local shorthands = nodejsUtils.calculate_shorthand(lists)
19+
version = shorthands[version]
20+
end
21+
22+
if (version == nil) then
23+
error("version not found for provided version " .. version)
24+
end
25+
26+
local arch_type = RUNTIME.archType
27+
local ext = ".tar.gz"
28+
local osType = RUNTIME.osType
29+
if RUNTIME.archType == "amd64" then
30+
arch_type = "x64"
31+
end
32+
if RUNTIME.osType == "windows" then
33+
ext = ".zip"
34+
osType = "win"
35+
end
36+
local filename = nodejsUtils.FileName:format(version, osType, arch_type, ext)
37+
local baseUrl = nodejsUtils.NodeBaseUrl:format(version)
38+
39+
local resp, err = http.get({
40+
url = baseUrl .. "SHASUMS256.txt"
41+
})
42+
if err ~= nil or resp.status_code ~= 200 then
43+
error("get checksum failed")
44+
end
45+
local checksum = nodejsUtils.get_checksum(resp.body, filename)
946
return {
10-
--- Version number
11-
version = "xxx",
12-
--- remote URL or local file path [optional]
13-
url = "xxx",
14-
--- SHA256 checksum [optional]
15-
sha256 = "xxx",
16-
--- md5 checksum [optional]
17-
md5 = "xxx",
18-
--- sha1 checksum [optional]
19-
sha1 = "xxx",
20-
--- sha512 checksum [optional]
21-
sha512 = "xx",
22-
--- additional need files [optional]
23-
addition = {
24-
{
25-
--- additional file name !
26-
name = "xxx",
27-
--- remote URL or local file path [optional]
28-
url = "xxx",
29-
--- SHA256 checksum [optional]
30-
sha256 = "xxx",
31-
--- md5 checksum [optional]
32-
md5 = "xxx",
33-
--- sha1 checksum [optional]
34-
sha1 = "xxx",
35-
--- sha512 checksum [optional]
36-
sha512 = "xx",
37-
}
38-
}
47+
version = version,
48+
url = baseUrl .. filename,
49+
sha256 = checksum,
3950
}
4051
end

lib/nodejs_utils.lua

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
local NODEJS_UTILS={}
2+
3+
NODEJS_UTILS.NodeBaseUrl = "https://nodejs.org/dist/v%s/"
4+
NODEJS_UTILS.FileName = "node-v%s-%s-%s%s"
5+
NODEJS_UTILS.npmDownloadUrl = "https://github.com/npm/cli/archive/v%s.%s"
6+
NODEJS_UTILS.VersionSourceUrl = "https://nodejs.org/dist/index.json"
7+
8+
function NODEJS_UTILS.compare_versions(v1o, v2o)
9+
local v1 = v1o.version
10+
local v2 = v2o.version
11+
local v1_parts = {}
12+
for part in string.gmatch(v1, "[^.]+") do
13+
table.insert(v1_parts, tonumber(part))
14+
end
15+
16+
local v2_parts = {}
17+
for part in string.gmatch(v2, "[^.]+") do
18+
table.insert(v2_parts, tonumber(part))
19+
end
20+
21+
for i = 1, math.max(#v1_parts, #v2_parts) do
22+
local v1_part = v1_parts[i] or 0
23+
local v2_part = v2_parts[i] or 0
24+
if v1_part > v2_part then
25+
return true
26+
elseif v1_part < v2_part then
27+
return false
28+
end
29+
end
30+
31+
return false
32+
end
33+
34+
35+
function NODEJS_UTILS.get_checksum(file_content, file_name)
36+
for line in string.gmatch(file_content, '([^\n]*)\n?') do
37+
local checksum, name = string.match(line, '(%w+)%s+(%S+)')
38+
if name == file_name then
39+
return checksum
40+
end
41+
end
42+
return nil
43+
end
44+
45+
function NODEJS_UTILS.is_semver_simple(str)
46+
-- match pattern: three digits, separated by dot
47+
local pattern = "^%d+%.%d+%.%d+$"
48+
return str:match(pattern) ~= nil
49+
end
50+
51+
52+
function NODEJS_UTILS.extract_semver(semver)
53+
local pattern = "^(%d+)%.(%d+)%.[%d.]+$"
54+
local major, minor = semver:match(pattern)
55+
return major, minor
56+
end
57+
58+
59+
function NODEJS_UTILS.calculate_shorthand(list)
60+
local versions_shorthand = {}
61+
for _, v in ipairs(list) do
62+
local version = v.version
63+
local major, minor = extract_semver(version)
64+
65+
if major then
66+
if not versions_shorthand[major] then
67+
versions_shorthand[major] = version
68+
else
69+
if compare_versions({version = version}, {version = versions_shorthand[major]}) then
70+
versions_shorthand[major] = version
71+
end
72+
end
73+
74+
if minor then
75+
local major_minor = major .. "." .. minor
76+
if not versions_shorthand[major_minor] then
77+
versions_shorthand[major_minor] = version
78+
else
79+
if compare_versions({version = version}, {version = versions_shorthand[major_minor]}) then
80+
versions_shorthand[major_minor] = version
81+
end
82+
end
83+
end
84+
end
85+
end
86+
87+
return versions_shorthand
88+
end
89+
90+
return NODEJS_UTILS

metadata.lua

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,19 @@ PLUGIN = {}
33

44
--- !!! MUST BE SET !!!
55
--- Plugin name
6-
PLUGIN.name = "java"
6+
PLUGIN.name = "nodejs"
77
--- Plugin author
8-
PLUGIN.author = "your name"
8+
PLUGIN.author = "Aooohan"
99
--- Plugin version
10-
PLUGIN.version = "0.0.1"
10+
PLUGIN.version = "0.0.4"
1111
--- Plugin repository
12-
PLUGIN.homepage = "https://github.com/version-fox/vfox-plugin-template"
12+
PLUGIN.homepage = "https://github.com/version-fox/vfox-nodejs"
1313
--- Plugin license
1414
PLUGIN.license = "MIT"
1515
--- Plugin description
16-
PLUGIN.description = "xxx"
16+
PLUGIN.description = "Node.js"
1717

1818
--- !!! OPTIONAL !!!
1919
-- minimum compatible vfox version
2020
PLUGIN.minRuntimeVersion = "0.2.2"
21-
-- legacy filenames
22-
PLUGIN.legacyFilenames = {
23-
".nvmrc",
24-
".node-version",
25-
}
2621

27-
PLUGIN.notes ={
28-
"some thing",
29-
"some thing",
30-
}

0 commit comments

Comments
 (0)