Skip to content

Commit

Permalink
Look up tailwind.config.js anywhere in the app directory (except dirs…
Browse files Browse the repository at this point in the history
… ignored explicitly)
  • Loading branch information
trinitytakei committed Jun 24, 2024
1 parent a070d5d commit 0e5b49d
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions lib/generators/phlex/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ module Phlex::Generators
class InstallGenerator < ::Rails::Generators::Base
source_root File.expand_path("templates", __dir__)

IGNORED_TAILWIND_CONFIGURATION_LOOKUP_DIRECTORIES = %w[
.git
.github
.ruby-lsp
bin
coverage
db
docs
log
node_modules
public
spec
storage
test
tmp
vendor
].freeze

APPLICATION_CONFIGURATION_PATH = Rails.root.join("config/application.rb")
TAILWIND_CONFIGURATION_PATH = Rails.root.join("tailwind.config.js")

def autoload_components
return unless APPLICATION_CONFIGURATION_PATH.exist?
Expand Down Expand Up @@ -38,9 +55,9 @@ def autoload_views
end

def configure_tailwind
return unless TAILWIND_CONFIGURATION_PATH.exist?
return unless tailwind_configuration_path.exist?

insert_into_file TAILWIND_CONFIGURATION_PATH, after: "content: [" do
insert_into_file tailwind_configuration_path, after: "content: [" do
"\n './app/views/**/*.rb'," \
end
end
Expand All @@ -56,5 +73,20 @@ def create_application_layout
def create_application_view
template "application_view.rb", Rails.root.join("app/views/application_view.rb")
end

private

def tailwind_configuration_path
@_tailwind_configuration_path ||=
Pathname.new(filtered_tailwind_configuration_files.first)
end

def filtered_tailwind_configuration_files
Dir.glob("#{Rails.root}/**/tailwind.config.js").grep_v(ignored_directories_regexp)
end

def ignored_directories_regexp
Regexp.new(IGNORED_TAILWIND_CONFIGURATION_LOOKUP_DIRECTORIES.join('|'))
end
end
end

0 comments on commit 0e5b49d

Please sign in to comment.