Current section

Files

Jump to
react_phoenix lib mix tasks react_phoenix.install.ex
Raw

lib/mix/tasks/react_phoenix.install.ex

defmodule Mix.Tasks.ReactPhoenix.Install do
@moduledoc """
Install the javascript file(s) necessary for ReactPhoenix to run properly
"""
use Mix.Task
def run(_args) do
info "\nInstalling ReactPhoenix..."
info "Copying javascript files..."
case File.cp(Path.join([package_path(), "priv", "js", "react_phoenix.js"]), Path.join([File.cwd!, "web", "static", "vendor", "react_phoenix.js"])) do
:ok ->
info "Done."
info "Adding required import to web/static/js/app.js..."
cmd "echo '\nimport reactPhoenix from \"./react_phoenix\"' >> ./web/static/js/app.js"
cmd "echo 'reactPhoenix()\n' >> ./web/static/js/app.js"
info "Done."
{:error, reason} -> error "Could not copy files: #{reason}"
end
info "\nIf you want the react_component function available in your view files,"
info "make sure to add `import ReactPhoenix` in the `view` section"
info "of your web/web.ex file."
end
defp info(txt), do: Mix.shell.info(txt)
defp error(txt), do: Mix.shell.error(txt)
defp cmd(txt), do: Mix.shell.cmd(txt)
def package_path do
__ENV__.file
|> Path.dirname()
|> String.split("/lib/mix")
|> hd()
end
end