Skip to content

Commit be6dee5

Browse files
committed
Fix install generator for typescript setups
1 parent d1b3c2f commit be6dee5

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

lib/generators/inertia/install/install_generator.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ def install_typescript
125125
return
126126
end
127127

128+
if File.exist?(application_js_path) && application_layout.read.include?("<%= vite_javascript_tag 'application' %>")
129+
say 'Renaming application.js to application.ts'
130+
FileUtils.mv(application_js_path, application_ts_path)
131+
say 'Updating Vite tag on application layout to use TypeScript'
132+
gsub_file application_layout.to_s, /<%= vite_javascript_tag 'application' %>/, "<%= vite_typescript_tag 'application' %>"
133+
end
134+
128135
add_dependencies(*FRAMEWORKS[framework]['packages_ts'])
129136

130137
say 'Copying adding scripts to package.json'
@@ -253,6 +260,14 @@ def typescript?
253260
@typescript = options[:typescript] || yes?('Would you like to use TypeScript? (y/n)', :green)
254261
end
255262

263+
def application_js_path
264+
js_file_path('entrypoints/application.js')
265+
end
266+
267+
def application_ts_path
268+
js_file_path('entrypoints/application.ts')
269+
end
270+
256271
def inertia_entrypoint
257272
"inertia.#{typescript? ? 'ts' : 'js'}"
258273
end

spec/generators/install/install_generator_spec.rb

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,40 @@
3232
let(:ext) { 'ts' }
3333

3434
include_context 'assert framework structure'
35+
include_context 'assert application.js entrypoint renaming'
36+
end
37+
end
38+
39+
shared_context 'assert application.js entrypoint renaming' do
40+
context 'when application.js exists' do
41+
before do
42+
FileUtils.mkdir_p(File.join(destination_root, 'app/frontend/entrypoints'))
43+
File.write(File.join(destination_root, 'app/frontend/entrypoints/application.js'), '// Application JS')
44+
end
45+
46+
it 'renames application.js to application.ts when TypeScript is enabled' do
47+
expect { generator }.not_to raise_error
48+
49+
if ext == 'ts'
50+
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be true
51+
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.js'))).to be false
52+
else
53+
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.js'))).to be true
54+
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be false
55+
end
56+
end
57+
end
58+
59+
context 'when application.js does not exist' do
60+
before do
61+
FileUtils.rm_f(File.join(destination_root, 'app/frontend/entrypoints/application.js'))
62+
end
63+
64+
it 'does not attempt to rename when TypeScript is enabled' do
65+
expect { generator }.not_to raise_error
66+
67+
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be false
68+
end
3569
end
3670
end
3771

@@ -81,6 +115,7 @@
81115
let(:ext) { 'ts' }
82116

83117
include_context 'assert framework structure'
118+
include_context 'assert application.js entrypoint renaming'
84119

85120
context 'with old Inertia version' do
86121
let(:inertia_version) { '1.2.0' }
@@ -101,6 +136,7 @@
101136
let(:ext) { 'ts' }
102137

103138
include_context 'assert framework structure'
139+
include_context 'assert application.js entrypoint renaming'
104140

105141
context 'with old Inertia version' do
106142
let(:inertia_version) { '1.2.0' }
@@ -192,7 +228,7 @@ def expect_packages_for(framework, ext: 'js')
192228
end)
193229
end
194230

195-
def expect_inertia_prepared_for(framework, ext: 'js')
231+
def expect_inertia_prepared_for(framework, ext: 'js', application_js_exists: false)
196232
expect(destination_root).to(have_structure do
197233
case framework
198234
when :react
@@ -230,10 +266,15 @@ def expect_inertia_prepared_for(framework, ext: 'js')
230266
end
231267
end
232268
file('app/views/layouts/application.html.erb') do
233-
if ext == 'ts'
269+
if ext == 'ts' && application_js_exists
270+
contains('<%= vite_typescript_tag "inertia" %>')
271+
contains("<%= vite_typescript_tag 'application' %>")
272+
elsif ext == 'ts' && !application_js_exists
234273
contains('<%= vite_typescript_tag "inertia" %>')
274+
contains("<%= vite_javascript_tag 'application' %>")
235275
else
236276
contains('<%= vite_javascript_tag "inertia" %>')
277+
contains("<%= vite_javascript_tag 'application' %>")
237278
end
238279
if framework == :react
239280
contains('<%= vite_react_refresh_tag %>')

0 commit comments

Comments
 (0)