Ash FrameworkAF
Ash Framework8mo ago
8 replies
Shahryar

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

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


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

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
Was this page helpful?