Skip to content

Commit d06b45e

Browse files
authored
Merge pull request #217 from Andy9822/Fix-install-generator-for-typescript-setups
Adjust install generator for typescript setups
2 parents b1992d7 + 969a56b commit d06b45e

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lib/generators/inertia/install/install_generator.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def install_vite
207207
exit(false)
208208
end
209209
if (capture = run('bundle exec vite install', capture: !verbose?))
210+
rename_application_js_to_ts if typescript?
210211
say 'Vite Rails successfully installed', :green
211212
else
212213
say capture
@@ -219,6 +220,15 @@ def install_vite
219220
end
220221
end
221222

223+
def rename_application_js_to_ts
224+
return unless File.exist?(application_js_path)
225+
return unless application_layout.read.include?("<%= vite_javascript_tag 'application' %>")
226+
227+
FileUtils.mv(application_js_path, application_ts_path)
228+
gsub_file application_layout.to_s, /<%= vite_javascript_tag 'application' %>/,
229+
"<%= vite_typescript_tag 'application' %>"
230+
end
231+
222232
def ruby_vite_installed?
223233
return true if package_manager.present? && ruby_vite?
224234

@@ -280,6 +290,14 @@ def typescript?
280290
@typescript = options[:typescript] || yes?('Would you like to use TypeScript? (y/n)', :green)
281291
end
282292

293+
def application_js_path
294+
js_file_path('entrypoints/application.js')
295+
end
296+
297+
def application_ts_path
298+
js_file_path('entrypoints/application.ts')
299+
end
300+
283301
def inertia_entrypoint
284302
"inertia.#{typescript? ? 'ts' : 'js'}#{'x' if react?}"
285303
end

spec/generators/install/install_generator_spec.rb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@
3535
end
3636
end
3737

38+
shared_context 'assert application.js entrypoint renaming' do
39+
let(:typescript_enabled?) { args.include?('--typescript') }
40+
41+
it 'renames application.js to application.ts if TypeScript flag is enabled' do
42+
expect { generator }.not_to raise_error
43+
44+
if typescript_enabled?
45+
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be true
46+
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.js'))).to be false
47+
else
48+
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.js'))).to be true
49+
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be false
50+
end
51+
end
52+
end
53+
3854
context 'without vite' do
3955
before do
4056
prepare_application(with_vite: false)
@@ -57,6 +73,14 @@
5773
end
5874
end)
5975
end
76+
77+
include_context 'assert application.js entrypoint renaming'
78+
79+
context 'with --typescript' do
80+
let(:args) { super() + %w[--typescript] }
81+
82+
include_context 'assert application.js entrypoint renaming'
83+
end
6084
end
6185
end
6286

@@ -172,7 +196,7 @@ def expect_packages_for(framework, ext: 'js')
172196
end)
173197
end
174198

175-
def expect_inertia_prepared_for(framework, ext: 'js')
199+
def expect_inertia_prepared_for(framework, ext: 'js', application_js_exists: false)
176200
expect(destination_root).to(have_structure do
177201
case framework
178202
when :react
@@ -206,10 +230,15 @@ def expect_inertia_prepared_for(framework, ext: 'js')
206230
end
207231
end
208232
file('app/views/layouts/application.html.erb') do
209-
if ext == 'ts'
233+
if ext == 'ts' && application_js_exists
234+
contains("<%= vite_typescript_tag \"inertia#{'.tsx' if framework == :react}\" %>")
235+
contains("<%= vite_typescript_tag 'application' %>")
236+
elsif ext == 'ts' && !application_js_exists
210237
contains("<%= vite_typescript_tag \"inertia#{'.tsx' if framework == :react}\" %>")
238+
contains("<%= vite_javascript_tag 'application' %>")
211239
else
212240
contains("<%= vite_javascript_tag \"inertia#{'.jsx' if framework == :react}\" %>")
241+
contains("<%= vite_javascript_tag 'application' %>")
213242
end
214243
if framework == :react
215244
contains('<%= vite_react_refresh_tag %>')

0 commit comments

Comments
 (0)