diff --git a/lib/inertia_rails/generators/helper.rb b/lib/inertia_rails/generators/helper.rb index d8f8166f..f3a9e669 100644 --- a/lib/inertia_rails/generators/helper.rb +++ b/lib/inertia_rails/generators/helper.rb @@ -4,14 +4,16 @@ module InertiaRails module Generators module Helper class << self - def guess_the_default_framework - package = Rails.root.join('package.json').read - case package - when %r{@inertiajs/react} + def guess_the_default_framework(package_json_path = Rails.root.join('package.json')) + package_json = JSON.parse(package_json_path.read) + dependencies = package_json['dependencies'] || {} + + if dependencies['@inertiajs/react'] 'react' - when %r{@inertiajs/svelte} - package.match?(/"svelte": "\^5/) ? 'svelte' : 'svelte4' - when %r{@inertiajs/vue3} + elsif dependencies['@inertiajs/svelte'] + version = dependencies['svelte'].gsub(/[\^~]/, '') # Remove ^ or ~ from version + version.start_with?('5') ? 'svelte' : 'svelte4' + elsif dependencies['@inertiajs/vue3'] 'vue' else Thor::Shell::Basic.new.say_error 'Could not determine the Inertia.js framework you are using.' diff --git a/spec/fixtures/package_json_files/empty_package.json b/spec/fixtures/package_json_files/empty_package.json new file mode 100644 index 00000000..18a1e415 --- /dev/null +++ b/spec/fixtures/package_json_files/empty_package.json @@ -0,0 +1,3 @@ +{ + "dependencies": {} +} diff --git a/spec/fixtures/package_json_files/react_package.json b/spec/fixtures/package_json_files/react_package.json new file mode 100644 index 00000000..5495cbcb --- /dev/null +++ b/spec/fixtures/package_json_files/react_package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "@inertiajs/react": "1.0.0" + } +} diff --git a/spec/fixtures/package_json_files/svelte4_package.json b/spec/fixtures/package_json_files/svelte4_package.json new file mode 100644 index 00000000..0dbf43d4 --- /dev/null +++ b/spec/fixtures/package_json_files/svelte4_package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@inertiajs/svelte": "1.0.0", + "svelte": "^4.0.0" + } +} diff --git a/spec/fixtures/package_json_files/svelte5_caret_package.json b/spec/fixtures/package_json_files/svelte5_caret_package.json new file mode 100644 index 00000000..5d12c146 --- /dev/null +++ b/spec/fixtures/package_json_files/svelte5_caret_package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@inertiajs/svelte": "1.0.0", + "svelte": "^5.0.0" + } +} diff --git a/spec/fixtures/package_json_files/svelte5_exact_package.json b/spec/fixtures/package_json_files/svelte5_exact_package.json new file mode 100644 index 00000000..ce87e0aa --- /dev/null +++ b/spec/fixtures/package_json_files/svelte5_exact_package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@inertiajs/svelte": "1.0.0", + "svelte": "5.0.0" + } +} diff --git a/spec/fixtures/package_json_files/svelte5_tilde_package.json b/spec/fixtures/package_json_files/svelte5_tilde_package.json new file mode 100644 index 00000000..bce22e10 --- /dev/null +++ b/spec/fixtures/package_json_files/svelte5_tilde_package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@inertiajs/svelte": "1.0.0", + "svelte": "~5.0.0" + } +} diff --git a/spec/fixtures/package_json_files/vue_package.json b/spec/fixtures/package_json_files/vue_package.json new file mode 100644 index 00000000..87c68965 --- /dev/null +++ b/spec/fixtures/package_json_files/vue_package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "@inertiajs/vue3": "1.0.0" + } +} diff --git a/spec/generators/generators_helper_spec.rb b/spec/generators/generators_helper_spec.rb new file mode 100644 index 00000000..89a24221 --- /dev/null +++ b/spec/generators/generators_helper_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'thor' +require_relative '../../lib/inertia_rails/generators/helper' + +RSpec.describe InertiaRails::Generators::Helper, type: :helper do + describe '#guess_the_default_framework' do + let(:package_json_path) { Pathname.new(File.expand_path("spec/fixtures/package_json_files/#{fixture_file_name}", Dir.pwd)) } + + shared_examples 'framework detection' do |file_name, expected_framework| + let(:fixture_file_name) { file_name } + + it "returns #{expected_framework.inspect} when inspect \"#{file_name}\"" do + expect(described_class.guess_the_default_framework(package_json_path)).to eq(expected_framework) + end + end + + it_behaves_like 'framework detection', 'react_package.json', 'react' + it_behaves_like 'framework detection', 'svelte5_caret_package.json', 'svelte' + it_behaves_like 'framework detection', 'svelte5_exact_package.json', 'svelte' + it_behaves_like 'framework detection', 'svelte5_tilde_package.json', 'svelte' + it_behaves_like 'framework detection', 'svelte4_package.json', 'svelte4' + it_behaves_like 'framework detection', 'vue_package.json', 'vue' + + # Handle exception + context 'when framework cannot be determined' do + let(:fixture_file_name) { 'empty_package.json' } + + it 'raises an error' do + allow(described_class).to receive(:exit) # Prevent `exit` from terminating the test + expect(Thor::Shell::Basic).to receive_message_chain(:new, :say_error) + .with('Could not determine the Inertia.js framework you are using.') + described_class.guess_the_default_framework(package_json_path) + end + end + end +end