Skip to content

Commit 0bb58d0

Browse files
committed
feat!: don't reinstall by default & add force install flag
BREAKING CHANGE: `--if-missing` is now the default, use `--force` to reinstall an existing version
1 parent a290e8f commit 0bb58d0

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

config/config.exs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import Config
22

33
config :nodelix,
44
version: "20.10.0",
5-
npm: [
6-
args: ["_build/dev/nodejs/versions/20.10.0/bin/npm"]
7-
],
8-
another: [
5+
test_profile: [
96
args: ["--version"]
107
]

lib/mix/tasks/nodelix.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ defmodule Mix.Tasks.Nodelix do
3838
@dialyzer {:no_missing_calls, run: 1}
3939

4040
@impl Mix.Task
41+
@spec run([String.t()]) :: :ok
4142
def run(args) do
4243
switches = [profile: :string, runtime_config: :boolean]
4344

lib/mix/tasks/nodelix.install.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule Mix.Tasks.Nodelix.Install do
1212
> API _should not_ be considered stable, and using a pinned version is _recommended_.
1313
1414
$ mix nodelix.install
15-
$ mix nodelix.install --if-missing
15+
$ mix nodelix.install --force
1616
1717
By default, it installs #{VersionManager.latest_lts_version()} but you
1818
can configure it in your config files, such as:
@@ -24,17 +24,17 @@ defmodule Mix.Tasks.Nodelix.Install do
2424
- `--runtime-config` - load the runtime configuration
2525
before executing command
2626
27-
- `--if-missing` - install only if the given version
28-
does not exist
27+
- `--force` - install even if the given version is already present
2928
"""
3029

3130
@shortdoc "Installs Node.js"
3231
@compile {:no_warn_undefined, Mix}
3332
@dialyzer {:no_missing_calls, run: 1}
3433

3534
@impl Mix.Task
35+
@spec run([String.t()]) :: :ok
3636
def run(args) do
37-
valid_options = [runtime_config: :boolean, if_missing: :boolean, assets: :boolean]
37+
valid_options = [runtime_config: :boolean, force: :boolean, assets: :boolean]
3838

3939
{opts, archive_base_url} =
4040
case OptionParser.parse_head!(args, strict: valid_options) do
@@ -51,15 +51,15 @@ defmodule Mix.Tasks.Nodelix.Install do
5151
mix nodelix.install
5252
mix nodelix.install 'https://nodejs.org/dist/v$version/node-v$version-$target.$ext'
5353
mix nodelix.install --runtime-config
54-
mix nodelix.install --if-missing
54+
mix nodelix.install --force
5555
""")
5656
end
5757

5858
if opts[:runtime_config], do: Mix.Task.run("app.config")
5959

6060
configured_version = Nodelix.configured_version()
6161

62-
if opts[:if_missing] && VersionManager.is_installed?(configured_version) do
62+
if VersionManager.is_installed?(configured_version) and !opts[:force] do
6363
:ok
6464
else
6565
if function_exported?(Mix, :ensure_application!, 1) do

lib/mix/tasks/nodelix.npm.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ defmodule Mix.Tasks.Nodelix.Npm do
2525
alias Nodelix.VersionManager
2626

2727
@shortdoc "Invokes npm with the provided arguments"
28-
@dialyzer {:no_missing_calls, run: 1}
2928

3029
@impl Mix.Task
30+
@spec run([String.t()]) :: :ok
3131
def run(args) do
3232
npm_path = VersionManager.bin_path(:npm, Nodelix.configured_version())
3333

test/nodelix_test.exs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,37 @@ defmodule NodelixTest do
1515

1616
test "run without profile" do
1717
assert ExUnit.CaptureIO.capture_io(fn ->
18-
assert Mix.Task.run("nodelix", ["--version"]) == :ok
18+
assert Mix.Task.rerun("nodelix", ["--version"]) == :ok
1919
end) =~ @version
2020
end
2121

2222
test "run on another profile" do
2323
assert ExUnit.CaptureIO.capture_io(fn ->
24-
assert Mix.Task.run("nodelix", ["--profile", "another", "--version"]) == :ok
24+
assert Mix.Task.rerun("nodelix", ["--profile", "test_profile"]) == :ok
2525
end) =~ @version
2626
end
2727

28-
test "updates on install" do
29-
Application.put_env(:nodelix, :version, "20.9.0")
30-
Mix.Task.rerun("nodelix.install", ["--if-missing"])
28+
test "installs and runs multiple versions" do
29+
Application.put_env(:nodelix, :version, "18.18.2")
30+
Mix.Task.rerun("nodelix.install")
3131

3232
assert ExUnit.CaptureIO.capture_io(fn ->
33-
assert Mix.Task.run("nodelix", ["--version"]) == :ok
34-
end) =~ "20.9.0"
33+
assert Mix.Task.rerun("nodelix", ["--version"]) == :ok
34+
end) =~ "18.18.2"
3535

36-
Application.delete_env(:nodelix, :version)
37-
38-
Mix.Task.rerun("nodelix.install", ["--if-missing"])
36+
Application.put_env(:nodelix, :version, @version)
3937

4038
assert ExUnit.CaptureIO.capture_io(fn ->
41-
assert Mix.Task.run("nodelix", ["--version"]) == :ok
39+
assert Mix.Task.rerun("nodelix", ["--version"]) == :ok
4240
end) =~ @version
4341
end
4442

43+
test "re-installs with force flag" do
44+
assert ExUnit.CaptureLog.capture_log(fn ->
45+
assert Mix.Task.rerun("nodelix.install", ["--force"]) == :ok
46+
end) =~ "Succesfully installed Node.js v#{@version}"
47+
end
48+
4549
test "installs with custom URL" do
4650
assert :ok =
4751
Mix.Task.rerun("nodelix.install", [

0 commit comments

Comments
 (0)