Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve framework detection logic and add test coverage #174

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/inertia_rails/generators/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/package_json_files/empty_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dependencies": {}
}
5 changes: 5 additions & 0 deletions spec/fixtures/package_json_files/react_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@inertiajs/react": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions spec/fixtures/package_json_files/svelte4_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@inertiajs/svelte": "1.0.0",
"svelte": "^4.0.0"
}
}
6 changes: 6 additions & 0 deletions spec/fixtures/package_json_files/svelte5_caret_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@inertiajs/svelte": "1.0.0",
"svelte": "^5.0.0"
}
}
6 changes: 6 additions & 0 deletions spec/fixtures/package_json_files/svelte5_exact_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@inertiajs/svelte": "1.0.0",
"svelte": "5.0.0"
}
}
6 changes: 6 additions & 0 deletions spec/fixtures/package_json_files/svelte5_tilde_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@inertiajs/svelte": "1.0.0",
"svelte": "~5.0.0"
}
}
5 changes: 5 additions & 0 deletions spec/fixtures/package_json_files/vue_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@inertiajs/vue3": "1.0.0"
}
}
37 changes: 37 additions & 0 deletions spec/generators/generators_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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