Skip to content

Commit

Permalink
Merge pull request #130 from darwin67/remove-node
Browse files Browse the repository at this point in the history
Remove node dependencies
  • Loading branch information
bluzky authored Jan 22, 2025
2 parents 6b9a470 + 2f57373 commit 78a1183
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 17 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ node_modules/

/.vscode/
/.ctags.d/
.tags
.tags
/.direnv/
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
description = "Phoenix Liveview component library inspired by shadcn UI";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };

# NOTE:
# override since `elixir` is defaulting to 1.18 atm, and lexical doesn't support that version yet
lexical =
pkgs.lexical.override { elixir = pkgs.beam27Packages.elixir_1_17; };
in {
devShells.default = pkgs.mkShell {
packages = [ lexical ];

buildInputs = with pkgs; [
beam27Packages.elixir_1_17
beam27Packages.erlang

# LSPs
# lexical
erlang-ls
];
};
});
}
34 changes: 23 additions & 11 deletions lib/mix/tasks/salad.init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Mix.Tasks.Salad.Init do
@default_components_path "lib/%APP_NAME%_web/components"
@color_schemes ~w(zinc slate stone gray neutral red rose orange green blue yellow violet)
@default_color_scheme "gray"
@default_tailwind_animate_version "1.0.7"

@impl true
def run(argv) do
Expand All @@ -38,8 +39,6 @@ defmodule Mix.Tasks.Salad.Init do
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),
Expand All @@ -50,7 +49,7 @@ defmodule Mix.Tasks.Salad.Init do
: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
:ok <- install_tailwind_animate(opts) do
if opts[:as_lib] do
Mix.shell().info("Done. Now you can use any component by `import SaladUI.<ComponentName>` in your project.")
else
Expand Down Expand Up @@ -218,15 +217,28 @@ defmodule Mix.Tasks.Salad.Init do
File.write!(target_path, source_code)
end

defp install_node_dependencies(opts) do
if Keyword.get(opts, :skip, false) do
Mix.shell().info("Skipping npm install (running in test environment)")
else
Mix.shell().info("Installing tailwindcss-animate")
Mix.shell().cmd("npm install -D tailwindcss-animate --prefix assets")
end
defp install_tailwind_animate(opts) do
tag = Keyword.get(opts, :tailwind_animate_version, @default_tailwind_animate_version)
Mix.shell().info("Downloading tailwindcss-animate.js v#{tag}")

:ok
url = "https://raw.githubusercontent.com/jamiebuilds/tailwindcss-animate/refs/tags/v#{tag}/index.js"
output_path = Keyword.get(opts, :output_path, Path.join(File.cwd!(), "assets/vendor/tailwindcss-animate.js"))

:inets.start()
:ssl.start()

case :httpc.request(:get, {url, []}, [], body_format: :binary) do
{:ok, {{_version, 200, _reason_phrase}, _headers, body}} ->
# Write the body to file
File.write!(output_path, body)
:ok

{:ok, {{_version, status_code, _reason_phrase}, _headers, _body}} ->
{:error, "Failed to download tailwindcss-animate with status #{status_code}"}

{:error, reason} ->
{:error, "Failed to download tailwindcss-animate.js: #{inspect(reason)}"}
end
end

defp print_usage, do: Mix.shell().info(@moduledoc)
Expand Down
2 changes: 1 addition & 1 deletion lib/salad_ui/patcher/tailwind_patcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule SaladUI.Patcher.TailwindPatcher do
# so the user does not to manually format his `tailwind.config.js`
# file after patching it.

@plugins ["@tailwindcss/typography", "tailwindcss-animate"]
@plugins ["@tailwindcss/typography", "./vendor/tailwindcss-animate"]
@tailwind_colors "./tailwind.colors.json"

def patch(tailwind_config_path, opts \\ []) do
Expand Down
1 change: 1 addition & 0 deletions test/mix/salad.init_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule Mix.Tasks.Salad.InitTest do
File.mkdir_p!(Path.dirname(@application_file_path))
File.mkdir_p!("assets/css")
File.mkdir_p!("assets/js")
File.mkdir_p!("assets/vendor")
File.mkdir_p!(@default_components_path)

File.write!("config/config.exs", "import Config\n")
Expand Down
8 changes: 4 additions & 4 deletions test/salad_ui/patcher/tailwind_patcher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ defmodule SaladUI.Patcher.TailwindPatcherTest do
patched_content = File.read!(@tailwind_config)
assert patched_content =~ "plugins: ["
assert patched_content =~ "require(\"@tailwindcss/typography\")"
assert patched_content =~ "require(\"tailwindcss-animate\")"
assert patched_content =~ "require(\"./vendor/tailwindcss-animate\")"
end

test "patch/1 doesn't duplicate existing plugins" do
initial_content = """
module.exports = {
plugins: [
require("@tailwindcss/typography"),
require("tailwindcss-animate")
require("./vendor/tailwindcss-animate")
]
}
"""
Expand All @@ -41,11 +41,11 @@ defmodule SaladUI.Patcher.TailwindPatcherTest do
patched_content = File.read!(@tailwind_config)

assert patched_content =~ "require(\"@tailwindcss/typography\")"
assert patched_content =~ "require(\"tailwindcss-animate\")"
assert patched_content =~ "require(\"./vendor/tailwindcss-animate\")"

# Count occurrences of each plugin
typography_count = patched_content |> String.split("@tailwindcss/typography") |> length() |> Kernel.-(1)
animate_count = patched_content |> String.split("tailwindcss-animate") |> length() |> Kernel.-(1)
animate_count = patched_content |> String.split("./vendor/tailwindcss-animate") |> length() |> Kernel.-(1)

assert typography_count == 1
assert animate_count == 1
Expand Down

0 comments on commit 78a1183

Please sign in to comment.