@@ -65,9 +65,21 @@ def install
6565 install_example_page if options [ :example_page ]
6666
6767 say 'Copying bin/dev'
68- copy_file " #{ __dir__ } /templates/ dev" , 'bin/dev'
68+ copy_file ' dev' , 'bin/dev'
6969 chmod 'bin/dev' , 0o755 , verbose : verbose?
7070
71+ if install_vite?
72+ say 'Adding redirect to localhost'
73+ routing_code = <<~RUBY
74+ \n # Redirect to localhost from 127.0.0.1 to use same IP address with Vite server
75+ constraints(host: "127.0.0.1") do
76+ get "(*path)", to: redirect { |params, req| "\# {req.protocol}localhost:\# {req.port}/\# {params[:path]}" }
77+ end
78+ RUBY
79+
80+ route routing_code
81+ end
82+
7183 say "Inertia's Rails adapter successfully installed" , :green
7284 end
7385
@@ -87,12 +99,15 @@ def install_inertia
8799 end
88100
89101 say "Copying #{ inertia_entrypoint } entrypoint"
90- template "#{ framework } /#{ inertia_entrypoint } " , js_file_path ( "entrypoints/#{ inertia_entrypoint } " )
102+ copy_file "#{ framework } /#{ inertia_entrypoint } " , js_file_path ( "entrypoints/#{ inertia_entrypoint } " )
103+
104+ say 'Copying InertiaController'
105+ copy_file 'inertia_controller.rb' , file_path ( 'app/controllers/inertia_controller.rb' )
91106
92107 if application_layout . exist?
93108 say "Adding #{ inertia_entrypoint } script tag to the application layout"
94109 headers = <<-ERB
95- <%= #{ vite_tag } "inertia" %>
110+ <%= #{ vite_tag } %>
96111 <%= inertia_ssr_head %>
97112 ERB
98113 insert_into_file application_layout . to_s , headers , after : "<%= vite_client_tag %>\n "
@@ -103,14 +118,14 @@ def install_inertia
103118 before : '<%= vite_client_tag %>'
104119 end
105120
106- gsub_file application_layout . to_s , /<title>/ , '<title inertia>' unless svelte?
121+ gsub_file application_layout . to_s , /<title>/ , '<title data- inertia>' unless svelte?
107122 else
108123 say_error 'Could not find the application layout file. Please add the following tags manually:' , :red
109124 say_error '- <title>...</title>'
110- say_error '+ <title inertia>...</title>'
125+ say_error '+ <title data- inertia>...</title>'
111126 say_error '+ <%= inertia_ssr_head %>'
112127 say_error '+ <%= vite_react_refresh_tag %>' if framework == 'react'
113- say_error "+ <%= #{ vite_tag } \" inertia \" %>"
128+ say_error "+ <%= #{ vite_tag } %>"
114129 end
115130 end
116131
@@ -192,6 +207,8 @@ def install_vite
192207 say_error 'Failed to install Vite Rails' , :red
193208 exit ( false )
194209 end
210+
211+ add_package_manager_to_bin_setup
195212 end
196213 end
197214 end
@@ -258,11 +275,13 @@ def typescript?
258275 end
259276
260277 def inertia_entrypoint
261- "inertia.#{ typescript? ? 'ts' : 'js' } "
278+ "inertia.#{ typescript? ? 'ts' : 'js' } #{ 'x' if react? } "
262279 end
263280
264281 def vite_tag
265- typescript? ? 'vite_typescript_tag' : 'vite_javascript_tag'
282+ tag = typescript? ? 'vite_typescript_tag' : 'vite_javascript_tag'
283+ filename = "\" #{ react? ? inertia_entrypoint : 'inertia' } \" "
284+ "#{ tag } #{ filename } "
266285 end
267286
268287 def inertia_resolved_version
@@ -280,6 +299,10 @@ def svelte?
280299 framework . start_with? 'svelte'
281300 end
282301
302+ def react?
303+ framework . start_with? 'react'
304+ end
305+
283306 def inertia_package
284307 "#{ FRAMEWORKS [ framework ] [ 'inertia_package' ] } @#{ options [ :inertia_version ] } "
285308 end
@@ -288,6 +311,26 @@ def framework
288311 @framework ||= options [ :framework ] || ask ( 'What framework do you want to use with Inertia?' , :green ,
289312 limited_to : FRAMEWORKS . keys , default : 'react' )
290313 end
314+
315+ def add_package_manager_to_bin_setup
316+ setup_file = file_path ( 'bin/setup' )
317+ return unless File . exist? ( setup_file )
318+
319+ content = File . read ( setup_file )
320+ pm_name = package_manager . name
321+
322+ # Check if package manager install already exists
323+ return if content . include? ( "#{ pm_name } install" )
324+
325+ if content . match? ( /system\( 'bundle check'\) \| \| system!\( 'bundle install'\) / )
326+ say 'Adding package manager install to bin/setup'
327+ cmd = "system! '#{ pm_name } install'"
328+ insert_into_file setup_file , " #{ cmd } \n " ,
329+ after : /system\( 'bundle check'\) \| \| system!\( 'bundle install'\) \n /
330+ else
331+ say_status "Couldn't add `#{ cmd } ` script to bin/setup, add it manually" , :red
332+ end
333+ end
291334 end
292335 end
293336end
0 commit comments