Skip to content

Commit

Permalink
feat(npm): add install_args option
Browse files Browse the repository at this point in the history
  • Loading branch information
amittamari committed Dec 19, 2023
1 parent 41e75af commit 650e7c3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
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
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
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",
"[email protected]",
vim.NIL, -- extra_packages
vim.NIL, -- install_extra_args
}
end)

Expand All @@ -77,6 +78,25 @@ describe("npm manager", function()
"install",
"[email protected]",
{ "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",
"[email protected]",
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)

0 comments on commit 650e7c3

Please sign in to comment.