From 650e7c32ac06a8a71317411f57ce54b3f0d41121 Mon Sep 17 00:00:00 2001 From: Amit Tamari <15573913+amittamari@users.noreply.github.com> Date: Tue, 19 Dec 2023 10:39:32 +0200 Subject: [PATCH] feat(npm): add `install_args` option --- lua/mason-core/installer/managers/npm.lua | 3 ++- .../installer/registry/providers/npm.lua | 5 +++++ lua/mason/settings.lua | 9 +++++++++ .../installer/managers/npm_spec.lua | 20 +++++++++++++++++++ .../installer/registry/providers/npm_spec.lua | 19 +++++++++++++++++- 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lua/mason-core/installer/managers/npm.lua b/lua/mason-core/installer/managers/npm.lua index 10a3e9fd5..0a79d7d5f 100644 --- a/lua/mason-core/installer/managers/npm.lua +++ b/lua/mason-core/installer/managers/npm.lua @@ -57,7 +57,7 @@ end ---@async ---@param pkg string ---@param version string ----@param opts? { extra_packages?: string[] } +---@param opts? { extra_packages?: string[], install_extra_args?: string[] } function M.install(pkg, version, opts) opts = opts or {} log.fmt_debug("npm: install %s %s %s", pkg, version, opts) @@ -67,6 +67,7 @@ function M.install(pkg, version, opts) "install", ("%s@%s"):format(pkg, version), opts.extra_packages or vim.NIL, + opts.install_extra_args or vim.NIL, } end diff --git a/lua/mason-core/installer/registry/providers/npm.lua b/lua/mason-core/installer/registry/providers/npm.lua index d1865b965..4d93a0343 100644 --- a/lua/mason-core/installer/registry/providers/npm.lua +++ b/lua/mason-core/installer/registry/providers/npm.lua @@ -2,6 +2,7 @@ local Result = require "mason-core.result" local _ = require "mason-core.functional" local providers = require "mason-core.providers" local util = require "mason-core.installer.registry.util" +local settings = require "mason.settings" ---@param purl Purl local function purl_to_npm(purl) @@ -25,6 +26,9 @@ function M.parse(source, purl) package = purl_to_npm(purl), version = purl.version, extra_packages = source.extra_packages, + npm = { + extra_args = settings.current.npm.install_args, + }, } return Result.success(parsed_source) @@ -40,6 +44,7 @@ function M.install(ctx, source) try(npm.init()) try(npm.install(source.package, source.version, { extra_packages = source.extra_packages, + install_extra_args = source.npm.extra_args, })) end) end diff --git a/lua/mason/settings.lua b/lua/mason/settings.lua index 56fbcfb9f..307ae78d6 100644 --- a/lua/mason/settings.lua +++ b/lua/mason/settings.lua @@ -68,6 +68,15 @@ local DEFAULT_SETTINGS = { install_args = {}, }, + npm = { + ---@since 1.0.0 + -- These args will be added to `npm install` calls. Note that setting extra args might impact intended behavior + -- and is not recommended. + -- + -- Example: { "--registry", "https://registry.npmjs.org/" } + install_args = {}, + }, + ui = { ---@since 1.0.0 -- Whether to automatically check for new versions when opening the :Mason window. diff --git a/tests/mason-core/installer/managers/npm_spec.lua b/tests/mason-core/installer/managers/npm_spec.lua index 59a8c84f7..2be8045bd 100644 --- a/tests/mason-core/installer/managers/npm_spec.lua +++ b/tests/mason-core/installer/managers/npm_spec.lua @@ -61,6 +61,7 @@ describe("npm manager", function() "install", "my-package@1.0.0", vim.NIL, -- extra_packages + vim.NIL, -- install_extra_args } end) @@ -77,6 +78,25 @@ describe("npm manager", function() "install", "my-package@1.0.0", { "extra-package" }, + vim.NIL, --install_extra_args + } + end) + + it("should install with extra args", function() + local ctx = create_dummy_context() + + installer.exec_in_context(ctx, function() + npm.install("my-package", "1.0.0", { + install_extra_args = { "--registry", "https://registry.npmjs.org/" }, + }) + end) + + assert.spy(ctx.spawn.npm).was_called(1) + assert.spy(ctx.spawn.npm).was_called_with { + "install", + "my-package@1.0.0", + vim.NIL, --extra_packages + { "--registry", "https://registry.npmjs.org/" }, -- install_extra_args } end) diff --git a/tests/mason-core/installer/registry/providers/npm_spec.lua b/tests/mason-core/installer/registry/providers/npm_spec.lua index b39d092ae..fecaf4fb3 100644 --- a/tests/mason-core/installer/registry/providers/npm_spec.lua +++ b/tests/mason-core/installer/registry/providers/npm_spec.lua @@ -2,6 +2,7 @@ local Purl = require "mason-core.purl" local Result = require "mason-core.result" local installer = require "mason-core.installer" local npm = require "mason-core.installer.registry.providers.npm" +local settings = require "mason.settings" local stub = require "luassert.stub" ---@param overrides Purl @@ -15,11 +16,20 @@ end describe("npm provider :: parsing", function() it("should parse package", function() + settings.set { + npm = { + install_args = { "--registry", "https://registry.npmjs.org/" }, + }, + } + assert.same( Result.success { package = "@namespace/package", version = "v1.5.0", extra_packages = { "extra" }, + npm = { + extra_args = { "--registry", "https://registry.npmjs.org/" }, + }, }, npm.parse({ extra_packages = { "extra" } }, purl()) ) @@ -38,12 +48,19 @@ describe("npm provider :: installing", function() package = "@namespace/package", version = "v1.5.0", extra_packages = { "extra" }, + npm = { + extra_args = { "--registry", "https://registry.npmjs.org/" }, + }, }) end) assert.is_true(result:is_success()) assert.spy(manager.init).was_called(1) assert.spy(manager.install).was_called(1) - assert.spy(manager.install).was_called_with("@namespace/package", "v1.5.0", { extra_packages = { "extra" } }) + assert.spy(manager.install).was_called_with( + "@namespace/package", + "v1.5.0", + { extra_packages = { "extra" }, install_extra_args = { "--registry", "https://registry.npmjs.org/" } } + ) end) end)