Skip to content

Commit 83e0a3f

Browse files
committed
♻️ Move application.ts rename to install_vite logic
Users may install Inertia on projects that already have Vite installed. Now, the renaming only occurs when the Inertia generator is also installing Vite, ensuring compatibility with existing Vite setups and preventing unnecessary file renaming.
1 parent 6463fba commit 83e0a3f

File tree

2 files changed

+11
-28
lines changed

2 files changed

+11
-28
lines changed

lib/generators/inertia/install/install_generator.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,6 @@ 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-
135128
add_dependencies(*FRAMEWORKS[framework]['packages_ts'])
136129

137130
say 'Copying adding scripts to package.json'
@@ -189,6 +182,7 @@ def install_vite
189182
exit(false)
190183
end
191184
if (capture = run('bundle exec vite install', capture: !verbose?))
185+
rename_application_js_to_ts if typescript?
192186
say 'Vite Rails successfully installed', :green
193187
else
194188
say capture
@@ -199,6 +193,13 @@ def install_vite
199193
end
200194
end
201195

196+
def rename_application_js_to_ts
197+
return unless File.exist?(application_js_path) && application_layout.read.include?("<%= vite_javascript_tag 'application' %>")
198+
199+
FileUtils.mv(application_js_path, application_ts_path)
200+
gsub_file application_layout.to_s, /<%= vite_javascript_tag 'application' %>/, "<%= vite_typescript_tag 'application' %>"
201+
end
202+
202203
def ruby_vite_installed?
203204
return true if package_manager.present? && ruby_vite?
204205

spec/generators/install/install_generator_spec.rb

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,20 @@
3232
let(:ext) { 'ts' }
3333

3434
include_context 'assert framework structure'
35-
include_context 'assert application.js entrypoint renaming'
3635
end
3736
end
3837

3938
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
39+
let(:typescript_enabled?) { args.include?('--typescript') }
4540

46-
it 'renames application.js to application.ts when TypeScript is enabled' do
41+
it 'renames application.js to application.ts if TypeScript flag is enabled' do
4742
expect { generator }.not_to raise_error
4843

49-
if ext == 'ts'
44+
if typescript_enabled?
5045
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be true
5146
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.js'))).to be false
5247
else
5348
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-
6749
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be false
6850
end
6951
end

0 commit comments

Comments
 (0)