Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Nov 28, 2023
1 parent b3a7934 commit 8e396d4
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 140 deletions.
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import Config

config :nodelix,
version: "20.10.0",
npm: [
args: ["_build/dev/nodejs/versions/20.10.0/bin/npm"]
],
another: [
args: ["--version"]
]
4 changes: 2 additions & 2 deletions lib/mix/tasks/nodelix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Mix.Tasks.Nodelix do
Example:
$ mix nodelix default --tls-min-v1.3
$ mix nodelix default some-script.js --some-option
If Node.js is not installed, it is automatically downloaded.
The arguments given to this task will be appended
Expand All @@ -24,7 +24,7 @@ defmodule Mix.Tasks.Nodelix do
Flags to control this Mix task must be given before the
profile:
$ mix nodelix --runtime-config default
$ mix nodelix --runtime-config default some-script.js --some-option
"""

@shortdoc "Invokes node with the profile and args"
Expand Down
21 changes: 9 additions & 12 deletions lib/mix/tasks/nodelix.install.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
defmodule Mix.Tasks.Nodelix.Install do
use Mix.Task

alias Nodelix.NodeDownloader
alias Nodelix.VersionManager

@moduledoc """
Installs Node.js.
$ mix nodelix.install
$ mix nodelix.install --if-missing
By default, it installs #{NodeDownloader.latest_lts_version()} but you
By default, it installs #{VersionManager.latest_lts_version()} but you
can configure it in your config files, such as:
config :nodelix, :version, "#{NodeDownloader.latest_lts_version()}"
config :nodelix, :version, "#{VersionManager.latest_lts_version()}"
## Options
Expand All @@ -34,7 +34,7 @@ defmodule Mix.Tasks.Nodelix.Install do
{opts, archive_base_url} =
case OptionParser.parse_head!(args, strict: valid_options) do
{opts, []} ->
{opts, NodeDownloader.default_archive_base_url()}
{opts, VersionManager.default_archive_base_url()}

{opts, [archive_base_url]} ->
{opts, archive_base_url}
Expand All @@ -44,15 +44,17 @@ defmodule Mix.Tasks.Nodelix.Install do
Invalid arguments to nodelix.install, expected one of:
mix nodelix.install
mix nodelix.install 'https://nodejs.org/dist/v$version/node-v$version-$target'
mix nodelix.install 'https://nodejs.org/dist/v$version/node-v$version-$target.$ext'
mix nodelix.install --runtime-config
mix nodelix.install --if-missing
""")
end

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

if opts[:if_missing] && latest_lts_version?() do
configured_version = Nodelix.configured_version()

if opts[:if_missing] && VersionManager.is_installed?(configured_version) do
:ok
else
if function_exported?(Mix, :ensure_application!, 1) do
Expand All @@ -61,12 +63,7 @@ defmodule Mix.Tasks.Nodelix.Install do
end

Mix.Task.run("loadpaths")
NodeDownloader.install(archive_base_url)
VersionManager.install(configured_version, archive_base_url)
end
end

defp latest_lts_version?() do
version = Nodelix.configured_version()
match?({:ok, ^version}, NodeDownloader.bin_version())
end
end
57 changes: 22 additions & 35 deletions lib/nodelix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Nodelix do
use Application
require Logger

alias Nodelix.NodeDownloader
alias Nodelix.VersionManager

@moduledoc """
Nodelix is an installer and runner for [Node.js](https://nodejs.org/).
Expand All @@ -14,14 +14,17 @@ defmodule Nodelix do
directory and environment:
config :nodelix,
version: "#{NodeDownloader.latest_lts_version()}",
version: "#{VersionManager.latest_lts_version()}",
default: [
args: ~w(
--version
some-script.js
--some-option
),
cd: Path.expand("../assets", __DIR__),
]
The default current directory is your project's root.
## Nodelix configuration
There are two global configurations for the nodelix application:
Expand All @@ -39,34 +42,18 @@ defmodule Nodelix do
Logger.warning("""
Node.js version is not configured. Please set it in your config files:
config :nodelix, :version, "#{NodeDownloader.latest_lts_version()}"
config :nodelix, :version, "#{VersionManager.latest_lts_version()}"
""")
end

configured_version = configured_version()

case NodeDownloader.bin_version() do
{:ok, ^configured_version} ->
:ok

{:ok, version} ->
Logger.warning("""
Outdated Node.js version. Expected #{configured_version}, got #{version}. \
Please run `mix nodelix.install` or update the version in your config files.\
""")

:error ->
:ok
end

Supervisor.start_link([], strategy: :one_for_one)
end

@doc """
Returns the configured Node.js version.
"""
def configured_version do
Application.get_env(:nodelix, :version, NodeDownloader.latest_lts_version())
Application.get_env(:nodelix, :version, VersionManager.latest_lts_version())
end

@doc """
Expand All @@ -77,13 +64,14 @@ defmodule Nodelix do
def config_for!(profile) when is_atom(profile) do
Application.get_env(:nodelix, profile) ||
raise ArgumentError, """
unknown nodelix profile. Make sure the profile is defined in your config/config.exs file, such as:
Unknown nodelix profile. Make sure the profile is defined in your config/config.exs file, such as:
config :nodelix,
version: "#{NodeDownloader.latest_lts_version()}",
version: "#{VersionManager.latest_lts_version()}",
#{profile}: [
args: ~w(
--version
some-script.js
--some-option
),
cd: Path.expand("../assets", __DIR__)
]
Expand All @@ -97,14 +85,15 @@ defmodule Nodelix do
The task output will be streamed directly to stdio. It
returns the status of the underlying call.
"""
def run(profile, extra_args) when is_atom(profile) and is_list(extra_args) do
def run(profile, extra_args \\ []) when is_atom(profile) and is_list(extra_args) do
config = config_for!(profile)
args = config[:args] || []
args = (config[:args] || []) ++ extra_args

if length(args) == 0, do: raise(ArgumentError, "No argument provided.")

env =
config
|> Keyword.get(:env, %{})
|> add_env_variable_to_ignore_browserslist_outdated_warning()

opts = [
cd: config[:cd] || File.cwd!(),
Expand All @@ -113,23 +102,21 @@ defmodule Nodelix do
stderr_to_stdout: true
]

NodeDownloader.bin_path()
|> System.cmd(args ++ extra_args, opts)
VersionManager.bin_path(:node, Nodelix.configured_version())
|> System.cmd(args, opts)
|> elem(1)
end

defp add_env_variable_to_ignore_browserslist_outdated_warning(env) do
Enum.into(env, %{"BROWSERSLIST_IGNORE_OLD_DATA" => "1"})
end

@doc """
Installs, if not available, and then runs `node`.
Returns the same as `run/2`.
"""
def install_and_run(profile, args) do
unless File.exists?(NodeDownloader.bin_path()) do
NodeDownloader.install()
version = configured_version()

unless VersionManager.is_installed?(version) do
VersionManager.install(version)
end

run(profile, args)
Expand Down
Loading

0 comments on commit 8e396d4

Please sign in to comment.