-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathcommands.rb
71 lines (61 loc) · 2.38 KB
/
commands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require "tailwindcss/ruby"
module Tailwindcss
module Commands
class << self
def compile_command(debug: false, **kwargs)
debug = ENV["TAILWINDCSS_DEBUG"].present? if ENV.key?("TAILWINDCSS_DEBUG")
rails_root = defined?(Rails) ? Rails.root : Pathname.new(Dir.pwd)
command = [
Tailwindcss::Ruby.executable(**kwargs),
"-i", rails_root.join("app/assets/tailwind/application.css").to_s,
"-o", rails_root.join("app/assets/builds/tailwind.css").to_s,
]
command << "--minify" unless (debug || rails_css_compressor?)
postcss_path = rails_root.join("postcss.config.js")
command += ["--postcss", postcss_path.to_s] if File.exist?(postcss_path)
command
end
def watch_command(always: false, poll: false, **kwargs)
compile_command(**kwargs).tap do |command|
command << "-w"
command << "always" if always
command << "-p" if poll
end
end
def rails_css_compressor?
defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
end
def engines_tailwindcss_roots
return [] unless defined?(Rails)
Rails::Engine.subclasses.select do |engine|
begin
spec = Gem::Specification.find_by_name(engine.engine_name)
spec.dependencies.any? { |d| d.name == 'tailwindcss-rails' }
rescue Gem::MissingSpecError
false
end
end.map do |engine|
[
Rails.root.join("app/assets/tailwind/#{engine.engine_name}/application.css"),
engine.root.join("app/assets/tailwind/#{engine.engine_name}/application.css")
].select(&:exist?).compact.first.to_s
end.compact
end
def enhance_command(command)
engine_roots = Tailwindcss::Commands.engines_tailwindcss_roots
if engine_roots.any?
Tempfile.create('tailwind.css') do |file|
file.write(engine_roots.map { |root| "@import \"#{root}\";" }.join("\n"))
file.write("\n@import \"#{Rails.root.join('app/assets/tailwind/application.css')}\";\n")
file.rewind
transformed_command = command.dup
transformed_command[2] = file.path
yield transformed_command if block_given?
end
else
yield command if block_given?
end
end
end
end
end