Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(npm): add install_args option #1581

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lua/mason-core/installer/managers/npm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lua/mason-core/installer/registry/providers/npm.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local Result = require "mason-core.result"
local _ = require "mason-core.functional"
local providers = require "mason-core.providers"
local settings = require "mason.settings"

---@param purl Purl
local function purl_to_npm(purl)
Expand All @@ -24,6 +25,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)
Expand All @@ -39,6 +43,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
Expand Down
9 changes: 9 additions & 0 deletions lua/mason/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions tests/mason-core/installer/managers/npm_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe("npm manager", function()
"install",
"my-package@1.0.0",
vim.NIL, -- extra_packages
vim.NIL, -- install_extra_args
}
end)

Expand All @@ -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)

Expand Down
19 changes: 18 additions & 1 deletion tests/mason-core/installer/registry/providers/npm_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
)
Expand All @@ -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)