Current section
Files
Jump to
Current section
Files
lib/mix/tasks/toddy.inspect.ex
defmodule Mix.Tasks.Toddy.Inspect do
@moduledoc "Inspect a Toddy app's initial view tree without a renderer."
@shortdoc "Print the initial UI tree as JSON"
use Mix.Task
@impl true
def run(args) do
Mix.Task.run("app.start")
case args do
[module_string | _] ->
module = Module.concat([module_string])
unless Code.ensure_loaded?(module) do
Mix.raise("Module #{module_string} not found")
end
# Call init to get initial model
{model, _commands} =
case module.init([]) do
{model, cmds} when is_list(cmds) -> {model, cmds}
{model, %Toddy.Command{} = cmd} -> {model, [cmd]}
model -> {model, []}
end
# Call view to get the tree
raw_tree = module.view(model)
tree = Toddy.Tree.normalize(raw_tree)
# Pretty-print as JSON
json = Jason.encode!(tree, pretty: true)
Mix.shell().info(json)
[] ->
Mix.raise("Usage: mix toddy.inspect MyApp")
end
end
end