diff --git a/lib/inertia_rails/generators/helper.rb b/lib/inertia_rails/generators/helper.rb index f3a9e66..fb37cdb 100644 --- a/lib/inertia_rails/generators/helper.rb +++ b/lib/inertia_rails/generators/helper.rb @@ -4,7 +4,9 @@ module InertiaRails module Generators module Helper class << self - def guess_the_default_framework(package_json_path = Rails.root.join('package.json')) + DEFAULT_PACKAGE_PATH = Rails.root.join('package.json') + + def guess_the_default_framework(package_json_path = DEFAULT_PACKAGE_PATH) package_json = JSON.parse(package_json_path.read) dependencies = package_json['dependencies'] || {} @@ -25,8 +27,8 @@ def guess_typescript Rails.root.join('tsconfig.json').exist? end - def guess_inertia_template - if Rails.root.join('tailwind.config.js').exist? || Rails.root.join('tailwind.config.ts').exist? + def guess_inertia_template(package_json_path = DEFAULT_PACKAGE_PATH) + if package_json_path.read.include?('"tailwindcss"') 'inertia_tw_templates' else 'inertia_templates' diff --git a/spec/fixtures/package_json_files/tailwind_package.json b/spec/fixtures/package_json_files/tailwind_package.json new file mode 100644 index 0000000..f2a81ba --- /dev/null +++ b/spec/fixtures/package_json_files/tailwind_package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "tailwindcss": "4.0.0" + } +} diff --git a/spec/generators/generators_helper_spec.rb b/spec/generators/generators_helper_spec.rb index 89a2422..4d244ad 100644 --- a/spec/generators/generators_helper_spec.rb +++ b/spec/generators/generators_helper_spec.rb @@ -34,4 +34,19 @@ end end end + + describe '#guess_inertia_template' do + let(:package_json_path) { Pathname.new(File.expand_path("spec/fixtures/package_json_files/#{fixture_file_name}", Dir.pwd)) } + + shared_examples 'template detection' do |file_name, expected_template| + let(:fixture_file_name) { file_name } + + it "returns #{expected_template.inspect} when inspect \"#{file_name}\"" do + expect(described_class.guess_inertia_template(package_json_path)).to eq(expected_template) + end + end + + it_behaves_like 'template detection', 'tailwind_package.json', 'inertia_tw_templates' + it_behaves_like 'template detection', 'empty_package.json', 'inertia_templates' + end end