Skip to content

Commit d6b4d55

Browse files
committed
feat: allow overriding executable with environment variable
1 parent cbf5512 commit d6b4d55

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/tailwindcss/commands.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ def executable(
2525
MESSAGE
2626
end
2727

28-
exe_path = Dir.glob(File.expand_path(File.join(exe_path, "*", "tailwindcss"))).find do |f|
29-
Gem::Platform.match(Gem::Platform.new(File.basename(File.dirname(f))))
28+
# Let the user override the executable path with an environment variable
29+
# Useful for when running under Nix
30+
exe_file = ENV["TAILWINDCSS_RAILS_EXECUTABLE_PATH"]
31+
32+
if exe_file.nil? || exe_file.empty?
33+
exe_file = Dir.glob(File.expand_path(File.join(exe_path, "*", "tailwindcss"))).find do |f|
34+
Gem::Platform.match(Gem::Platform.new(File.basename(File.dirname(f))))
35+
end
3036
end
3137

32-
if exe_path.nil?
38+
if exe_file.nil? || exe_file.empty?
3339
raise ExecutableNotFoundException, <<~MESSAGE
3440
Cannot find the tailwindcss executable for #{platform} in #{exe_path}
3541
@@ -52,7 +58,7 @@ def executable(
5258
MESSAGE
5359
end
5460

55-
exe_path
61+
exe_file
5662
end
5763

5864
def compile_command(debug: false, **kwargs)

test/lib/tailwindcss/commands_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def mock_exe_directory(platform)
2626
end
2727
end
2828

29+
test ".executable accepts an override through the TAILWINDCSS_RAILS_EXECUTABLE_PATH environment variable" do
30+
filename = "/nix/store/li4jwsd16y68yjyznbffaf98jccipg36-tailwindcss-3.2.7/bin/tailwindcss"
31+
ENV["TAILWINDCSS_RAILS_EXECUTABLE_PATH"] = filename
32+
assert_equal(filename, Tailwindcss::Commands.executable)
33+
ENV["TAILWINDCSS_RAILS_EXECUTABLE_PATH"] = ""
34+
end
35+
2936
test ".executable raises UnsupportedPlatformException when we're not on a supported platform" do
3037
Gem::Platform.stub(:match, false) do # nothing is supported
3138
assert_raises(Tailwindcss::Commands::UnsupportedPlatformException) do

0 commit comments

Comments
 (0)