generated from version-fox/vfox-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update nodejs * update nodejs
- Loading branch information
Showing
8 changed files
with
178 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters