Skip to content

Adjust install generator for typescript setups #217

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 16 additions & 0 deletions lib/generators/inertia/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def install_vite
exit(false)
end
if (capture = run('bundle exec vite install', capture: !verbose?))
rename_application_js_to_ts if typescript?
say 'Vite Rails successfully installed', :green
else
say capture
Expand All @@ -192,6 +193,13 @@ def install_vite
end
end

def rename_application_js_to_ts
return unless File.exist?(application_js_path) && application_layout.read.include?("<%= vite_javascript_tag 'application' %>")

FileUtils.mv(application_js_path, application_ts_path)
gsub_file application_layout.to_s, /<%= vite_javascript_tag 'application' %>/, "<%= vite_typescript_tag 'application' %>"
end

def ruby_vite_installed?
return true if package_manager.present? && ruby_vite?

Expand Down Expand Up @@ -253,6 +261,14 @@ def typescript?
@typescript = options[:typescript] || yes?('Would you like to use TypeScript? (y/n)', :green)
end

def application_js_path
js_file_path('entrypoints/application.js')
end

def application_ts_path
js_file_path('entrypoints/application.ts')
end

def inertia_entrypoint
"inertia.#{typescript? ? 'ts' : 'js'}"
end
Expand Down
33 changes: 31 additions & 2 deletions spec/generators/install/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@
end
end

shared_context 'assert application.js entrypoint renaming' do
let(:typescript_enabled?) { args.include?('--typescript') }

it 'renames application.js to application.ts if TypeScript flag is enabled' do
expect { generator }.not_to raise_error

if typescript_enabled?
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be true
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.js'))).to be false
else
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.js'))).to be true
expect(File.exist?(File.join(destination_root, 'app/frontend/entrypoints/application.ts'))).to be false
end
end
end

context 'without vite' do
before do
prepare_application(with_vite: false)
Expand All @@ -57,6 +73,14 @@
end
end)
end

include_context 'assert application.js entrypoint renaming'

context 'with --typescript' do
let(:args) { super() + %w[--typescript] }

include_context 'assert application.js entrypoint renaming'
end
end
end

Expand Down Expand Up @@ -192,7 +216,7 @@ def expect_packages_for(framework, ext: 'js')
end)
end

def expect_inertia_prepared_for(framework, ext: 'js')
def expect_inertia_prepared_for(framework, ext: 'js', application_js_exists: false)
expect(destination_root).to(have_structure do
case framework
when :react
Expand Down Expand Up @@ -230,10 +254,15 @@ def expect_inertia_prepared_for(framework, ext: 'js')
end
end
file('app/views/layouts/application.html.erb') do
if ext == 'ts'
if ext == 'ts' && application_js_exists
contains('<%= vite_typescript_tag "inertia" %>')
contains("<%= vite_typescript_tag 'application' %>")
elsif ext == 'ts' && !application_js_exists
contains('<%= vite_typescript_tag "inertia" %>')
contains("<%= vite_javascript_tag 'application' %>")
else
contains('<%= vite_javascript_tag "inertia" %>')
contains("<%= vite_javascript_tag 'application' %>")
end
if framework == :react
contains('<%= vite_react_refresh_tag %>')
Expand Down