How can change dir in Igniter.add_task cross platform? (suggestion)

i have no windows to test it, just get the response from claude that can be wrong it says it is not cross platform code for example in windows has problem
def run_install(igniter) do
package_manager = Map.get(igniter.assigns, :package_manager, :npm)

igniter
|> Igniter.add_notice("Running #{package_manager} install in assets directory...")
|> Igniter.add_task("cmd", ["--cd", "assets", Atom.to_string(package_manager), "install"])
end
def run_install(igniter) do
package_manager = Map.get(igniter.assigns, :package_manager, :npm)

igniter
|> Igniter.add_notice("Running #{package_manager} install in assets directory...")
|> Igniter.add_task("cmd", ["--cd", "assets", Atom.to_string(package_manager), "install"])
end
So i decided to change it to this
defmodule Mix.Tasks.Mishka.Assets.Install do
@shortdoc "Runs package manager install in assets directory"
@moduledoc false

use Mix.Task

@impl Mix.Task
def run([package_manager]) do
File.cd!("assets", fn ->
IO.puts("\n#{IO.ANSI.cyan()}Running #{package_manager} install...#{IO.ANSI.reset()}")

case System.cmd(package_manager, ["install"], into: IO.stream(:stdio, :line)) do
{_, 0} ->
IO.puts("#{IO.ANSI.green()}✓ Dependencies installed successfully!#{IO.ANSI.reset()}")

{_, exit_code} ->
IO.puts(
"#{IO.ANSI.red()}✗ Failed to install dependencies (exit code: #{exit_code})#{IO.ANSI.reset()}"
)
end
end)
end
end
defmodule Mix.Tasks.Mishka.Assets.Install do
@shortdoc "Runs package manager install in assets directory"
@moduledoc false

use Mix.Task

@impl Mix.Task
def run([package_manager]) do
File.cd!("assets", fn ->
IO.puts("\n#{IO.ANSI.cyan()}Running #{package_manager} install...#{IO.ANSI.reset()}")

case System.cmd(package_manager, ["install"], into: IO.stream(:stdio, :line)) do
{_, 0} ->
IO.puts("#{IO.ANSI.green()}✓ Dependencies installed successfully!#{IO.ANSI.reset()}")

{_, exit_code} ->
IO.puts(
"#{IO.ANSI.red()}✗ Failed to install dependencies (exit code: #{exit_code})#{IO.ANSI.reset()}"
)
end
end)
end
end
And change the command runner function like this
def run_install(igniter) do
File.cwd() |> IO.inspect(label: "we are here=====>")
package_manager = Map.get(igniter.assigns, :package_manager, :npm)

igniter
|> Igniter.add_notice("Running #{package_manager} install in assets directory...")
|> Igniter.add_task("mishka.assets.install", [Atom.to_string(package_manager)])
end
def run_install(igniter) do
File.cwd() |> IO.inspect(label: "we are here=====>")
package_manager = Map.get(igniter.assigns, :package_manager, :npm)

igniter
|> Igniter.add_notice("Running #{package_manager} install in assets directory...")
|> Igniter.add_task("mishka.assets.install", [Atom.to_string(package_manager)])
end
it works, but it is a good way? or the Igniter has better way for this? Thank you in advance
Solution:
So you need to write a mix task that does what you want
Jump to solution
4 Replies
ZachDaniel
ZachDaniel2w ago
Tasks are only for mix tasks Not for arbitrary shell commands
Solution
ZachDaniel
ZachDaniel2w ago
So you need to write a mix task that does what you want
ZachDaniel
ZachDaniel2w ago
And invoke it with add_task
Shahryar
ShahryarOP2w ago
so the second way i did is right? i created a Mix.Tasks.Mishka.Assets.Install and run in add_task

Did you find this page helpful?