Skip to content

Commit

Permalink
modify salad.init task to automatically patch application.ex
Browse files Browse the repository at this point in the history
  • Loading branch information
selenil committed Dec 7, 2024
1 parent d36bdb4 commit f9e113e
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion lib/mix/tasks/salad.init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,24 @@ defmodule Mix.Tasks.Salad.Init do
env = Atom.to_string(Mix.env())
app_name = Mix.Project.config()[:app] |> Atom.to_string() |> String.downcase()
assets_path = build_assets_path(env)
application_file_path = Path.join(File.cwd!(), "lib/#{app_name}/application.ex")

node_opts = if env == "test", do: [skip: true], else: []

File.mkdir_p!(component_path)

with :ok <- write_config(component_path),
:ok <- init_tw_merge_cache(application_file_path),
:ok <- patch_css(color_scheme, assets_path),
:ok <- patch_js(assets_path),
:ok <- copy_tailwind_colors(assets_path),
:ok <- copy_zag_files(assets_path),
:ok <- patch_tailwind_config(opts),
:ok <- maybe_write_helpers_module(component_path, app_name, opts),
:ok <- maybe_write_component_module(component_path, app_name, opts),
:ok <- install_node_dependencies(node_opts) do
if opts[:as_lib] do
Mix.shell().info("Done. Now you can use any component by `import SaladUI.<ComponentName>` in your project.")
Mix.shell().info("Done. Now you can use any component by importSaladUI.<ComponentName> in your project.")
else
Mix.shell().info("Done. Now you can add components by running mix salad.add <component_name>")
end
Expand Down Expand Up @@ -117,6 +120,23 @@ defmodule Mix.Tasks.Salad.Init do
end
end

defp init_tw_merge_cache(application_file_path) do
cache_module = "TwMerge.Cache"
description = "Start TwMerge cache"

Mix.shell().info("Adding Tailwind merge cache to application supervisor")

if File.exists?(application_file_path) do
Patcher.patch_elixir_application(
application_file_path,
cache_module,
description
)
else
{:error, "application.ex not found"}
end
end

defp patch_css(color_scheme, assets_path) do
app_css_path = Path.join(File.cwd!(), "assets/css/app.css")
css_color_scheme_path = Path.join([assets_path, "colors", "#{color_scheme}.css"])
Expand Down Expand Up @@ -155,6 +175,25 @@ defmodule Mix.Tasks.Salad.Init do
:ok
end

defp copy_zag_files(assets_path) do
Mix.shell().info("Copying zag files to assets folder")
source_path = Path.join(assets_path, "zag")
target_path = Path.join(File.cwd!(), "assets/js/zag")

File.mkdir_p!(target_path)

Enum.each(File.ls!(source_path), fn file ->
unless File.exists?(Path.join(target_path, file)) do
File.cp!(Path.join(source_path, file), Path.join(target_path, file))
end
end)

Mix.shell().info("\nZagHook installed successfully")
Mix.shell().info("Do not forget to import it into your app.js file and pass it to your live socket")

:ok
end

defp patch_tailwind_config(opts) do
Mix.shell().info("Patching tailwind.config.js")
tailwind_config_path = Path.join(File.cwd!(), "assets/tailwind.config.js")
Expand Down

0 comments on commit f9e113e

Please sign in to comment.