Skip to content

Commit e2cdf45

Browse files
committed
support mirror & compatible m1 chip
fix #4 fix #3
1 parent d4eec29 commit e2cdf45

4 files changed

Lines changed: 40 additions & 23 deletions

File tree

hooks/available.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local json = require("json")
22
local http = require("http")
3-
local nodejsUtils = require("nodejs_utils")
3+
local util = require("util")
44

55
--- Return all available versions provided by this plugin
66
--- @param ctx table Empty table used as context, for future extension
@@ -11,7 +11,7 @@ function PLUGIN:Available(ctx)
1111
return available_result
1212
end
1313
local resp, err = http.get({
14-
url = nodejsUtils.VersionSourceUrl
14+
url = util.getBaseUrl() .. util.VersionSourceUrl
1515
})
1616
if err ~= nil or resp.status_code ~= 200 then
1717
return {}
@@ -31,7 +31,7 @@ function PLUGIN:Available(ctx)
3131
}
3232
})
3333
end
34-
table.sort(result, nodejsUtils.compare_versions)
34+
table.sort(result, util.compare_versions)
3535
available_result = result
3636
return result
3737
end

hooks/pre_install.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local http = require("http")
2-
local nodejsUtils = require("nodejs_utils")
2+
local util = require("util")
33
--- Returns some pre-installed information, such as version number, download address, local files, etc.
44
--- If checksum is provided, vfox will automatically check it for you.
55
--- @param ctx table
@@ -13,9 +13,9 @@ function PLUGIN:PreInstall(ctx)
1313
version = lists[1].version
1414
end
1515

16-
if not nodejsUtils.is_semver_simple(version) then
16+
if not util.is_semver_simple(version) then
1717
local lists = self:Available({})
18-
local shorthands = nodejsUtils.calculate_shorthand(lists)
18+
local shorthands = util.calculate_shorthand(lists)
1919
version = shorthands[version]
2020
end
2121

@@ -33,16 +33,24 @@ function PLUGIN:PreInstall(ctx)
3333
ext = ".zip"
3434
osType = "win"
3535
end
36-
local filename = nodejsUtils.FileName:format(version, osType, arch_type, ext)
37-
local baseUrl = nodejsUtils.NodeBaseUrl:format(version)
36+
-- add logic for macOS M1~
37+
if RUNTIME.osType == "darwin" then
38+
local major, _ = util.extract_semver(version)
39+
if major and tonumber(major) <= 16 then
40+
arch_type = "x64"
41+
end
42+
end
43+
44+
local filename = util.FileName:format(version, osType, arch_type, ext)
45+
local baseUrl = util.getBaseUrl() .. util.NodeBaseUrl:format(version)
3846

3947
local resp, err = http.get({
4048
url = baseUrl .. "SHASUMS256.txt"
4149
})
4250
if err ~= nil or resp.status_code ~= 200 then
4351
error("get checksum failed")
4452
end
45-
local checksum = nodejsUtils.get_checksum(resp.body, filename)
53+
local checksum = util.get_checksum(resp.body, filename)
4654
return {
4755
version = version,
4856
url = baseUrl .. filename,

hooks/pre_use.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
local nodejsUtils = require("nodejs_utils")
1+
local util = require("util")
22
--- When user invoke `use` command, this function will be called to get the
33
--- valid version information.
44
--- @param ctx table Context information
55
function PLUGIN:PreUse(ctx)
66
--- user input version
77
local version = ctx.version
88

9-
local shorthands = nodejsUtils.calculate_shorthand(ctx.installedSdks)
9+
local shorthands = util.calculate_shorthand(ctx.installedSdks)
1010

11-
if not nodejsUtils.is_semver_simple(version) then
11+
if not util.is_semver_simple(version) then
1212
version = shorthands[version]
1313
end
1414

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
local NODEJS_UTILS={}
1+
local UTIL ={}
22

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"
3+
UTIL.NodeBaseUrl = "/v%s/"
4+
UTIL.FileName = "node-v%s-%s-%s%s"
5+
UTIL.VersionSourceUrl = "/index.json"
76

8-
function NODEJS_UTILS.compare_versions(v1o, v2o)
7+
function UTIL.getBaseUrl()
8+
local mirror = os.getenv("VFOX_NODEJS_MIRROR")
9+
if mirror == "" or mirror == nil then
10+
return "https://nodejs.org/dist"
11+
end
12+
return mirror
13+
end
14+
15+
16+
17+
function UTIL.compare_versions(v1o, v2o)
918
local v1 = v1o.version
1019
local v2 = v2o.version
1120
local v1_parts = {}
@@ -32,7 +41,7 @@ function NODEJS_UTILS.compare_versions(v1o, v2o)
3241
end
3342

3443

35-
function NODEJS_UTILS.get_checksum(file_content, file_name)
44+
function UTIL.get_checksum(file_content, file_name)
3645
for line in string.gmatch(file_content, '([^\n]*)\n?') do
3746
local checksum, name = string.match(line, '(%w+)%s+(%S+)')
3847
if name == file_name then
@@ -42,21 +51,21 @@ function NODEJS_UTILS.get_checksum(file_content, file_name)
4251
return nil
4352
end
4453

45-
function NODEJS_UTILS.is_semver_simple(str)
54+
function UTIL.is_semver_simple(str)
4655
-- match pattern: three digits, separated by dot
4756
local pattern = "^%d+%.%d+%.%d+$"
4857
return str:match(pattern) ~= nil
4958
end
5059

5160

52-
function NODEJS_UTILS.extract_semver(semver)
61+
function UTIL.extract_semver(semver)
5362
local pattern = "^(%d+)%.(%d+)%.[%d.]+$"
5463
local major, minor = semver:match(pattern)
5564
return major, minor
5665
end
5766

5867

59-
function NODEJS_UTILS.calculate_shorthand(list)
68+
function UTIL.calculate_shorthand(list)
6069
local versions_shorthand = {}
6170
for _, v in ipairs(list) do
6271
local version = v.version
@@ -87,4 +96,4 @@ function NODEJS_UTILS.calculate_shorthand(list)
8796
return versions_shorthand
8897
end
8998

90-
return NODEJS_UTILS
99+
return UTIL

0 commit comments

Comments
 (0)