I’d like to call an anonymous function in my Mixfile aliases to install brunch for my web applications as part of an overall install alias. However, I can’t get the syntax of the anonymous function correct.
defp aliases do
["install": [&install_brunch/1]]
end
defp install_brunch(_) do
args =
for app <- Mix.Project.config[:web_apps] do
"--app #{app}"
end
|> Kernel.++(["npm install"])
Mix.shell.cmd args
end
I’m a little confused because run_alias/3 I thought was executing the function directly, but the stacktrace looks like it’s trying to execute it using Mix.Task.run/2.
According to the documentation of Mix.Shell. cmd/1 you are supposed to pass in a string and not a list of strings. Your alias function itself is called very well.
Ultimately it will need to be a list because the full Keyword list will be ["deps.unlock --all", "deps.get --no-archives-check", "deps.compile", &install_brunch/1, "ecto.setup"].
But I did remove the backets and try it to no avail
Can you try joining the args in install_brunch/1 with Enum.join(args, " "), please?
defp install_brunch(_) do
args =
for app <- Mix.Project.config[:web_apps] do
"--app #{app}"
end
|> Kernel.++(["npm install"])
args |> Enum.join(" ") |> Mix.shell.cmd
end
defp install_brunch(_) do
args =
for app <- Mix.Project.config[:web_apps] do
"--app #{app}"
end
|> Kernel.++(["npm install"])
|> Enum.join(" ")
Mix.shell.cmd args
end
Produces:
$mix install
warning: undefined behaviour function run/1 (for behaviour Mix.Task)
mix.exs:1
sh: --: invalid option
Usage: sh [GNU long option] [option] ...
sh [GNU long option] [option] script-file ...
GNU long options:
--debug
--debugger
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--protected
--rcfile
--restricted
--verbose
--version
--wordexp
Shell options:
-irsD or -c command or -O shopt_option (invocation only)
-abefhkmnptuvxBCHP or -o option
You are trying to run this command: “–app foo --app bar npm install”. If you print the args before calling Mix.shell.cmd you should be able to verify it.
Yup, printing the args verifies they are correct. But it doesn’t produce the same result as when I run mix cmd --app admin --app web npm install from the CLI (which runs correctly).
$mix install
"--app admin --app web npm install"
sh: --: invalid option
Usage: sh [GNU long option] [option] ...
sh [GNU long option] [option] script-file ...
GNU long options:
--debug
--debugger
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--protected
--rcfile
--restricted
--verbose
--version
--wordexp
Shell options:
-irsD or -c command or -O shopt_option (invocation only)
-abefhkmnptuvxBCHP or -o option
Produced by:
defp aliases do
["install": [&install_brunch/1]]
end
defp install_brunch(_) do
args =
for app <- Mix.Project.config[:web_apps] do
"--app #{app}"
end
|> Kernel.++(["npm install"])
|> Enum.join(" ")
IO.inspect args
Mix.shell.cmd args
end
Mix.shell.cmd is not related to the “mix cmd” task. If you want to invoke
“mix cmd” then you need to prefix the argument you are passing to
Mix.shell.cmd with that. Or you can call Mix.Task.run(“cmd”, args) where
args is a list of arguments as you originally had.