diff options
| author | William Boman <william@redwill.se> | 2023-04-04 22:02:55 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-04 20:02:55 +0000 |
| commit | c625abf171dfaad790f5703465a93a94334e5049 (patch) | |
| tree | 46d9a2825954c29fb190ac69b8b7d21981074624 /lua | |
| parent | test(async): add tests for control mechanisms (#1176) (diff) | |
| download | mason-c625abf171dfaad790f5703465a93a94334e5049.tar mason-c625abf171dfaad790f5703465a93a94334e5049.tar.gz mason-c625abf171dfaad790f5703465a93a94334e5049.tar.bz2 mason-c625abf171dfaad790f5703465a93a94334e5049.tar.lz mason-c625abf171dfaad790f5703465a93a94334e5049.tar.xz mason-c625abf171dfaad790f5703465a93a94334e5049.tar.zst mason-c625abf171dfaad790f5703465a93a94334e5049.zip | |
fix(npm): set install-strategy on npm >= 9 (#1179)
Closes #1175.
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/mason-core/installer/managers/npm.lua | 24 | ||||
| -rw-r--r-- | lua/mason-core/semver.lua | 9 | ||||
| -rw-r--r-- | lua/mason-vendor/semver.lua | 11 |
3 files changed, 40 insertions, 4 deletions
diff --git a/lua/mason-core/installer/managers/npm.lua b/lua/mason-core/installer/managers/npm.lua index 61cf3a38..f8312829 100644 --- a/lua/mason-core/installer/managers/npm.lua +++ b/lua/mason-core/installer/managers/npm.lua @@ -4,10 +4,26 @@ local installer = require "mason-core.installer" local log = require "mason-core.log" local path = require "mason-core.path" local platform = require "mason-core.platform" +local semver = require "mason-core.semver" +local spawn = require "mason-core.spawn" local M = {} ---@async +---@param predicate fun(npm_version: Semver): boolean +---@return boolean +local function npm_version_satisfies(predicate) + return Result.try(function(try) + local npm_versions = try(spawn.npm { "version", "--json" }).stdout + ---@type { npm: string } + local versions = try(Result.pcall(vim.json.decode, npm_versions)) + ---@type Semver + local npm_version = try(semver.parse(versions.npm)) + return predicate(npm_version) + end):get_or_else(false) +end + +---@async function M.init() log.debug "npm: init" local ctx = installer.context() @@ -18,7 +34,7 @@ function M.init() "--scope=mason", }) - -- Use global-style. The reasons for this are: + -- Use shallow install-strategy. The reasons for this are: -- a) To avoid polluting the executables (aka bin-links) that npm creates. -- b) The installation is, after all, more similar to a "global" installation. We don't really gain -- any of the benefits of not using global style (e.g., deduping the dependency tree). @@ -27,7 +43,11 @@ function M.init() -- is a bit unreliable across npm versions (especially <7), so we take extra measures to avoid -- inadvertently polluting global npm config. try(Result.pcall(function() - ctx.fs:append_file(".npmrc", "global-style=true") + if npm_version_satisfies(_.gte(semver.new "9.0.0")) then + ctx.fs:append_file(".npmrc", "\ninstall-strategy=shallow") + else + ctx.fs:append_file(".npmrc", "\nglobal-style=true") + end end)) ctx.stdio_sink.stdout "Initialized npm root\n" diff --git a/lua/mason-core/semver.lua b/lua/mason-core/semver.lua index e10f006b..635b7e36 100644 --- a/lua/mason-core/semver.lua +++ b/lua/mason-core/semver.lua @@ -4,9 +4,14 @@ local semver = require "mason-vendor.semver" local M = {} ---@param version string -function M.parse(version) +function M.new(version) version = version:gsub("^v", "") - return Result.pcall(semver, version) + return semver(version) +end + +---@param version string +function M.parse(version) + return Result.pcall(M.new, version) end return M diff --git a/lua/mason-vendor/semver.lua b/lua/mason-vendor/semver.lua index b61e3b40..189515df 100644 --- a/lua/mason-vendor/semver.lua +++ b/lua/mason-vendor/semver.lua @@ -144,18 +144,28 @@ local function smallerPrerelease(mine, other) return smallerIdList(splitByDot(mine), splitByDot(other)) end +---@class ISemver local methods = {} +---@return Semver function methods:nextMajor() return semver(self.major + 1, 0, 0) end +---@return Semver function methods:nextMinor() return semver(self.major, self.minor + 1, 0) end +---@return Semver function methods:nextPatch() return semver(self.major, self.minor, self.patch + 1) end +---@class Semver : ISemver +---@field major integer +---@field minor integer +---@field patch integer +---@field prerelease? string +---@field build? string local mt = { __index = methods } function mt:__eq(other) return self.major == other.major and @@ -188,6 +198,7 @@ function mt:__tostring() return table.concat(buffer) end +---@return Semver local function new(major, minor, patch, prerelease, build) assert(major, "At least one parameter is needed") |
