Skip to content

Commit

Permalink
feat!: don't reinstall by default & add force install flag
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `--if-missing` is now the default, use `--force` to reinstall an existing version
  • Loading branch information
sheerlox committed Nov 30, 2023
1 parent a290e8f commit 0bb58d0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
5 changes: 1 addition & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import Config

config :nodelix,
version: "20.10.0",
npm: [
args: ["_build/dev/nodejs/versions/20.10.0/bin/npm"]
],
another: [
test_profile: [
args: ["--version"]
]
1 change: 1 addition & 0 deletions lib/mix/tasks/nodelix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule Mix.Tasks.Nodelix do
@dialyzer {:no_missing_calls, run: 1}

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

Expand Down
12 changes: 6 additions & 6 deletions lib/mix/tasks/nodelix.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Mix.Tasks.Nodelix.Install do
> API _should not_ be considered stable, and using a pinned version is _recommended_.
$ mix nodelix.install
$ mix nodelix.install --if-missing
$ mix nodelix.install --force
By default, it installs #{VersionManager.latest_lts_version()} but you
can configure it in your config files, such as:
Expand All @@ -24,17 +24,17 @@ defmodule Mix.Tasks.Nodelix.Install do
- `--runtime-config` - load the runtime configuration
before executing command
- `--if-missing` - install only if the given version
does not exist
- `--force` - install even if the given version is already present
"""

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

@impl Mix.Task
@spec run([String.t()]) :: :ok
def run(args) do
valid_options = [runtime_config: :boolean, if_missing: :boolean, assets: :boolean]
valid_options = [runtime_config: :boolean, force: :boolean, assets: :boolean]

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

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

configured_version = Nodelix.configured_version()

if opts[:if_missing] && VersionManager.is_installed?(configured_version) do
if VersionManager.is_installed?(configured_version) and !opts[:force] do
:ok
else
if function_exported?(Mix, :ensure_application!, 1) do
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/nodelix.npm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ defmodule Mix.Tasks.Nodelix.Npm do
alias Nodelix.VersionManager

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

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

Expand Down
26 changes: 15 additions & 11 deletions test/nodelix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,37 @@ defmodule NodelixTest do

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

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

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

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

Application.delete_env(:nodelix, :version)

Mix.Task.rerun("nodelix.install", ["--if-missing"])
Application.put_env(:nodelix, :version, @version)

assert ExUnit.CaptureIO.capture_io(fn ->
assert Mix.Task.run("nodelix", ["--version"]) == :ok
assert Mix.Task.rerun("nodelix", ["--version"]) == :ok
end) =~ @version
end

test "re-installs with force flag" do
assert ExUnit.CaptureLog.capture_log(fn ->
assert Mix.Task.rerun("nodelix.install", ["--force"]) == :ok
end) =~ "Succesfully installed Node.js v#{@version}"
end

test "installs with custom URL" do
assert :ok =
Mix.Task.rerun("nodelix.install", [
Expand Down

0 comments on commit 0bb58d0

Please sign in to comment.